Skip to content

Commit 311d392

Browse files
Use Application instead of Context in FeedViewModel.
1 parent 404c13d commit 311d392

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.local.feed
22

3+
import android.app.Application
34
import android.content.Context
45
import androidx.core.content.edit
56
import androidx.lifecycle.LiveData
@@ -13,6 +14,7 @@ import io.reactivex.rxjava3.core.Flowable
1314
import io.reactivex.rxjava3.functions.Function5
1415
import io.reactivex.rxjava3.processors.BehaviorProcessor
1516
import io.reactivex.rxjava3.schedulers.Schedulers
17+
import org.schabi.newpipe.App
1618
import org.schabi.newpipe.R
1719
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
1820
import org.schabi.newpipe.database.stream.StreamWithState
@@ -27,12 +29,12 @@ import java.time.OffsetDateTime
2729
import java.util.concurrent.TimeUnit
2830

2931
class FeedViewModel(
30-
private val applicationContext: Context,
32+
private val application: Application,
3133
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
3234
initialShowPlayedItems: Boolean = true,
3335
initialShowFutureItems: Boolean = true
3436
) : ViewModel() {
35-
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
37+
private val feedDatabaseManager = FeedDatabaseManager(application)
3638

3739
private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>()
3840
private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
@@ -114,24 +116,24 @@ class FeedViewModel(
114116
}
115117

116118
fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
117-
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit {
118-
this.putBoolean(applicationContext.getString(R.string.feed_show_played_items_key), showPlayedItems)
119+
PreferenceManager.getDefaultSharedPreferences(application).edit {
120+
this.putBoolean(application.getString(R.string.feed_show_played_items_key), showPlayedItems)
119121
this.apply()
120122
}
121123

122-
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(applicationContext)
124+
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(application)
123125

124126
fun toggleFutureItems(showFutureItems: Boolean) {
125127
toggleShowFutureItems.onNext(showFutureItems)
126128
}
127129

128130
fun saveShowFutureItemsToPreferences(showFutureItems: Boolean) =
129-
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit {
130-
this.putBoolean(applicationContext.getString(R.string.feed_show_future_items_key), showFutureItems)
131+
PreferenceManager.getDefaultSharedPreferences(application).edit {
132+
this.putBoolean(application.getString(R.string.feed_show_future_items_key), showFutureItems)
131133
this.apply()
132134
}
133135

134-
fun getShowFutureItemsFromPreferences() = getShowFutureItemsFromPreferences(applicationContext)
136+
fun getShowFutureItemsFromPreferences() = getShowFutureItemsFromPreferences(application)
135137

136138
companion object {
137139
private fun getShowPlayedItemsFromPreferences(context: Context) =
@@ -143,7 +145,7 @@ class FeedViewModel(
143145
fun getFactory(context: Context, groupId: Long) = viewModelFactory {
144146
initializer {
145147
FeedViewModel(
146-
context.applicationContext,
148+
App.getApp(),
147149
groupId,
148150
// Read initial value from preferences
149151
getShowPlayedItemsFromPreferences(context.applicationContext),

0 commit comments

Comments
 (0)