2019-04-28 22:43:54 +02:00
|
|
|
package org.schabi.newpipe.local.subscription.dialog
|
|
|
|
|
|
|
|
import android.content.Context
|
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
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2020-03-05 23:37:00 +01:00
|
|
|
import io.reactivex.Completable
|
2019-04-28 22:43:54 +02:00
|
|
|
import io.reactivex.Flowable
|
2020-03-05 23:37:00 +01:00
|
|
|
import io.reactivex.disposables.Disposable
|
2019-04-28 22:43:54 +02:00
|
|
|
import io.reactivex.functions.BiFunction
|
|
|
|
import io.reactivex.schedulers.Schedulers
|
|
|
|
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
|
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionEntity
|
|
|
|
import org.schabi.newpipe.local.feed.FeedDatabaseManager
|
|
|
|
import org.schabi.newpipe.local.subscription.FeedGroupIcon
|
|
|
|
import org.schabi.newpipe.local.subscription.SubscriptionManager
|
|
|
|
|
2020-01-28 06:59:49 +01:00
|
|
|
class FeedGroupDialogViewModel(applicationContext: Context, val groupId: Long = FeedGroupEntity.GROUP_ALL_ID) : ViewModel() {
|
|
|
|
class Factory(val context: Context, val groupId: Long = FeedGroupEntity.GROUP_ALL_ID) : ViewModelProvider.Factory {
|
2019-04-28 22:43:54 +02:00
|
|
|
@Suppress("UNCHECKED_CAST")
|
|
|
|
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
|
|
|
return FeedGroupDialogViewModel(context.applicationContext, groupId) as T
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
|
|
|
|
private var subscriptionManager = SubscriptionManager(applicationContext)
|
|
|
|
|
2020-03-05 23:47:00 +01:00
|
|
|
private val mutableGroupLiveData = MutableLiveData<FeedGroupEntity>()
|
|
|
|
private val mutableSubscriptionsLiveData = MutableLiveData<Pair<List<SubscriptionEntity>, Set<Long>>>()
|
|
|
|
private val mutableDialogEventLiveData = MutableLiveData<DialogEvent>()
|
|
|
|
val groupLiveData: LiveData<FeedGroupEntity> = mutableGroupLiveData
|
|
|
|
val subscriptionsLiveData: LiveData<Pair<List<SubscriptionEntity>, Set<Long>>> = mutableSubscriptionsLiveData
|
|
|
|
val dialogEventLiveData: LiveData<DialogEvent> = mutableDialogEventLiveData
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-03-05 23:37:00 +01:00
|
|
|
private var actionProcessingDisposable: Disposable? = null
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
private var feedGroupDisposable = feedDatabaseManager.getGroup(groupId)
|
|
|
|
.subscribeOn(Schedulers.io())
|
2020-03-05 23:47:00 +01:00
|
|
|
.subscribe(mutableGroupLiveData::postValue)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
private var subscriptionsDisposable = Flowable
|
|
|
|
.combineLatest(subscriptionManager.subscriptions(), feedDatabaseManager.subscriptionIdsForGroup(groupId),
|
|
|
|
BiFunction { t1: List<SubscriptionEntity>, t2: List<Long> -> t1 to t2.toSet() })
|
|
|
|
.subscribeOn(Schedulers.io())
|
2020-03-05 23:47:00 +01:00
|
|
|
.subscribe(mutableSubscriptionsLiveData::postValue)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
override fun onCleared() {
|
|
|
|
super.onCleared()
|
2020-03-05 23:37:00 +01:00
|
|
|
actionProcessingDisposable?.dispose()
|
2019-04-28 22:43:54 +02:00
|
|
|
subscriptionsDisposable.dispose()
|
|
|
|
feedGroupDisposable.dispose()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun createGroup(name: String, selectedIcon: FeedGroupIcon, selectedSubscriptions: Set<Long>) {
|
2020-03-05 23:37:00 +01:00
|
|
|
doAction(feedDatabaseManager.createGroup(name, selectedIcon)
|
|
|
|
.flatMapCompletable {
|
|
|
|
feedDatabaseManager.updateSubscriptionsForGroup(it, selectedSubscriptions.toList())
|
|
|
|
})
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 03:01:23 +01:00
|
|
|
fun updateGroup(name: String, selectedIcon: FeedGroupIcon, selectedSubscriptions: Set<Long>, sortOrder: Long) {
|
2020-03-05 23:37:00 +01:00
|
|
|
doAction(feedDatabaseManager.updateSubscriptionsForGroup(groupId, selectedSubscriptions.toList())
|
|
|
|
.andThen(feedDatabaseManager.updateGroup(FeedGroupEntity(groupId, name, selectedIcon, sortOrder))))
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteGroup() {
|
2020-03-05 23:37:00 +01:00
|
|
|
doAction(feedDatabaseManager.deleteGroup(groupId))
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 23:37:00 +01:00
|
|
|
private fun doAction(completable: Completable) {
|
|
|
|
if (actionProcessingDisposable == null) {
|
2020-03-05 23:47:00 +01:00
|
|
|
mutableDialogEventLiveData.value = DialogEvent.ProcessingEvent
|
2020-03-05 23:37:00 +01:00
|
|
|
|
|
|
|
actionProcessingDisposable = completable
|
|
|
|
.subscribeOn(Schedulers.io())
|
2020-03-05 23:47:00 +01:00
|
|
|
.subscribe { mutableDialogEventLiveData.postValue(DialogEvent.SuccessEvent) }
|
2020-03-05 23:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class DialogEvent {
|
|
|
|
object ProcessingEvent : DialogEvent()
|
|
|
|
object SuccessEvent : DialogEvent()
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
2020-05-01 20:13:21 +02:00
|
|
|
}
|