Fixed toggle not in sync with list after app restart + refactored the code a bit

This commit is contained in:
litetex 2021-11-11 19:46:15 +01:00
parent c35fe4f3f1
commit adf9badbf6
3 changed files with 24 additions and 14 deletions

View File

@ -126,9 +126,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
_feedBinding = FragmentFeedBinding.bind(rootView)
super.onViewCreated(rootView, savedInstanceState)
val factory = FeedViewModel.Factory(requireContext(), groupId, showPlayedItems)
val factory = FeedViewModel.Factory(requireContext(), groupId)
viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java)
showPlayedItems = viewModel.getSavedPlayedItemsToggle()
showPlayedItems = viewModel.getShowPlayedItemsFromPreferences()
viewModel.stateLiveData.observe(viewLifecycleOwner, { it?.let(::handleResult) })
groupAdapter = GroupieAdapter().apply {
@ -214,7 +214,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
showPlayedItems = !item.isChecked
updateTogglePlayedItemsButton(item)
viewModel.togglePlayedItems(showPlayedItems)
viewModel.savePlayedItemsToggle(showPlayedItems)
viewModel.saveShowPlayedItemsToPreferences(showPlayedItems)
}
return super.onOptionsItemSelected(item)

View File

@ -26,12 +26,11 @@ import java.time.OffsetDateTime
import java.util.concurrent.TimeUnit
class FeedViewModel(
val applicationContext: Context,
private val applicationContext: Context,
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
initialShowPlayedItems: Boolean = true
) : ViewModel() {
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>()
private val streamItems = toggleShowPlayedItems
@ -85,21 +84,32 @@ class FeedViewModel(
toggleShowPlayedItems.onNext(showPlayedItems)
}
fun savePlayedItemsToggle(showPlayedItems: Boolean) = sharedPreferences.edit {
this.putBoolean(applicationContext.getString(R.string.show_played_items_filter_key), showPlayedItems)
this.apply()
}
fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit {
this.putBoolean(applicationContext.getString(R.string.feed_show_played_items_key), showPlayedItems)
this.apply()
}
fun getSavedPlayedItemsToggle() = sharedPreferences.getBoolean(applicationContext.getString(R.string.show_played_items_filter_key), true)
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(applicationContext)
companion object {
private fun getShowPlayedItemsFromPreferences(context: Context) =
PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(context.getString(R.string.feed_show_played_items_key), true)
}
class Factory(
private val context: Context,
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
private val showPlayedItems: Boolean
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return FeedViewModel(context.applicationContext, groupId, showPlayedItems) as T
return FeedViewModel(
context.applicationContext,
groupId,
// Read initial value from preferences
getShowPlayedItemsFromPreferences(context.applicationContext)
) as T
}
}
}

View File

@ -14,7 +14,7 @@
<string name="saved_tabs_key" translatable="false">saved_tabs_key</string>
<!-- Key values -->
<string name="show_played_items_filter_key" translatable="false">show_played_items_preference_key</string>
<string name="feed_show_played_items_key" translatable="false">feed_show_played_items</string>
<string name="download_path_video_key" translatable="false">download_path</string>
<string name="download_path_audio_key" translatable="false">download_path_audio</string>