Bimba.git

ref: 9c36169de2028b25d2ff12182c57699ab1baed12

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
99
package ml.adamsprogs.bimba.dashboard.ui.home

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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!!

	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		val homeViewModel =
			ViewModelProvider(this)[HomeViewModel::class.java]

		_binding = FragmentHomeBinding.inflate(inflater, container, false)
		val root: View = binding.root

		binding.searchBar.lastSuggestions = listOf<Item>()
		homeViewModel.items.observe(viewLifecycleOwner) {
			binding.searchBar.updateLastSuggestions(it.take(6))  // xxx workaround for suggestions behind navbar; should be paginated server-side
		}
		binding.searchBar.addTextChangeListener(SearchBarWatcher(homeViewModel))
		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 on suggestion click go to line or stop
		// todo on searchbar focus && if != '' -> populate suggestions

		return root
	}

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

class SearchBarWatcher(private val homeViewModel: HomeViewModel) : TextWatcher {
	private val handler = Handler(Looper.getMainLooper())
	private var workRunnable = Runnable {}

	override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
	}

	override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
	}

	override fun afterTextChanged(s: Editable?) {
		handler.removeCallbacks(workRunnable)
		workRunnable = Runnable {
			val text = s.toString()
			homeViewModel.getItems(text)
		}
		handler.postDelayed(workRunnable, 1000) // todo make good time (probably between 500, 1000ms)
	}
}