Bimba.git

ref: 0f3ae9e13096218a22641ba4684cd48d77f987e3

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
100
101
102
103
104
105
106
107
108
109
package ml.adamsprogs.bimba.dashboard.ui.home

import android.content.SharedPreferences
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.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.api.Server
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

		val shp = requireContext().getSharedPreferences("shp", AppCompatActivity.MODE_PRIVATE)

		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, shp))  // todo pass shp and host
		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 searchbar focus && if != '' -> populate suggestions

		return root
	}

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

class SearchBarWatcher(private val homeViewModel: HomeViewModel, private val shp: SharedPreferences) :
	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 host = shp.getString("host", "bimba.apiote.xyz")!!
			val text = s.toString()
			homeViewModel.getItems(
				Server(
				host, shp.getString("token", "")!!,
				shp.getString("${host}_feeds", "")!!
			), text)
		}
		handler.postDelayed(workRunnable, 1000) // todo make good time (probably between 500, 1000ms)
	}
}