Bimba.git

commit 58f495a882e8c075c728f352596d1a3495c183d5

Author: Adam Evyčędo <git@apiote.xyz>

fix feed chooser

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


diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt
index 2642ac78ca1681506a386eba7c3b4d7a86bea3ab..aaabcdc6a63e0a4d6da769274b660fd5db0611b0 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt
@@ -11,6 +11,7 @@ import android.view.View
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.content.res.AppCompatResources
 import androidx.core.content.edit
+import androidx.lifecycle.ViewModelProvider
 import androidx.recyclerview.widget.LinearLayoutManager
 import kotlinx.coroutines.MainScope
 import kotlinx.coroutines.launch
@@ -28,23 +29,26 @@ // TODO on internet connection -> getServer
 // TODO swipe to refresh?
 
 class FeedChooserActivity : AppCompatActivity() {
+	private lateinit var viewModel: FeedsViewModel
 	private var _binding: ActivityFeedChooserBinding? = null
 	private val binding get() = _binding!!
 
 	private lateinit var adapter: BimbaFeedInfoAdapter
 
 	private val feeds: MutableMap<String, FeedInfo> = mutableMapOf()
-	private lateinit var feedsSettings: FeedsSettings
 
 	override fun onCreate(savedInstanceState: Bundle?) {
 		super.onCreate(savedInstanceState)
 		_binding = ActivityFeedChooserBinding.inflate(layoutInflater)
 		setContentView(binding.root)
 
+		viewModel = ViewModelProvider(this)[FeedsViewModel::class.java]
+
 		migrateFeedsSettings(this)
-		feedsSettings = FeedsSettings.load(this, Server.get(this).apiPath)
+		viewModel.loadSettings(this)
 
 		setUpRecycler()
+
 		getServer()
 
 		binding.button.setOnClickListener {
@@ -52,31 +56,33 @@ 			moveOn()
 		}
 	}
 
+	private fun showBottomSheet(feedID: String) {
+		FeedBottomSheet(
+			feedID,
+			feeds,
+			viewModel.settings.value ?: FeedsSettings(mutableMapOf())
+		) { settings ->
+			if (settings != null) {
+				viewModel.setSettings(feedID, settings)
+				updateItems(null, viewModel.settings.value, false)
+				adapter.notifyItemChanged(adapter.getItemPosition(feedID))
+			}
+		}.show(supportFragmentManager, FeedBottomSheet.TAG)
+	}
+
 	private fun setUpRecycler() {
 		binding.resultsRecycler.layoutManager = LinearLayoutManager(this)
 		adapter =
 			BimbaFeedInfoAdapter(
 				layoutInflater,
 				feeds.map { it.value }.sortedBy { it.name },
-				feedsSettings,
+				viewModel.settings.value!!,
 				this,
-				{ feedID ->
-					FeedBottomSheet(feeds[feedID]!!, feedsSettings.settings[feedID]) {
-						if (it != null) {
-							feedsSettings.settings[feedID] = it
-							updateItems(null, feedsSettings, false)
-							adapter.notifyItemChanged(adapter.getItemPosition(feedID))
-						}
-					}.show(supportFragmentManager, FeedBottomSheet.TAG)
+				{
+					showBottomSheet(it)
 				},
-				{ feedID, isEnabled ->
-					feedsSettings.settings[feedID] =
-						feedsSettings.settings[feedID]?.copy(enabled = isEnabled) ?: FeedSettings(
-							enabled = false,
-							useOnline = true
-						)
-					updateItems(null, feedsSettings, false)
-				})
+				{ feedID, isEnabled -> viewModel.setEnabled(feedID, isEnabled) }
+			)
 		binding.resultsRecycler.adapter = adapter
 	}
 
@@ -111,7 +117,7 @@ 		}
 	}
 
 	private fun moveOn() {
-		feedsSettings.save(this, Server.get(this))
+		viewModel.settings.value?.save(this, Server.get(this))
 		val preferences = getSharedPreferences("shp", MODE_PRIVATE)
 		preferences.edit(true) {
 			putBoolean("inFeedsTransaction", false)




diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt
index e12daa5c56560c84de23ba21bc3cd3ef267ef5b1..c2ba176fd6e1bc6518d7eeb418e2f52703c87bb6 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt
@@ -141,8 +141,9 @@ 	}
 }
 
 class FeedBottomSheet(
-	private var feed: FeedInfo,
-	private var settings: FeedSettings?,
+	private val feedID: String,
+	private val feeds: MutableMap<String, FeedInfo>,
+	private val feedsSettings: FeedsSettings,
 	private val onDismiss: (FeedSettings?) -> Unit
 ) :
 	BottomSheetDialogFragment() {
@@ -150,12 +151,16 @@ 	companion object {
 		const val TAG = "DepartureBottomSheet"
 	}
 
+	private var settings: FeedSettings? = null
+
 	override fun onCreateView(
 		inflater: LayoutInflater,
 		container: ViewGroup?,
 		savedInstanceState: Bundle?
 	): View {
 		val content = inflater.inflate(R.layout.feed_bottom_sheet, container, false)
+		val feed = feeds[feedID]!!
+		var settings = feedsSettings.settings[feedID]
 		content.findViewById<MaterialTextView>(R.id.title).text = feed.name
 		content.findViewById<MaterialTextView>(R.id.description).text = feed.description
 		content.findViewById<MaterialTextView>(R.id.outdated_info_warning).visibility =
@@ -169,8 +174,8 @@ 			content.findViewById(R.id.timetable_validity).apply {
 				visibility = View.VISIBLE
 				text = getString(
 					R.string.current_timetable_validity,
-					feed.validSince!!.format(DateTimeFormatter.ISO_LOCAL_DATE),
-					feed.validTill!!.format(DateTimeFormatter.ISO_LOCAL_DATE)
+					feed.validSince.format(DateTimeFormatter.ISO_LOCAL_DATE),
+					feed.validTill.format(DateTimeFormatter.ISO_LOCAL_DATE)
 				)
 			}
 		}
@@ -184,6 +189,7 @@ 				settings = settings?.copy(useOnline = isChecked) ?: FeedSettings(
 					enabled = true,
 					useOnline = isChecked
 				)
+				this@FeedBottomSheet.settings = settings
 			}
 		}
 		return content
