articles_list: don't use feed_id on tag list
authorDa Risk <da_risk@geekorum.com>
Mon, 18 May 2020 14:27:55 -0400
changeset 692 c27a20a3384a
parent 691 fa76835b003d
child 693 cdc81caa38e8
articles_list: don't use feed_id on tag list
app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesViewModel.kt
--- a/app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesViewModel.kt	Sun May 17 22:34:13 2020 -0400
+++ b/app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesViewModel.kt	Mon May 18 14:27:55 2020 -0400
@@ -47,8 +47,6 @@
 import kotlinx.coroutines.flow.mapLatest
 import kotlinx.coroutines.launch
 
-private const val STATE_FEED_ID = "feed_id"
-private const val STATE_TAG = "tag"
 private const val STATE_NEED_UNREAD = "need_unread"
 private const val STATE_ORDER_MOST_RECENT_FIRST = "order_most_recent_first" // most_recent_first, oldest_first
 
@@ -57,25 +55,14 @@
  */
 abstract class BaseArticlesViewModel(
     private val state: SavedStateHandle,
-    private val articlesRepository: ArticlesRepository,
-    private val backgroundJobManager: BackgroundJobManager,
-    private val account: Account
+    private val articlesRepository: ArticlesRepository
 ) : ViewModel() {
 
     abstract val articles: LiveData<PagedList<Article>>
 
     private val _pendingArticlesSetUnread = MutableLiveData<Int>().apply { value = 0 }
 
-    private var refreshJobName: MutableLiveData<String?> = MutableLiveData<String?>().apply {
-        value = null
-    }
-
-    val isRefreshing: LiveData<Boolean> = refreshJobName.switchMap {
-        if (it == null)
-            SyncInProgressLiveData(account, ArticlesContract.AUTHORITY)
-        else
-            backgroundJobManager.isRefreshingStatus(state.get<Long>(STATE_FEED_ID)!!)
-    }
+    abstract val isRefreshing: LiveData<Boolean>
 
     private var shouldRefreshOnZeroItems = true
     private val unreadActionUndoManager = UndoManager<Action>()
@@ -86,6 +73,8 @@
     val haveZeroArticles: LiveData<Boolean>
         get() = articles.map { it.size == 0 }
 
+    abstract fun refresh()
+
     fun setSortByMostRecentFirst(mostRecentFirst: Boolean) {
         state[STATE_ORDER_MOST_RECENT_FIRST] = mostRecentFirst
     }
@@ -94,17 +83,6 @@
         state[STATE_NEED_UNREAD] = needUnread
     }
 
-    fun refresh() {
-        viewModelScope.launch {
-            val feedId: Long = state[STATE_FEED_ID]!!
-            if (Feed.isVirtualFeed(feedId)) {
-                backgroundJobManager.refresh(account)
-            } else {
-                refreshJobName.value = backgroundJobManager.refreshFeed(account, feedId)
-            }
-        }
-    }
-
     fun setArticleUnread(articleId: Long, newValue: Boolean) {
         val unreadAction = articlesRepository.setArticleUnread(articleId, newValue)
         unreadActionUndoManager.recordAction(unreadAction)
@@ -273,9 +251,9 @@
     @Assisted private val state: SavedStateHandle,
     articlesRepository: ArticlesRepository,
     private val feedsRepository: FeedsRepository,
-    backgroundJobManager: BackgroundJobManager,
-    account: Account
-) : BaseArticlesViewModel(state, articlesRepository, backgroundJobManager, account) {
+    private val backgroundJobManager: BackgroundJobManager,
+    private val account: Account
+) : BaseArticlesViewModel(state, articlesRepository) {
 
     val feedId = state.getLiveData(STATE_FEED_ID, Feed.FEED_ID_ALL_ARTICLES).apply {
         // workaround for out of sync values see
@@ -290,6 +268,17 @@
         getArticlesForFeed(it)
     }
 
+    private var refreshJobName: MutableLiveData<String?> = MutableLiveData<String?>().apply {
+        value = null
+    }
+
+    override val isRefreshing: LiveData<Boolean> = refreshJobName.switchMap {
+        if (it == null)
+            SyncInProgressLiveData(account, ArticlesContract.AUTHORITY)
+        else
+            backgroundJobManager.isRefreshingStatus(state.get<Long>(STATE_FEED_ID)!!)
+    }
+
     private fun getArticlesForFeed(feed: Feed): LiveData<PagedList<Article>> {
         val isMostRecentOrderFlow = state.getLiveData<Boolean>(STATE_ORDER_MOST_RECENT_FIRST).asFlow()
         val needUnreadFlow = state.getLiveData<Boolean>(STATE_NEED_UNREAD).asFlow()
@@ -312,6 +301,21 @@
     }
 
 
+    override fun refresh() {
+        viewModelScope.launch {
+            val feedId: Long = state[STATE_FEED_ID]!!
+            if (Feed.isVirtualFeed(feedId)) {
+                backgroundJobManager.refresh(account)
+            } else {
+                refreshJobName.value = backgroundJobManager.refreshFeed(account, feedId)
+            }
+        }
+    }
+
+    companion object {
+        private const val STATE_FEED_ID = "feed_id"
+    }
+
     @AssistedInject.Factory
     interface Factory : ViewModelAssistedFactory<ArticlesListViewModel> {
         override fun create(state: SavedStateHandle): ArticlesListViewModel
@@ -322,9 +326,9 @@
 class ArticlesListByTagViewModel @AssistedInject constructor(
     @Assisted private val state: SavedStateHandle,
     articlesRepository: ArticlesRepository,
-    backgroundJobManager: BackgroundJobManager,
-    account: Account
-) : BaseArticlesViewModel(state, articlesRepository, backgroundJobManager, account) {
+    private val backgroundJobManager: BackgroundJobManager,
+    private val account: Account
+) : BaseArticlesViewModel(state, articlesRepository) {
 
     val tag = state.getLiveData<String>(STATE_TAG).apply {
         // workaround for out of sync values see
@@ -335,6 +339,11 @@
     override val articles: LiveData<PagedList<Article>> = tag.switchMap {
         getArticlesForTag(it)
     }
+    override val isRefreshing: LiveData<Boolean> = SyncInProgressLiveData(account, ArticlesContract.AUTHORITY)
+
+    override fun refresh() {
+        backgroundJobManager.refresh(account)
+    }
 
     private fun getArticlesForTag(tag: String): LiveData<PagedList<Article>> {
         val isMostRecentOrderFlow = state.getLiveData<Boolean>(STATE_ORDER_MOST_RECENT_FIRST).asFlow()
@@ -351,6 +360,9 @@
             }
     }
 
+    companion object {
+        private const val STATE_TAG = "tag"
+    }
 
     @AssistedInject.Factory
     interface Factory : ViewModelAssistedFactory<ArticlesListByTagViewModel> {