FreshRSSDabatabase.kt 5.66 KB
Newer Older
Christophe Henry's avatar
Christophe Henry committed
1
package fr.chenry.android.freshrss.store.database
2

3
import android.graphics.Bitmap
Christophe Henry's avatar
Christophe Henry committed
4
import androidx.room.*
5
import dev.matrix.roomigrant.GenerateRoomMigrations
Christophe Henry's avatar
Christophe Henry committed
6
import fr.chenry.android.freshrss.store.database.models.*
7
import io.reactivex.Flowable
Christophe Henry's avatar
Christophe Henry committed
8
import kotlinx.coroutines.*
9
import org.joda.time.DateTime
10
import org.joda.time.LocalDateTime
11

12 13 14 15 16 17 18
@Database(
    version = 8,
    entities = [
        Account::class, Article::class, Subscription::class,
        FeedTag::class, FeedToTagRelation::class
    ]
)
Christophe Henry's avatar
Christophe Henry committed
19
@TypeConverters(Converters::class)
20
@GenerateRoomMigrations(ManualMigrations::class)
21 22 23
abstract class FreshRSSDabatabase: RoomDatabase() {


Christophe Henry's avatar
Christophe Henry committed
24
    protected abstract fun getAccountsDAO(): AccountsDAO
Christophe Henry's avatar
Christophe Henry committed
25
    protected abstract fun getArticlesDAO(): ArticlesDAO
26
    protected abstract fun getSubscriptionsDAO(): SubscriptionsDAO
27
    protected abstract fun getSubscriptionCategoriesDAO(): SubscriptionCategoriesDAO
28
    protected abstract fun getFeedToTagRelationDAO(): FeedToTagRelationDAO
Christophe Henry's avatar
Christophe Henry committed
29

30
    // Accounts
31
    fun getAccount() = getAccountsDAO().getAccount()
32

33
    suspend fun insertAccount(account: Account) = getAccountsDAO().insert(account)
34 35
    suspend fun updateLastFetchDate(id: Int, date: DateTime) =
        getAccountsDAO().updateLastFetchDate(AccountLastFetchDateUpdate(id, date))
Christophe Henry's avatar
Christophe Henry committed
36 37 38
    fun updateTotalUnreadCount(id: Int, unreadCount: Int) = getAccountsDAO().updateUnreadCount(id, unreadCount)
    fun incrementTotalUnreadCount(id: Int) = getAccountsDAO().incrementUnreadCount(id)
    fun decrementTotalUnreadCount(id: Int) = getAccountsDAO().decrementUnreadCount(id)
Christophe Henry's avatar
Christophe Henry committed
39

40
    // Articles
41 42
    fun getArticlesByStreamId(streamId: String) = getArticlesDAO().getByStreamId(streamId)

43
    fun getArticleById(id: String): Flowable<Article> = getArticlesDAO().getById(id)
44 45
    fun getArticleByStreamIdAndUnread(streamId: String) = getArticlesDAO().getByStreamIdAndUnread(streamId)
    fun getArticleByStreamIdAndFavorite(streamId: String) = getArticlesDAO().getByStreamIdAndFavorite(streamId)
46

47 48 49 50 51 52
    suspend fun insertArticles(articles: Articles) = getArticlesDAO().insertAll(articles)
    suspend fun updateArticleReadStatuses(readStatuses: List<ArticleReadStatusUpdate>) =
        getArticlesDAO().updateReadStatuses(readStatuses)

