Bimba.git

commit 7eb98e065355d9509fc20cc86751cd344eef81aa

Author: Adam <git@apiote.xyz>

reformat TextWatcher logic to HomeViewModel

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeFragment.kt b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeFragment.kt
index 21d9a6aed2b9609ca89d5ba99509f1f4eee9a4ba..6485bded6ea46f7590d1e0a1a69fe774fe8b691e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeFragment.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeFragment.kt
@@ -1,11 +1,6 @@
 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
@@ -17,7 +12,6 @@ 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() {
@@ -26,6 +20,8 @@ 	private var _binding: FragmentHomeBinding? = null
 
 	private val binding get() = _binding!!
 
+	private var lastSuggestions = listOf<Item>()
+
 	override fun onCreateView(
 		inflater: LayoutInflater,
 		container: ViewGroup?,
@@ -43,7 +39,13 @@ 		binding.searchBar.lastSuggestions = listOf()
 		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.lastSuggestions = lastSuggestions
+		binding.searchBar.addTextChangeListener(
+			homeViewModel.SearchBarWatcher(
+				requireContext().getSharedPreferences("shp", AppCompatActivity.MODE_PRIVATE)
+			)
+		)
 		binding.searchBar.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener {
 			override fun onButtonClicked(buttonCode: Int) {
 				when (buttonCode) {
@@ -79,31 +81,5 @@
 	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)
 	}
 }
\ No newline at end of file




diff --git a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt
index c6d5594594dd7abd59a9cb183108acb16517c41d..2f80380ccffae7b029abef53266549b5be85a5c8 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt
@@ -1,5 +1,10 @@
 package ml.adamsprogs.bimba.dashboard.ui.home
 
+import android.content.SharedPreferences
+import android.os.Handler
+import android.os.Looper
+import android.text.Editable
+import android.text.TextWatcher
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
@@ -36,6 +41,32 @@ 				else -> {
 					 TODO("Error response")
 				}
 			}
+		}
+	}
+
+	inner class SearchBarWatcher(private val preferences: 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 = preferences.getString("host", "bimba.apiote.xyz")!!
+				val text = s.toString()
+				getItems(
+					Server(
+						host, preferences.getString("token", "")!!,
+						preferences.getString("${host}_feeds", "")!!
+					), text)
+			}
+			handler.postDelayed(workRunnable, 1000) // todo(ux,low) make good time (probably between 500, 1000ms)
 		}
 	}
 }
\ No newline at end of file