Bimba.git

commit 7ad0ef262309552fdf48ef7596a8fe46a520fcf7

Author: Adam <git@apiote.xyz>

moves getting server to Api

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


diff --git a/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt b/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
index f29db966823fce52669d6fb63f08c1caaff82fe3..06c49b9b35338ae17af9b98256b50a63ef00b4b6 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
@@ -1,5 +1,7 @@
 package ml.adamsprogs.bimba.api
 
+import android.content.Context
+import android.content.Context.MODE_PRIVATE
 import android.net.ConnectivityManager
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
@@ -10,7 +12,18 @@ import java.net.HttpURLConnection
 import java.net.URL
 import java.net.URLEncoder
 
-data class Server(val host: String, val token: String, val feeds: String)
+data class Server(val host: String, val token: String, val feeds: String) {
+	companion object {
+		fun get(context: Context): Server {
+			val preferences = context.getSharedPreferences("shp", MODE_PRIVATE)
+			val host = preferences.getString("host", "bimba.apiote.xyz")!!
+			return Server(
+				host, preferences.getString("token", "")!!,
+				preferences.getString("${host}_feeds", "")!!
+			)
+		}
+	}
+}
 
 data class Result(val stream: InputStream?, val error: Error?)
 




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 321dae34daa24c2f563b4452185ec9defe9fa59f..07b12ab2f43b4b158cf835feaede03a13554b398 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
@@ -38,10 +38,7 @@
 		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
-			)
+			homeViewModel.SearchBarWatcher(requireContext(), cm)
 		)
 		binding.searchBar.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener {
 			override fun onButtonClicked(buttonCode: Int) {




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 c1bc4e04500f5a7bb2c82ba34090c5b5b142372a..abaafb36ea21072a914aec949226368972bad7ea 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,6 +1,6 @@
 package ml.adamsprogs.bimba.dashboard.ui.home
 
-import android.content.SharedPreferences
+import android.content.Context
 import android.net.ConnectivityManager
 import android.os.Handler
 import android.os.Looper
@@ -45,7 +45,7 @@ 		}
 	}
 
 	inner class SearchBarWatcher(
-		private val preferences: SharedPreferences,
+		private val context: Context,
 		private val cm: ConnectivityManager
 	) :
 		TextWatcher {
@@ -61,14 +61,10 @@
 		override fun afterTextChanged(s: Editable?) {
 			handler.removeCallbacks(workRunnable)
 			workRunnable = Runnable {
-				val host = preferences.getString("host", "bimba.apiote.xyz")!!
 				val text = s.toString()
 				getItems(
 					cm,
-					Server(
-						host, preferences.getString("token", "")!!,
-						preferences.getString("${host}_feeds", "")!!
-					), text
+					Server.get(context), text
 				)
 			}
 			handler.postDelayed(




diff --git a/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt
index b64e65eb48f499695f9a5edb77d9e00536360921..ee3f919a34ed8b3505c7eed4f017e97b4ef51a84 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt
@@ -2,7 +2,6 @@ package ml.adamsprogs.bimba.departures
 
 import android.content.Context
 import android.content.Intent
-import android.content.SharedPreferences
 import android.net.ConnectivityManager
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
@@ -27,8 +26,6 @@
 class DeparturesActivity : AppCompatActivity() {
 	private var _binding: ActivityDeparturesBinding? = null
 	private val binding get() = _binding!!
-
-	private lateinit var preferences: SharedPreferences
 
 	private lateinit var adapter: BimbaDeparturesAdapter
 
@@ -57,8 +54,6 @@ 		}
 		binding.departuresRecycler.adapter = adapter
 		WindowCompat.setDecorFitsSystemWindows(window, false)
 
-		preferences = getSharedPreferences("shp", MODE_PRIVATE)
-
 		getDepartures()
 	}
 
@@ -81,14 +76,10 @@ 	}
 
 	private fun getDepartures() {
 		val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
-		val host = preferences.getString("host", "bimba.apiote.xyz")!!
 		MainScope().launch {
 			val departuresResult = getDepartures(
 				cm,
-				Server(
-					host, preferences.getString("token", "")!!,
-					preferences.getString("${host}_feeds", "")!!
-				), getCode()
+				Server.get(this@DeparturesActivity), getCode()
 			)
 			val response = if (departuresResult.stream != null) {
 				unmarshallDepartureResponse(departuresResult.stream)




diff --git a/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
index 2027f08cab67cdd6eea79c994a65250c32aa4720..4f1dc4f30f3299cc6bede2235041838b9920044e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
@@ -3,7 +3,6 @@
 import android.annotation.SuppressLint
 import android.content.Context
 import android.content.Intent
-import android.content.SharedPreferences
 import android.location.Location
 import android.location.LocationListener
 import android.location.LocationManager
@@ -35,7 +34,6 @@ 	private var _binding: ActivityResultsBinding? = null
 	private val binding get() = _binding!!
 
 	private lateinit var adapter: BimbaResultsAdapter
-	private lateinit var preferences: SharedPreferences
 
 	private val handler = Handler(Looper.getMainLooper())
 	private var runnable = Runnable {}
@@ -45,8 +43,6 @@ 		super.onCreate(savedInstanceState)
 		_binding = ActivityResultsBinding.inflate(layoutInflater)
 		setContentView(binding.root)
 
-		preferences = getSharedPreferences("shp", MODE_PRIVATE)
-
 		binding.resultsRecycler.layoutManager = LinearLayoutManager(this)
 		adapter = BimbaResultsAdapter(layoutInflater, this, listOf()) {
 			when (it) {
@@ -76,13 +72,7 @@ 			}
 			Mode.MODE_SEARCH -> {
 				val query = intent.extras?.getString("query")!!
 				supportActionBar?.title = "Results for ‘$query’"
-				val host = preferences.getString("host", "bimba.apiote.xyz")!!
-				getItemsByQuery(
-					Server(
-						host, preferences.getString("token", "")!!,
-						preferences.getString("${host}_feeds", "")!!
-					), query
-				)
+				getItemsByQuery(Server.get(this), query)
 			}
 		}
 	}
@@ -105,13 +95,7 @@
 	override fun onLocationChanged(location: Location) {
 		handler.removeCallbacks(runnable)
 		val code = OpenLocationCode.encode(location.latitude, location.longitude)
-		val host = preferences.getString("host", "bimba.apiote.xyz")!!
-		getItemsByLocation(
-			Server(
-				host, preferences.getString("token", "")!!,
-				preferences.getString("${host}_feeds", "")!!
-			), code
-		)
+		getItemsByLocation(Server.get(this), code)
 	}
 
 	override fun onDestroy() {