Skip to content

Commit adf9bad

Browse files
committed
Fixed toggle not in sync with list after app restart + refactored the code a bit
1 parent c35fe4f commit adf9bad

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
126126
_feedBinding = FragmentFeedBinding.bind(rootView)
127127
super.onViewCreated(rootView, savedInstanceState)
128128

129-
val factory = FeedViewModel.Factory(requireContext(), groupId, showPlayedItems)
129+
val factory = FeedViewModel.Factory(requireContext(), groupId)
130130
viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java)
131-
showPlayedItems = viewModel.getSavedPlayedItemsToggle()
131+
showPlayedItems = viewModel.getShowPlayedItemsFromPreferences()
132132
viewModel.stateLiveData.observe(viewLifecycleOwner, { it?.let(::handleResult) })
133133

134134
groupAdapter = GroupieAdapter().apply {
@@ -214,7 +214,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
214214
showPlayedItems = !item.isChecked
215215
updateTogglePlayedItemsButton(item)
216216
viewModel.togglePlayedItems(showPlayedItems)
217-
viewModel.savePlayedItemsToggle(showPlayedItems)
217+
viewModel.saveShowPlayedItemsToPreferences(showPlayedItems)
218218
}
219219

220220
return super.onOptionsItemSelected(item)

app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import java.time.OffsetDateTime
2626
import java.util.concurrent.TimeUnit
2727

2828
class FeedViewModel(
29-
val applicationContext: Context,
29+
private val applicationContext: Context,
3030
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
3131
initialShowPlayedItems: Boolean = true
3232
) : ViewModel() {
3333
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
34-
private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
3534

3635
private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>()
3736
private val streamItems = toggleShowPlayedItems
@@ -85,21 +84,32 @@ class FeedViewModel(
8584
toggleShowPlayedItems.onNext(showPlayedItems)
8685
}
8786

88-
fun savePlayedItemsToggle(showPlayedItems: Boolean) = sharedPreferences.edit {
89-
this.putBoolean(applicationContext.getString(R.string.show_played_items_filter_key), showPlayedItems)
90-
this.apply()
91-
}
87+
fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
88+
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit {
89+
this.putBoolean(applicationContext.getString(R.string.feed_show_played_items_key), showPlayedItems)
90+
this.apply()
91+
}
9292

93-
fun getSavedPlayedItemsToggle() = sharedPreferences.getBoolean(applicationContext.getString(R.string.show_played_items_filter_key), true)
93+
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(applicationContext)
94+
95+
companion object {
96+
private fun getShowPlayedItemsFromPreferences(context: Context) =
97+
PreferenceManager.getDefaultSharedPreferences(context)
98+
.getBoolean(context.getString(R.string.feed_show_played_items_key), true)
99+
}
94100

95101
class Factory(
96102
private val context: Context,
97-
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
98-
private val showPlayedItems: Boolean
103+
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID
99104
) : ViewModelProvider.Factory {
100105
@Suppress("UNCHECKED_CAST")
101106
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
102-
return FeedViewModel(context.applicationContext, groupId, showPlayedItems) as T
107+
return FeedViewModel(
108+
context.applicationContext,
109+
groupId,
110+
// Read initial value from preferences
111+
getShowPlayedItemsFromPreferences(context.applicationContext)
112+
) as T
103113
}
104114
}
105115
}

app/src/main/res/values/settings_keys.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<string name="saved_tabs_key" translatable="false">saved_tabs_key</string>
1515

1616
<!-- Key values -->
17-
<string name="show_played_items_filter_key" translatable="false">show_played_items_preference_key</string>
17+
<string name="feed_show_played_items_key" translatable="false">feed_show_played_items</string>
1818
<string name="download_path_video_key" translatable="false">download_path</string>
1919
<string name="download_path_audio_key" translatable="false">download_path_audio</string>
2020

0 commit comments

Comments
 (0)