    suspend fun updateArticleFavoriteStatuses(favoriteStatuses: List<ArticleFavoriteStatusUpdate>) =
        getArticlesDAO().updateFavoriteStatuses(favoriteStatuses)
53

54 55 56
    suspend fun updateArticleScrollPosition(id: String, scrollPosition: Int) =
        getArticlesDAO().updateScrollPosition(ArticleScrollPositionUpdate(id, scrollPosition))

57 58
    fun getArticlesMostRecentPubDate() = getArticlesDAO().mostRecentPubDate()

59 60
    suspend fun syncAllArticlesReadStatus(ids: List<String>) = getArticlesDAO().syncAllReadStatus(ids)
    suspend fun syncAllArticlesFavoriteStatus(ids: List<String>) = getArticlesDAO().syncAllFavoriteStatus(ids)
61

62
    // Subscriptions
Christophe Henry's avatar
Christophe Henry committed
63 64
    suspend fun syncSubscriptions(subscriptions: Map<String, Subscription>) = withContext(Dispatchers.IO) {
        val suppressTask = async {
65 66
            getSubscriptionsDAO().deleteAllAbsentsById(subscriptions.keys)
        }
Christophe Henry's avatar
Christophe Henry committed
67 68 69 70 71 72 73

        val idsInDB = getAllSubcriptionsIds()
        val grouped = subscriptions.values.groupBy {it.id in idsInDB}

        val updateTask = async {
            val toUpdate = grouped[true]?.map {it.toSubscriptionUpdate()} ?: listOf()
            getSubscriptionsDAO().updateValues(toUpdate)
74
        }
Christophe Henry's avatar
Christophe Henry committed
75 76 77

        val insertTask = async {
            val toInsert = grouped[false] ?: listOf()
78 79 80
            getSubscriptionsDAO().insertAll(toInsert)
        }

Christophe Henry's avatar
Christophe Henry committed
81 82
        listOf(suppressTask, updateTask, insertTask).joinAll()
    }
83

84 85
    suspend fun updateSubscriptionCount(id: String, count: Int) = getSubscriptionsDAO().updateCount(id, count)
    suspend fun updateSubscriptionNewestArticleDate(id: String, newestArticleDate: LocalDateTime) =
86
        getSubscriptionsDAO().updateNewestArticleDate(id, newestArticleDate)
Christophe Henry's avatar
Christophe Henry committed
87

88 89
    fun incrementSubscriptionCount(id: String) = getSubscriptionsDAO().incrementCount(id)
    fun decrementSubscriptionCount(id: String) = getSubscriptionsDAO().decrementCount(id)
90
    fun insertSubscriptionImage(id: String, bitmap: Bitmap) = getSubscriptionsDAO().insertImage(id, bitmap)
91
    fun getSubcriptionsById(id: String) = getSubscriptionsDAO().byId(id)
92
    fun getSubcriptionTitleById(id: String) = getSubscriptionsDAO().titleById(id)
93 94
    fun getSubcriptionsBySubscriptionCategory(feedTag: FeedTag) =
        getSubscriptionsDAO().bySubscriptionCategory(feedTag.id)
95

96 97
    fun getSubcriptionsBySubscriptionCategoryAndUnreadCount(feedTag: FeedTag) =
        getSubscriptionsDAO().bySubscriptionCategoryAndUnreadCount(feedTag.id)
98

99 100
    fun getSubcriptionsBySubscriptionCategoryAndFavorites(feedTag: FeedTag) =
        getSubscriptionsDAO().bySubscriptionCategoryAndFavorites(feedTag.id)
101

102
    fun getAllSubcriptions() = getSubscriptionsDAO().getAll()
103
    fun getAllUnreadSubcriptions() = getSubscriptionsDAO().getAllUnread()
104
    fun getAllSubscriptionsWithFavorites() = getSubscriptionsDAO().getAllWithFavorites()
105

Christophe Henry's avatar
Christophe Henry committed
106
    suspend fun getAllSubcriptionsIds() = getSubscriptionsDAO().getAllIds()
107
    suspend fun getAllSubcriptionsWithImageToUpdate() = getSubscriptionsDAO().withImageToUpdate()
108 109

    // Subscription catagoeries
110 111 112 113 114 115
    suspend fun syncAllSubscriptionCategories(feedTags: FeedTags) =
        getSubscriptionCategoriesDAO().syncAll(feedTags)

    fun getAllFolders() = getSubscriptionCategoriesDAO().allFolders()
    fun getFoldersWithUnreadItems() = getSubscriptionCategoriesDAO().foldersWithUnreadItems()
    fun getFoldersWithFavorites() = getSubscriptionCategoriesDAO().foldersWithFavorites()
116

117 118 119
    // Feed tag relation
    suspend fun syncFeedToTagRelationForFeed(feedId: String, tagIds: List<String>) =
        getFeedToTagRelationDAO().syncForFeed(feedId, tagIds)
120
}