Bimba.git

ref: 2f14b9ea523c41743d6e0ed0da2d2f3cba903e26

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
89
90
91
92
93
94
95
96
97
98
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 android.widget.FrameLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
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

// todo search: https://github.com/material-components/material-components-android/blob/master/docs/components/Search.md

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)
		}

		val root = binding.root
		ViewCompat.setOnApplyWindowInsetsListener(root) { view, windowInsets ->
			val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
			view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply {
				topMargin = insets.top
			}
			WindowInsetsCompat.CONSUMED
		}

		binding.searchBar.lastSuggestions = lastSuggestions
		val cm = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		binding.searchBar.addTextChangeListener(
			homeViewModel.SearchBarWatcher(requireContext(), 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
	}
}