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

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

11
@Database(
12
    version = 10,
13
    entities = [
14 15
        Account::class, Article::class, UnreadArticleId::class,
        FavoriteArticleId::class, Subscription::class,
16 17 18
        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
Christophe Henry's avatar
Christophe Henry committed
31
    fun getAccount() = getAccountsDAO().getAccount()
32

Christophe Henry's avatar
Christophe Henry committed
33
    suspend fun insertAccount(account: Account) = getAccountsDAO().insert(account)
34 35
    suspend fun updateLastFetchDate(id: Int, date: DateTime) =
        getAccountsDAO().updateLastFetchDate(AccountLastFetchDateUpdate(id, date))
36

Christophe Henry's avatar
Christophe Henry committed
37 38 39
    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
40

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

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

48 49 50 51 52 53
    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)
54

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

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

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

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

        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)
75
        }
Christophe Henry's avatar
Christophe Henry committed
76 77 78

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

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

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

89 90
    fun incrementSubscriptionCount(id: String) = getSubscriptionsDAO().incrementCount(id)
    fun decrementSubscriptionCount(id: String) = getSubscriptionsDAO().decrementCount(id)
91 92
    fun insertSubscriptionImages(imageUpdate: SubscriptionImageUpdate) =
        getSubscriptionsDAO().insertImages(imageUpdate)
93

Christophe Henry's avatar
Christophe Henry committed
94
    fun getSubcriptionsById(id: String) = getSubscriptionsDAO().byId(id)
95
    fun getSubcriptionTitleById(id: String) = getSubscriptionsDAO().titleById(id)
96 97
    fun getSubcriptionsBySubscriptionCategory(feedTag: FeedTag) =
        getSubscriptionsDAO().bySubscriptionCategory(feedTag.id)
98

99 100
    fun getSubcriptionsBySubscriptionCategoryAndUnreadCount(feedTag: FeedTag) =
        getSubscriptionsDAO().bySubscriptionCategoryAndUnreadCount(feedTag.id)
101

102 103
    fun getSubcriptionsBySubscriptionCategoryAndFavorites(feedTag: FeedTag) =
        getSubscriptionsDAO().bySubscriptionCategoryAndFavorites(feedTag.id)
104

105
    fun getAllSubcriptions() = getSubscriptionsDAO().getAll()
106
    fun getAllUnreadSubcriptions() = getSubscriptionsDAO().getAllUnread()
107
    fun getAllSubscriptionsWithFavorites() = getSubscriptionsDAO().getAllWithFavorites()
108

Christophe Henry's avatar
Christophe Henry committed
109
    suspend fun getAllSubcriptionsIds() = getSubscriptionsDAO().getAllIds()
110
    suspend fun getAllSubcriptionsWithImageToUpdate() = getSubscriptionsDAO().withImageToUpdate()
111 112

    // Subscription catagoeries
113 114 115 116 117 118
    suspend fun syncAllSubscriptionCategories(feedTags: FeedTags) =
        getSubscriptionCategoriesDAO().syncAll(feedTags)

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

120 121 122
    // Feed tag relation
    suspend fun syncFeedToTagRelationForFeed(feedId: String, tagIds: List<String>) =
        getFeedToTagRelationDAO().syncForFeed(feedId, tagIds)
Christophe Henry's avatar
Christophe Henry committed
123
}