2019-04-28 22:43:54 +02:00
|
|
|
package org.schabi.newpipe.local.feed
|
|
|
|
|
2022-07-31 05:05:50 +02:00
|
|
|
import android.app.Application
|
2019-04-28 22:43:54 +02:00
|
|
|
import android.content.Context
|
2021-11-08 05:11:10 +01:00
|
|
|
import androidx.core.content.edit
|
2020-03-05 23:47:00 +01:00
|
|
|
import androidx.lifecycle.LiveData
|
2019-04-28 22:43:54 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
2022-07-26 17:31:14 +02:00
|
|
|
import androidx.lifecycle.viewmodel.initializer
|
|
|
|
import androidx.lifecycle.viewmodel.viewModelFactory
|
2021-11-08 05:11:10 +01:00
|
|
|
import androidx.preference.PreferenceManager
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
|
|
|
import io.reactivex.rxjava3.core.Flowable
|
2022-06-24 18:03:48 +02:00
|
|
|
import io.reactivex.rxjava3.functions.Function5
|
2020-04-05 16:11:03 +02:00
|
|
|
import io.reactivex.rxjava3.processors.BehaviorProcessor
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
2022-07-31 05:05:50 +02:00
|
|
|
import org.schabi.newpipe.App
|
2021-11-10 22:16:17 +01:00
|
|
|
import org.schabi.newpipe.R
|
2020-01-28 06:59:49 +01:00
|
|
|
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
|
2020-04-05 16:11:03 +02:00
|
|
|
import org.schabi.newpipe.database.stream.StreamWithState
|
|
|
|
import org.schabi.newpipe.local.feed.item.StreamItem
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.local.feed.service.FeedEventManager
|
2020-11-04 18:07:40 +01:00
|
|
|
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent
|
|
|
|
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.IdleEvent
|
|
|
|
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ProgressEvent
|
|
|
|
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.SuccessResultEvent
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT
|
2020-10-31 21:55:45 +01:00
|
|
|
import java.time.OffsetDateTime
|
|
|
|
import java.util.concurrent.TimeUnit
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
class FeedViewModel(
|
2022-07-31 05:05:50 +02:00
|
|
|
private val application: Application,
|
2020-04-05 16:11:03 +02:00
|
|
|
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
|
2022-06-24 18:03:48 +02:00
|
|
|
initialShowPlayedItems: Boolean = true,
|
|
|
|
initialShowFutureItems: Boolean = true
|
2020-04-05 16:11:03 +02:00
|
|
|
) : ViewModel() {
|
2022-07-31 05:05:50 +02:00
|
|
|
private val feedDatabaseManager = FeedDatabaseManager(application)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
private val toggleShowPlayedItems = BehaviorProcessor.create<Boolean>()
|
2021-09-03 21:35:23 +02:00
|
|
|
private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
|
2020-04-05 16:11:03 +02:00
|
|
|
.startWithItem(initialShowPlayedItems)
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
2022-06-24 18:03:48 +02:00
|
|
|
private val toggleShowFutureItems = BehaviorProcessor.create<Boolean>()
|
|
|
|
private val toggleShowFutureItemsFlowable = toggleShowFutureItems
|
|
|
|
.startWithItem(initialShowFutureItems)
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
2020-03-05 23:47:00 +01:00
|
|
|
private val mutableStateLiveData = MutableLiveData<FeedState>()
|
|
|
|
val stateLiveData: LiveData<FeedState> = mutableStateLiveData
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
private var combineDisposable = Flowable
|
2020-10-31 21:55:45 +01:00
|
|
|
.combineLatest(
|
|
|
|
FeedEventManager.events(),
|
2021-09-03 22:03:34 +02:00
|
|
|
toggleShowPlayedItemsFlowable,
|
2022-06-24 18:03:48 +02:00
|
|
|
toggleShowFutureItemsFlowable,
|
2020-10-31 21:55:45 +01:00
|
|
|
feedDatabaseManager.notLoadedCount(groupId),
|
|
|
|
feedDatabaseManager.oldestSubscriptionUpdate(groupId),
|
2020-04-05 16:11:03 +02:00
|
|
|
|
2022-06-24 18:03:48 +02:00
|
|
|
Function5 { t1: FeedEventManager.Event, t2: Boolean, t3: Boolean,
|
|
|
|
t4: Long, t5: List<OffsetDateTime> ->
|
|
|
|
return@Function5 CombineResultEventHolder(t1, t2, t3, t4, t5.firstOrNull())
|
2020-11-04 18:07:40 +01:00
|
|
|
}
|
2020-10-31 21:55:45 +01:00
|
|
|
)
|
|
|
|
.throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS)
|
|
|
|
.subscribeOn(Schedulers.io())
|
2021-09-03 21:35:23 +02:00
|
|
|
.observeOn(Schedulers.io())
|
2022-06-24 18:03:48 +02:00
|
|
|
.map { (event, showPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
|
2022-03-18 18:15:44 +01:00
|
|
|
val streamItems = if (event is SuccessResultEvent || event is IdleEvent)
|
2021-09-03 21:35:23 +02:00
|
|
|
feedDatabaseManager
|
2022-07-13 11:37:46 +02:00
|
|
|
.getStreams(groupId, showPlayedItems, showFutureItems)
|
2021-09-03 22:03:34 +02:00
|
|
|
.blockingGet(arrayListOf())
|
2021-09-03 21:35:23 +02:00
|
|
|
else
|
|
|
|
arrayListOf()
|
|
|
|
|
|
|
|
CombineResultDataHolder(event, streamItems, notLoadedCount, oldestUpdate)
|
|
|
|
}
|
2020-10-31 21:55:45 +01:00
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe { (event, listFromDB, notLoadedCount, oldestUpdate) ->
|
|
|
|
mutableStateLiveData.postValue(
|
|
|
|
when (event) {
|
2020-04-05 16:11:03 +02:00
|
|
|
is IdleEvent -> FeedState.LoadedState(listFromDB.map { e -> StreamItem(e) }, oldestUpdate, notLoadedCount)
|
2019-12-16 08:36:04 +01:00
|
|
|
is ProgressEvent -> FeedState.ProgressState(event.currentProgress, event.maxProgress, event.progressMessage)
|
2020-04-05 16:11:03 +02:00
|
|
|
is SuccessResultEvent -> FeedState.LoadedState(listFromDB.map { e -> StreamItem(e) }, oldestUpdate, notLoadedCount, event.itemsErrors)
|
2019-12-16 08:36:04 +01:00
|
|
|
is ErrorResultEvent -> FeedState.ErrorState(event.error)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
2020-10-31 21:55:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if (event is ErrorResultEvent || event is SuccessResultEvent) {
|
|
|
|
FeedEventManager.reset()
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
2020-10-31 21:55:45 +01:00
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
override fun onCleared() {
|
|
|
|
super.onCleared()
|
|
|
|
combineDisposable.dispose()
|
|
|
|
}
|
2019-12-16 08:36:04 +01:00
|
|
|
|
2021-09-03 21:35:23 +02:00
|
|
|
private data class CombineResultEventHolder(
|
2021-09-03 22:03:34 +02:00
|
|
|
val t1: FeedEventManager.Event,
|
|
|
|
val t2: Boolean,
|
2022-06-24 18:03:48 +02:00
|
|
|
val t3: Boolean,
|
|
|
|
val t4: Long,
|
|
|
|
val t5: OffsetDateTime?
|
2021-09-03 22:03:34 +02:00
|
|
|
)
|
2021-09-03 21:35:23 +02:00
|
|
|
|
|
|
|
private data class CombineResultDataHolder(
|
2021-09-03 22:03:34 +02:00
|
|
|
val t1: FeedEventManager.Event,
|
|
|
|
val t2: List<StreamWithState>,
|
|
|
|
val t3: Long,
|
|
|
|
val t4: OffsetDateTime?
|
|
|
|
)
|
2020-04-05 16:11:03 +02:00
|
|
|
|
|
|
|
fun togglePlayedItems(showPlayedItems: Boolean) {
|
|
|
|
toggleShowPlayedItems.onNext(showPlayedItems)
|
|
|
|
}
|
|
|
|
|
2021-11-11 19:46:15 +01:00
|
|
|
fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
|
2022-07-31 05:05:50 +02:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(application).edit {
|
|
|
|
this.putBoolean(application.getString(R.string.feed_show_played_items_key), showPlayedItems)
|
2021-11-11 19:46:15 +01:00
|
|
|
this.apply()
|
|
|
|
}
|
2021-11-08 05:11:10 +01:00
|
|
|
|
2022-07-31 05:05:50 +02:00
|
|
|
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(application)
|
2021-11-11 19:46:15 +01:00
|
|
|
|
2022-06-24 18:03:48 +02:00
|
|
|
fun toggleFutureItems(showFutureItems: Boolean) {
|
|
|
|
toggleShowFutureItems.onNext(showFutureItems)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun saveShowFutureItemsToPreferences(showFutureItems: Boolean) =
|
2022-07-31 05:05:50 +02:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(application).edit {
|
|
|
|
this.putBoolean(application.getString(R.string.feed_show_future_items_key), showFutureItems)
|
2022-06-24 18:03:48 +02:00
|
|
|
this.apply()
|
|
|
|
}
|
|
|
|
|
2022-07-31 05:05:50 +02:00
|
|
|
fun getShowFutureItemsFromPreferences() = getShowFutureItemsFromPreferences(application)
|
2022-06-24 18:03:48 +02:00
|
|
|
|
2021-11-11 19:46:15 +01:00
|
|
|
companion object {
|
|
|
|
private fun getShowPlayedItemsFromPreferences(context: Context) =
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getBoolean(context.getString(R.string.feed_show_played_items_key), true)
|
2022-06-24 18:03:48 +02:00
|
|
|
private fun getShowFutureItemsFromPreferences(context: Context) =
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getBoolean(context.getString(R.string.feed_show_future_items_key), true)
|
2022-07-26 17:31:14 +02:00
|
|
|
fun getFactory(context: Context, groupId: Long) = viewModelFactory {
|
|
|
|
initializer {
|
|
|
|
FeedViewModel(
|
2022-07-31 05:05:50 +02:00
|
|
|
App.getApp(),
|
2022-07-26 17:31:14 +02:00
|
|
|
groupId,
|
|
|
|
// Read initial value from preferences
|
|
|
|
getShowPlayedItemsFromPreferences(context.applicationContext),
|
|
|
|
getShowFutureItemsFromPreferences(context.applicationContext)
|
|
|
|
)
|
|
|
|
}
|
2020-04-05 16:11:03 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 20:13:01 +02:00
|
|
|
}
|