make sure all timeline database operations run on a background thread (#3391)
This commit is contained in:
parent
b8c77a795c
commit
816dc0cbbc
2 changed files with 44 additions and 37 deletions
|
@ -3,23 +3,29 @@ package com.keylesspalace.tusky.appstore
|
|||
import com.google.gson.Gson
|
||||
import com.keylesspalace.tusky.db.AccountManager
|
||||
import com.keylesspalace.tusky.db.AppDatabase
|
||||
import io.reactivex.rxjava3.disposables.Disposable
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.rx3.asFlow
|
||||
import javax.inject.Inject
|
||||
|
||||
class CacheUpdater @Inject constructor(
|
||||
eventHub: EventHub,
|
||||
private val accountManager: AccountManager,
|
||||
accountManager: AccountManager,
|
||||
appDatabase: AppDatabase,
|
||||
gson: Gson
|
||||
) {
|
||||
|
||||
private val disposable: Disposable
|
||||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
init {
|
||||
val timelineDao = appDatabase.timelineDao()
|
||||
|
||||
disposable = eventHub.events.subscribe { event ->
|
||||
val accountId = accountManager.activeAccount?.id ?: return@subscribe
|
||||
scope.launch {
|
||||
eventHub.events.asFlow().collect { event ->
|
||||
val accountId = accountManager.activeAccount?.id ?: return@collect
|
||||
when (event) {
|
||||
is FavoriteEvent ->
|
||||
timelineDao.setFavourited(accountId, event.statusId, event.favourite)
|
||||
|
@ -40,8 +46,9 @@ class CacheUpdater @Inject constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
this.disposable.dispose()
|
||||
this.scope.cancel()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,25 +89,25 @@ AND
|
|||
"""UPDATE TimelineStatusEntity SET favourited = :favourited
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setFavourited(accountId: Long, statusId: String, favourited: Boolean)
|
||||
abstract suspend fun setFavourited(accountId: Long, statusId: String, favourited: Boolean)
|
||||
|
||||
@Query(
|
||||
"""UPDATE TimelineStatusEntity SET bookmarked = :bookmarked
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setBookmarked(accountId: Long, statusId: String, bookmarked: Boolean)
|
||||
abstract suspend fun setBookmarked(accountId: Long, statusId: String, bookmarked: Boolean)
|
||||
|
||||
@Query(
|
||||
"""UPDATE TimelineStatusEntity SET reblogged = :reblogged
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setReblogged(accountId: Long, statusId: String, reblogged: Boolean)
|
||||
abstract suspend fun setReblogged(accountId: Long, statusId: String, reblogged: Boolean)
|
||||
|
||||
@Query(
|
||||
"""DELETE FROM TimelineStatusEntity WHERE timelineUserId = :accountId AND
|
||||
(authorServerId = :userId OR reblogAccountId = :userId)"""
|
||||
)
|
||||
abstract fun removeAllByUser(accountId: Long, userId: String)
|
||||
abstract suspend fun removeAllByUser(accountId: Long, userId: String)
|
||||
|
||||
/**
|
||||
* Removes everything in the TimelineStatusEntity and TimelineAccountEntity tables for one user account
|
||||
|
@ -128,7 +128,7 @@ WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId =
|
|||
"""DELETE FROM TimelineStatusEntity WHERE timelineUserId = :accountId
|
||||
AND serverId = :statusId"""
|
||||
)
|
||||
abstract fun delete(accountId: Long, statusId: String)
|
||||
abstract suspend fun delete(accountId: Long, statusId: String)
|
||||
|
||||
/**
|
||||
* Cleans the TimelineStatusEntity and TimelineAccountEntity tables from old entries.
|
||||
|
@ -168,31 +168,31 @@ AND serverId = :statusId"""
|
|||
"""UPDATE TimelineStatusEntity SET poll = :poll
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setVoted(accountId: Long, statusId: String, poll: String)
|
||||
abstract suspend fun setVoted(accountId: Long, statusId: String, poll: String)
|
||||
|
||||
@Query(
|
||||
"""UPDATE TimelineStatusEntity SET expanded = :expanded
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setExpanded(accountId: Long, statusId: String, expanded: Boolean)
|
||||
abstract suspend fun setExpanded(accountId: Long, statusId: String, expanded: Boolean)
|
||||
|
||||
@Query(
|
||||
"""UPDATE TimelineStatusEntity SET contentShowing = :contentShowing
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setContentShowing(accountId: Long, statusId: String, contentShowing: Boolean)
|
||||
abstract suspend fun setContentShowing(accountId: Long, statusId: String, contentShowing: Boolean)
|
||||
|
||||
@Query(
|
||||
"""UPDATE TimelineStatusEntity SET contentCollapsed = :contentCollapsed
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setContentCollapsed(accountId: Long, statusId: String, contentCollapsed: Boolean)
|
||||
abstract suspend fun setContentCollapsed(accountId: Long, statusId: String, contentCollapsed: Boolean)
|
||||
|
||||
@Query(
|
||||
"""UPDATE TimelineStatusEntity SET pinned = :pinned
|
||||
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)"""
|
||||
)
|
||||
abstract fun setPinned(accountId: Long, statusId: String, pinned: Boolean)
|
||||
abstract suspend fun setPinned(accountId: Long, statusId: String, pinned: Boolean)
|
||||
|
||||
@Query(
|
||||
"""DELETE FROM TimelineStatusEntity
|
||||
|
|
Loading…
Reference in a new issue