@@ -26,12 +26,11 @@ import java.time.OffsetDateTime
2626import java.util.concurrent.TimeUnit
2727
2828class 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}
0 commit comments