SubscriptionsVM.kt 3.05 KB
Newer Older
1
package fr.chenry.android.freshrss.store.viewmodels
2 3

import androidx.lifecycle.*
4
import fr.chenry.android.freshrss.FreshRSSApplication
5
import fr.chenry.android.freshrss.R
6
import fr.chenry.android.freshrss.store.database.models.Subscriptions
7
import org.joda.time.LocalDateTime
8 9

sealed class SubscriptionsVM: ViewModel() {
10 11 12 13 14 15 16 17 18 19 20 21 22
    abstract val subscriptionsLiveData: LiveData<Subscriptions>

    val liveData: LiveData<Subscriptions> by lazy {
        MutableLiveData<Subscriptions>().apply {
            value = load()
            subscriptionsLiveData.observeForever {value = load()}
        }
    }

    val sectionnedLiveData: LiveData<Map<String, Subscriptions>> by lazy {
        MutableLiveData<Map<String, Subscriptions>>().apply {
            liveData.observeForever {value = groupBy(it)}
        }
23 24
    }

25 26 27 28 29 30
    protected open fun groupBy(subscriptions: Subscriptions): Map<String, Subscriptions> =
        subscriptions.groupBy {it.title[0].toString().toUpperCase()}

    protected open fun load(): Subscriptions = subscriptionsLiveData.value?.sortedWith(Comparator {o1, o2 ->
        o1.title.compareTo(o2.title, true)
    }) ?: listOf()
31 32 33
}

class AllSubscriptionsVM: SubscriptionsVM() {
34
    override val subscriptionsLiveData = FreshRSSApplication.database.getAllSubcriptions().toLiveData()
35 36 37
}

class UnreadSubscriptionsVM: SubscriptionsVM() {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    override val subscriptionsLiveData = FreshRSSApplication.database.getAllUnreadSubcriptions().toLiveData()

    override fun groupBy(subscriptions: Subscriptions): Map<String, Subscriptions> = subscriptions.groupBy {
        val today = LocalDateTime.now().withTime(0, 0, 0, 0)
        when {
            it.newestArticleDate.isAfter(today.minusDays(1)) -> R.string.human_time_grouping_today
            it.newestArticleDate.isAfter(today.minusDays(2)) -> R.string.human_time_grouping_yesterday
            it.newestArticleDate.isAfter(today.withDayOfWeek(1)) -> R.string.human_time_grouping_this_week
            it.newestArticleDate.isAfter(today.withDayOfWeek(1).minusWeeks(1)) -> R.string.human_time_grouping_last_week
            it.newestArticleDate.isAfter(today.withDayOfMonth(1)) -> R.string.human_time_grouping_this_month
            it.newestArticleDate.isAfter(today.withDayOfMonth(1).minusMonths(1)) -> R.string.human_time_grouping_last_month
            it.newestArticleDate.isAfter(today.withDayOfYear(1)) -> R.string.human_time_grouping_this_year
            else -> R.string.human_time_grouping_old_articles
        }.let {id -> FreshRSSApplication.getStringR(id)}
    }

    override fun load(): Subscriptions = subscriptionsLiveData.value?.sortedWith(Comparator {o1, o2 ->
        if(o1.newestArticleDate.isEqual(o2.newestArticleDate))
            o1.title.compareTo(o2.title, true) else
            o2.newestArticleDate.compareTo(o1.newestArticleDate)
    }) ?: listOf()
59 60 61
}

class FavoritesSubscriptionsVM: SubscriptionsVM() {
62 63 64 65
    override val subscriptionsLiveData: LiveData<Subscriptions> = MutableLiveData<Subscriptions>()
        .apply {value = listOf()}

    override fun groupBy(subscriptions: Subscriptions): Map<String, Subscriptions> = mapOf()
66
}