SubscriptionArticleVM.kt 933 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package fr.chenry.android.freshrss.store.viewmodels

import androidx.lifecycle.*
import fr.chenry.android.freshrss.store.database.FreshRSSDabatabase
import fr.chenry.android.freshrss.store.database.models.*

class SubscriptionArticleVM(private val articleId: ItemId): ViewModel() {
    private val flowable by lazy { FreshRSSDabatabase.instance.getArticleById(articleId) }
    private val source: LiveData<Articles> by lazy { flowable.toLiveData() }
    val liveData: LiveData<Article> by lazy {
        MutableLiveData<Article>().apply {
            value = flowable.blockingFirst().first()
            source.observeForever { value = it.first() }
        }
    }
}

class SubscriptionArticleVMF(private val articleId: ItemId): ViewModelProvider.NewInstanceFactory() {
    override fun <T: ViewModel?> create(modelClass: Class<T>): T {
        @Suppress("UNCHECKED_CAST")
        return SubscriptionArticleVM(articleId) as T
    }
}