TransactionsDao & FeedsDao: make some function suspend
authorDa Risk <da_risk@geekorum.com>
Tue, 11 Dec 2018 14:39:26 -0800
changeset 35 d2f081b5b95f
parent 34 ff311c97b464
child 36 5c1712a5cc1c
TransactionsDao & FeedsDao: make some function suspend
app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesRepository.kt
app/src/main/java/com/geekorum/ttrss/articles_list/FeedsRepository.kt
app/src/main/java/com/geekorum/ttrss/data/FeedsDao.kt
app/src/main/java/com/geekorum/ttrss/data/TransactionsDao.kt
--- a/app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesRepository.kt	Tue Dec 11 14:38:14 2018 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesRepository.kt	Tue Dec 11 14:39:26 2018 -0800
@@ -30,6 +30,7 @@
 import com.geekorum.ttrss.network.ApiService
 import com.geekorum.ttrss.providers.ArticlesContract
 import com.geekorum.ttrss.session.Action
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.cancelAndJoin
@@ -82,7 +83,7 @@
         return setUnreadAction
     }
 
-    private fun saveTransaction(articleId: Long, field: ArticlesContract.Transaction.Field, value: Boolean) {
+    private suspend fun saveTransaction(articleId: Long, field: ArticlesContract.Transaction.Field, value: Boolean) {
         val transaction = Transaction(articleId = articleId,
             field = field.toString(),
             value = value)
@@ -111,13 +112,15 @@
     private var executionJob: Job? = null
 
     override fun execute() {
-        executionJob = GlobalScope.launch{
+        executionJob = GlobalScope.launch(Dispatchers.IO) {
             updateArticleField(newValue)
         }
     }
 
     override fun undo() {
-        GlobalScope.launch{
+        GlobalScope.launch(Dispatchers.IO) {
+            // wait for the request to be cancelled and done
+            // to be sure that the new one happens after
             executionJob?.cancelAndJoin()
             updateArticleField(!newValue)
         }
@@ -131,7 +134,7 @@
         }
     }
 
-    private fun saveTransaction(articleId: Long, field: ArticlesContract.Transaction.Field, value: Boolean) {
+    private suspend fun saveTransaction(articleId: Long, field: ArticlesContract.Transaction.Field, value: Boolean) {
         val transaction = Transaction(articleId = articleId,
             field = field.toString(),
             value = value)
--- a/app/src/main/java/com/geekorum/ttrss/articles_list/FeedsRepository.kt	Tue Dec 11 14:38:14 2018 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/articles_list/FeedsRepository.kt	Tue Dec 11 14:39:26 2018 -0800
@@ -29,6 +29,7 @@
 import com.geekorum.ttrss.data.FeedsDao
 import com.geekorum.ttrss.network.ApiCallException
 import com.geekorum.ttrss.network.ApiService
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.launch
 import javax.inject.Inject
@@ -98,7 +99,7 @@
         return feedsDao.getFeedsForCategory(catId)
     }
 
-    private fun refresh() = GlobalScope.launch {
+    private fun refresh() = GlobalScope.launch(Dispatchers.IO) {
         try {
             val feeds = apiService.getFeeds()
             val categories = apiService.getCategories()
--- a/app/src/main/java/com/geekorum/ttrss/data/FeedsDao.kt	Tue Dec 11 14:38:14 2018 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/data/FeedsDao.kt	Tue Dec 11 14:39:26 2018 -0800
@@ -40,8 +40,8 @@
     @get:Query("SELECT * FROM feeds ORDER BY title")
     abstract val allFeeds: LiveData<List<Feed>>
 
-    @get:Query("SELECT * FROM feeds")
-    internal abstract val allFeedsList: List<Feed>
+    @Query("SELECT * FROM feeds")
+    internal abstract suspend fun getAllFeedsList(): List<Feed>
 
     @get:Query("SELECT * FROM categories ORDER BY title")
     abstract val allCategories: LiveData<List<Category>>
@@ -49,20 +49,20 @@
     @get:Query("SELECT * FROM categories WHERE unread_count > 0 ORDER BY title")
     abstract val allUnreadCategories: LiveData<List<Category>>
 
-    @get:Query("SELECT * FROM categories")
-    internal abstract val allCategoriesList: List<Category>
+    @Query("SELECT * FROM categories")
+    abstract suspend fun getAllCategoriesList(): List<Category>
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    abstract fun insertFeeds(feeds: Collection<Feed>)
+    abstract suspend fun insertFeeds(feeds: Collection<Feed>)
 
     @Delete
-    internal abstract fun deleteFeeds(feeds: Collection<Feed>)
+    internal abstract suspend fun deleteFeeds(feeds: Collection<Feed>)
 
     @Query("DELETE FROM ARTICLES where feed_id=:feedId")
     internal abstract fun deleteArticleFromFeed(feedId: Long)
 
     @Transaction
-    open fun deleteFeedsAndArticles(toBeDelete: List<Feed>) {
+    open suspend fun deleteFeedsAndArticles(toBeDelete: List<Feed>) {
         for ((id) in toBeDelete) {
             deleteArticleFromFeed(id)
         }
@@ -74,10 +74,10 @@
     abstract fun getFeedById(id: Long): LiveData<Feed>
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    abstract fun insertCategories(categories: Collection<Category>)
+    abstract suspend fun insertCategories(categories: Collection<Category>)
 
     @Delete
-    abstract fun deleteCategories(categories: Collection<Category>)
+    abstract suspend fun deleteCategories(categories: Collection<Category>)
 
     @Query("SELECT * FROM feeds WHERE unread_count > 0 AND cat_id=:catId ORDER BY title")
     abstract fun getUnreadFeedsForCategory(catId: Long): LiveData<List<Feed>>
@@ -86,25 +86,23 @@
     abstract fun getFeedsForCategory(catId: Long): LiveData<List<Feed>>
 
     @Transaction
-    open fun setFeedsAndCategories(feeds: Collection<Feed>, categories: Collection<Category>) {
+    open suspend fun setFeedsAndCategories(feeds: Collection<Feed>, categories: Collection<Category>) {
         setCategories(categories)
         setFeeds(feeds)
     }
 
-    private fun setFeeds(feeds: Collection<Feed>) {
+    private suspend fun setFeeds(feeds: Collection<Feed>) {
         val feedsIds: List<Long> = feeds.map { it.id }
-        val toDelete = allFeedsList.filter { it.id !in feedsIds }
+        val toDelete = getAllFeedsList().filter { it.id !in feedsIds }
 
         deleteFeedsAndArticles(toDelete)
         insertFeeds(feeds)
     }
 
-    private fun setCategories(categories: Collection<Category>) {
+    private suspend fun setCategories(categories: Collection<Category>) {
         val categoriesIds: List<Long> = categories.map { category -> category.id }
-        val toDelete = allCategoriesList.filter { it.id !in categoriesIds }
+        val toDelete = getAllCategoriesList().filter { it.id !in categoriesIds }
         deleteCategories(toDelete)
         insertCategories(categories)
     }
-
-
 }
--- a/app/src/main/java/com/geekorum/ttrss/data/TransactionsDao.kt	Tue Dec 11 14:38:14 2018 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/data/TransactionsDao.kt	Tue Dec 11 14:39:26 2018 -0800
@@ -32,17 +32,17 @@
 @Dao
 abstract class TransactionsDao {
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    abstract fun insertTransaction(transaction: Transaction)
+    abstract suspend fun insertTransaction(transaction: Transaction)
 
     @Delete
-    abstract fun deleteTransactions(vararg transactions: Transaction)
+    abstract suspend fun deleteTransactions(vararg transactions: Transaction)
 
     @Delete
-    abstract fun deleteTransactions(transactions: Collection<Transaction>)
+    abstract suspend fun deleteTransactions(transactions: Collection<Transaction>)
 
 
     @Query("SELECT * FROM transactions WHERE article_id=:articleId AND field=:field")
-    abstract fun getTransactionForArticleAndType(articleId: Long, field: String): List<Transaction>
+    abstract suspend fun getTransactionForArticleAndType(articleId: Long, field: String): List<Transaction>
 
     /**
      * Insert a unique transaction for a [Transaction.articleId] [Transaction.field] pair.
@@ -50,7 +50,7 @@
      * @param transaction
      */
     @androidx.room.Transaction
-    open fun insertUniqueTransaction(transaction: Transaction) {
+    open suspend fun insertUniqueTransaction(transaction: Transaction) {
         val existingTransactions = getTransactionForArticleAndType(transaction.articleId, transaction.field)
         deleteTransactions(existingTransactions)
         insertTransaction(transaction)