1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

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

View File

@ -26,12 +26,11 @@ import java.time.OffsetDateTime
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
class FeedViewModel( class FeedViewModel(
val applicationContext: Context, private val applicationContext: Context,
groupId: Long = FeedGroupEntity.GROUP_ALL_ID, groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
initialShowPlayedItems: Boolean = true initialShowPlayedItems: Boolean = true
) : ViewModel() { ) : ViewModel() {
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext) private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>() private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>()
private val streamItems = toggleShowPlayedItems private val streamItems = toggleShowPlayedItems
@ -85,21 +84,32 @@ class FeedViewModel(
toggleShowPlayedItems.onNext(showPlayedItems) toggleShowPlayedItems.onNext(showPlayedItems)
} }
fun savePlayedItemsToggle(showPlayedItems: Boolean) = sharedPreferences.edit { fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
this.putBoolean(applicationContext.getString(R.string.show_played_items_filter_key), showPlayedItems) PreferenceManager.getDefaultSharedPreferences(applicationContext).edit {
this.apply() 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( class Factory(
private val context: Context, private val context: Context,
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID, private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID
private val showPlayedItems: Boolean
) : ViewModelProvider.Factory { ) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T { 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> <string name="saved_tabs_key" translatable="false">saved_tabs_key</string>
<!-- Key values --> <!-- 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_video_key" translatable="false">download_path</string>
<string name="download_path_audio_key" translatable="false">download_path_audio</string> <string name="download_path_audio_key" translatable="false">download_path_audio</string>