@@ -191,6 +197,7 @@ 	}
 
 	override fun onDismiss(dialog: DialogInterface) {
 		onDismiss(settings)
+		settings = null
 		super.onDismiss(dialog)
 	}
 }
\ No newline at end of file




diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedsViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedsViewModel.kt
new file mode 100644
index 0000000000000000000000000000000000000000..22faaa10edbd16162766777f7d60ae45c7713ed5
--- /dev/null
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedsViewModel.kt
@@ -0,0 +1,29 @@
+package xyz.apiote.bimba.czwek.settings.feeds
+
+import android.content.Context
+import android.util.Log
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+import xyz.apiote.bimba.czwek.api.Server
+
+class FeedsViewModel : ViewModel() {
+	private val _settings = MutableLiveData<FeedsSettings>()
+	val settings: LiveData<FeedsSettings> = _settings
+
+	fun loadSettings(context: Context) {
+		_settings.value = FeedsSettings.load(context, Server.get(context).apiPath)
+	}
+
+	fun setSettings(feedID: String, feedSettings: FeedSettings) {
+		val feedsSettings = _settings.value ?: FeedsSettings(mutableMapOf())
+		feedsSettings.settings[feedID] = feedSettings
+		_settings.value = feedsSettings
+		Log.i("Settings", "${_settings.value}")
+	}
+
+	fun setEnabled(feedID: String, enabled: Boolean) {
+		val feedSettings = (_settings.value ?: FeedsSettings(mutableMapOf())).settings[feedID]
+		setSettings(feedID, feedSettings?.copy(enabled = enabled) ?: FeedSettings(enabled, true))
+	}
+}
\ No newline at end of file