Bimba.git

ref: e200ec2c2898c340acd1e8aca5452e0dd4acf877

app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeFragment.kt


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package ml.adamsprogs.bimba.dashboard.ui.home

import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.mancj.materialsearchbar.MaterialSearchBar
import com.mancj.materialsearchbar.MaterialSearchBar.BUTTON_NAVIGATION
import ml.adamsprogs.bimba.search.BimbaSuggestionsAdapter
import ml.adamsprogs.bimba.dashboard.MainActivity
import ml.adamsprogs.bimba.api.Item
import ml.adamsprogs.bimba.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {
	private var _binding: FragmentHomeBinding? = null
	private val binding get() = _binding!!

	private var lastSuggestions = listOf<Item>()

	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		_binding = FragmentHomeBinding.inflate(inflater, container, false)

		val homeViewModel =
			ViewModelProvider(this)[HomeViewModel::class.java]
		homeViewModel.items.observe(viewLifecycleOwner) {
			binding.searchBar.updateLastSuggestions(it)
		}

		binding.searchBar.lastSuggestions = lastSuggestions
		val cm = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		binding.searchBar.addTextChangeListener(
			homeViewModel.SearchBarWatcher(
				requireContext().getSharedPreferences("shp", AppCompatActivity.MODE_PRIVATE),
				cm
			)
		)
		binding.searchBar.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener {
			override fun onButtonClicked(buttonCode: Int) {
				when (buttonCode) {
					BUTTON_NAVIGATION -> {
						(context as MainActivity).onNavigationClicked()
					}
				}
			}

			override fun onSearchStateChanged(enabled: Boolean) {
			}

			override fun onSearchConfirmed(text: CharSequence?) {
				binding.searchBar.clearSuggestions()
				(context as MainActivity).onSearchClicked(text)
			}
		})
		binding.searchBar.setCardViewElevation(0)
		binding.searchBar.setCustomSuggestionAdapter(BimbaSuggestionsAdapter(layoutInflater, context) {
			binding.searchBar.clearSuggestions()
			(context as MainActivity).onSuggestionClicked(it)
		})

		binding.floatingActionButton.setOnClickListener {
			binding.searchBar.clearSuggestions()
			(context as MainActivity).onGpsClicked(it)
		}
		/* todo(ux,low) on searchbar focus && if != '' -> populate suggestions
		binding.searchBar.searchEditText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
			Log.v("Focus", "$hasFocus")
			if(binding.searchBar.text != "" && hasFocus) {
				binding.searchBar.lastSuggestions = lastSuggestions
			}
		}*/

		return binding.root
	}

	override fun onDestroyView() {
		super.onDestroyView()
		_binding = null
	}
}