Use Application instead of Context in FeedViewModel.

This commit is contained in:
Isira Seneviratne 2022-07-31 08:35:50 +05:30
parent 404c13d4c1
commit 311d392386

View file

@ -1,5 +1,6 @@
package org.schabi.newpipe.local.feed package org.schabi.newpipe.local.feed
import android.app.Application
import android.content.Context import android.content.Context
import androidx.core.content.edit import androidx.core.content.edit
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
@ -13,6 +14,7 @@ import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.functions.Function5 import io.reactivex.rxjava3.functions.Function5
import io.reactivex.rxjava3.processors.BehaviorProcessor import io.reactivex.rxjava3.processors.BehaviorProcessor
import io.reactivex.rxjava3.schedulers.Schedulers import io.reactivex.rxjava3.schedulers.Schedulers
import org.schabi.newpipe.App
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.database.feed.model.FeedGroupEntity
import org.schabi.newpipe.database.stream.StreamWithState import org.schabi.newpipe.database.stream.StreamWithState
@ -27,12 +29,12 @@ import java.time.OffsetDateTime
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
class FeedViewModel( class FeedViewModel(
private val applicationContext: Context, private val application: Application,
groupId: Long = FeedGroupEntity.GROUP_ALL_ID, groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
initialShowPlayedItems: Boolean = true, initialShowPlayedItems: Boolean = true,
initialShowFutureItems: Boolean = true initialShowFutureItems: Boolean = true
) : ViewModel() { ) : ViewModel() {
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext) private val feedDatabaseManager = FeedDatabaseManager(application)
private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>() private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>()
private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
@ -114,24 +116,24 @@ class FeedViewModel(
} }
fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) = fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit { PreferenceManager.getDefaultSharedPreferences(application).edit {
this.putBoolean(applicationContext.getString(R.string.feed_show_played_items_key), showPlayedItems) this.putBoolean(application.getString(R.string.feed_show_played_items_key), showPlayedItems)
this.apply() this.apply()
} }
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(applicationContext) fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(application)
fun toggleFutureItems(showFutureItems: Boolean) { fun toggleFutureItems(showFutureItems: Boolean) {
toggleShowFutureItems.onNext(showFutureItems) toggleShowFutureItems.onNext(showFutureItems)
} }
fun saveShowFutureItemsToPreferences(showFutureItems: Boolean) = fun saveShowFutureItemsToPreferences(showFutureItems: Boolean) =
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit { PreferenceManager.getDefaultSharedPreferences(application).edit {
this.putBoolean(applicationContext.getString(R.string.feed_show_future_items_key), showFutureItems) this.putBoolean(application.getString(R.string.feed_show_future_items_key), showFutureItems)
this.apply() this.apply()
} }
fun getShowFutureItemsFromPreferences() = getShowFutureItemsFromPreferences(applicationContext) fun getShowFutureItemsFromPreferences() = getShowFutureItemsFromPreferences(application)
companion object { companion object {
private fun getShowPlayedItemsFromPreferences(context: Context) = private fun getShowPlayedItemsFromPreferences(context: Context) =
@ -143,7 +145,7 @@ class FeedViewModel(
fun getFactory(context: Context, groupId: Long) = viewModelFactory { fun getFactory(context: Context, groupId: Long) = viewModelFactory {
initializer { initializer {
FeedViewModel( FeedViewModel(
context.applicationContext, App.getApp(),
groupId, groupId,
// Read initial value from preferences // Read initial value from preferences
getShowPlayedItemsFromPreferences(context.applicationContext), getShowPlayedItemsFromPreferences(context.applicationContext),