ArticlesListFragment: observe the refresh of the specific feed
authorDa Risk <da_risk@geekorum.com>
Sun, 10 Nov 2019 14:59:06 -0800
changeset 544 0b9eb2b30bf8
parent 543 199ff7119340
child 545 1ce33111cdc8
ArticlesListFragment: observe the refresh of the specific feed
app/src/main/java/com/geekorum/ttrss/articles_list/ActivityViewModel.kt
app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesListFragment.kt
app/src/main/java/com/geekorum/ttrss/articles_list/FragmentViewModel.kt
app/src/main/java/com/geekorum/ttrss/background_job/BackgroundJobManager.kt
app/src/main/res/layout/fragment_article_list.xml
--- a/app/src/main/java/com/geekorum/ttrss/articles_list/ActivityViewModel.kt	Sun Nov 10 14:34:44 2019 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/articles_list/ActivityViewModel.kt	Sun Nov 10 14:59:06 2019 -0800
@@ -28,19 +28,14 @@
 import androidx.lifecycle.SavedStateHandle
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.switchMap
-import com.geekorum.geekdroid.accounts.SyncInProgressLiveData
-import com.geekorum.geekdroid.app.lifecycle.EmptyEvent
 import com.geekorum.geekdroid.app.lifecycle.Event
 import com.geekorum.geekdroid.dagger.ViewModelAssistedFactory
 import com.geekorum.ttrss.background_job.BackgroundJobManager
 import com.geekorum.ttrss.data.Article
 import com.geekorum.ttrss.data.Feed
 import com.geekorum.ttrss.network.TtRssBrowserLauncher
-import com.geekorum.ttrss.providers.ArticlesContract
 import com.squareup.inject.assisted.Assisted
 import com.squareup.inject.assisted.AssistedInject
-import com.geekorum.geekdroid.app.lifecycle.EmptyEvent.Companion.makeEmptyEvent as SearchClosedEvent
-import com.geekorum.geekdroid.app.lifecycle.EmptyEvent.Companion.makeEmptyEvent as SearchOpenedEvent
 
 private const val STATE_FEED_ID = "feed_id"
 private const val STATE_ACCOUNT = "account"
@@ -71,10 +66,8 @@
     private val _searchQuery = MutableLiveData<String>()
     val searchQuery: LiveData<String> = _searchQuery
 
-    val isRefreshing: LiveData<Boolean> =
-        state.getLiveData<Account>(STATE_ACCOUNT).switchMap {
-            SyncInProgressLiveData(it, ArticlesContract.AUTHORITY)
-        }
+    private val _refreshClickedEvent = MutableLiveData<Event<Any>>()
+    val refreshClickedEvent: LiveData<Event<Any>> = _refreshClickedEvent
 
     init {
         browserLauncher.warmUp()
@@ -90,13 +83,7 @@
     }
 
     fun refresh() {
-        val feedId: Long = state[STATE_FEED_ID]!!
-        val account: Account = state[STATE_ACCOUNT]!!
-        if (Feed.isVirtualFeed(feedId)) {
-            backgroundJobManager.refresh(account)
-        } else {
-            backgroundJobManager.refreshFeed(account, feedId)
-        }
+        _refreshClickedEvent.value = Event(Any())
     }
 
     fun displayArticle(position: Int, article: Article) {
--- a/app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesListFragment.kt	Sun Nov 10 14:34:44 2019 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/articles_list/ArticlesListFragment.kt	Sun Nov 10 14:59:06 2019 -0800
@@ -32,6 +32,7 @@
 import androidx.recyclerview.widget.ItemTouchHelper
 import androidx.recyclerview.widget.RecyclerView
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
+import com.geekorum.geekdroid.app.lifecycle.EventObserver
 import com.geekorum.geekdroid.dagger.DaggerDelegateSavedStateVMFactory
 import com.geekorum.geekdroid.views.recyclerview.ItemSwiper
 import com.geekorum.geekdroid.views.recyclerview.ScrollFromBottomAppearanceItemAnimator
@@ -93,7 +94,10 @@
             }
         }
 
-        binding.activityViewModel = activityViewModel
+        activityViewModel.refreshClickedEvent.observe(this, EventObserver {
+            fragmentViewModel.refresh()
+        })
+
         binding.fragmentViewModel = fragmentViewModel
     }
 
@@ -110,13 +114,13 @@
         recyclerView.setupCardSpacing()
 
         swipeRefresh.setOnRefreshListener {
-            activityViewModel.refresh()
+            fragmentViewModel.refresh()
             // leave the progress at least 1s, then refresh its value
             // So if the user trigger a refresh but no sync operation is launch (eg: because of no connectivity)
             // the SwipeRefreshLayout will come back to original status
             viewLifecycleOwner.lifecycleScope.launch {
                 delay(1000)
-                binding.swipeRefreshContainer.isRefreshing = activityViewModel.isRefreshing.value!!
+                binding.swipeRefreshContainer.isRefreshing = fragmentViewModel.isRefreshing.value!!
             }
         }
         recyclerView.itemAnimator = ScrollFromBottomAppearanceItemAnimator(recyclerView, DefaultItemAnimator())
--- a/app/src/main/java/com/geekorum/ttrss/articles_list/FragmentViewModel.kt	Sun Nov 10 14:34:44 2019 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/articles_list/FragmentViewModel.kt	Sun Nov 10 14:59:06 2019 -0800
@@ -28,18 +28,22 @@
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.map
 import androidx.lifecycle.switchMap
+import androidx.lifecycle.viewModelScope
 import androidx.paging.DataSource
-import androidx.paging.LivePagedListBuilder
 import androidx.paging.PagedList
 import androidx.paging.toLiveData
+import com.geekorum.geekdroid.accounts.SyncInProgressLiveData
 import com.geekorum.geekdroid.dagger.ViewModelAssistedFactory
 import com.geekorum.ttrss.background_job.BackgroundJobManager
 import com.geekorum.ttrss.data.Article
 import com.geekorum.ttrss.data.Feed
+import com.geekorum.ttrss.providers.ArticlesContract
 import com.geekorum.ttrss.session.Action
 import com.geekorum.ttrss.session.UndoManager
 import com.squareup.inject.assisted.Assisted
 import com.squareup.inject.assisted.AssistedInject
+import kotlinx.coroutines.launch
+import java.util.UUID
 
 private const val PREF_VIEW_MODE = "view_mode"
 private const val STATE_FEED_ID = "feed_id"
@@ -77,6 +81,17 @@
         getArticlesForFeed(it)
     }
 
+    private var refreshJobId: MutableLiveData<UUID> = MutableLiveData<UUID>().apply {
+        value = null
+    }
+
+    val isRefreshing: LiveData<Boolean> = refreshJobId.switchMap {
+        if (it == null)
+            SyncInProgressLiveData(account, ArticlesContract.AUTHORITY)
+        else
+            backgroundJobManager.isRefreshingStatus(it)
+    }
+
     private val unreadActionUndoManager = UndoManager<Action>()
 
     // default value in databinding is False for boolean and 0 for int
@@ -134,7 +149,14 @@
     }
 
     fun refresh() {
-        backgroundJobManager.refreshFeed(account, state[STATE_FEED_ID]!!)
+        viewModelScope.launch {
+            val feedId: Long = state[STATE_FEED_ID]!!
+            if (Feed.isVirtualFeed(feedId)) {
+                backgroundJobManager.refresh(account)
+            } else {
+                refreshJobId.value = backgroundJobManager.refreshFeed(account, feedId)
+            }
+        }
     }
 
     fun setArticleUnread(articleId: Long, newValue: Boolean) {
--- a/app/src/main/java/com/geekorum/ttrss/background_job/BackgroundJobManager.kt	Sun Nov 10 14:34:44 2019 -0800
+++ b/app/src/main/java/com/geekorum/ttrss/background_job/BackgroundJobManager.kt	Sun Nov 10 14:59:06 2019 -0800
@@ -65,9 +65,6 @@
         impl.refresh(account)
     }
 
-    fun refreshFeed(account: Account, feedId: Long) {
-        impl.refreshFeed(account, feedId)
-    }
     /**
      * Refresh a feed.
      * @return workmanager job uuid
@@ -132,11 +129,6 @@
         requestSync(account, extras)
     }
 
-    fun refreshFeed(account: Account, feedId: Long) {
-        val extras = Bundle()
-        extras.putLong(SyncContract.EXTRA_FEED_ID, feedId)
-        requestSync(account, extras)
-    }
     suspend fun refreshFeed(account: Account, feedId: Long): UUID {
         val constraints = Constraints.Builder()
                 .setRequiredNetworkType(NetworkType.CONNECTED)
--- a/app/src/main/res/layout/fragment_article_list.xml	Sun Nov 10 14:34:44 2019 -0800
+++ b/app/src/main/res/layout/fragment_article_list.xml	Sun Nov 10 14:59:06 2019 -0800
@@ -28,7 +28,6 @@
 
     <data>
         <import type="android.view.View" />
-        <variable name="activityViewModel" type="com.geekorum.ttrss.articles_list.ActivityViewModel" />
         <variable name="fragmentViewModel" type="com.geekorum.ttrss.articles_list.FragmentViewModel" />
     </data>
 
@@ -36,7 +35,7 @@
             android:id="@+id/swipe_refresh_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            app:refreshing="@{safeUnbox(activityViewModel.refreshing)}">
+            app:refreshing="@{safeUnbox(fragmentViewModel.refreshing)}">
 
         <androidx.constraintlayout.widget.ConstraintLayout
                 android:layout_width="match_parent"
@@ -52,7 +51,7 @@
                       android:gravity="center"
                       android:textAppearance="?textAppearanceHeadline4"
                       android:padding="@dimen/activity_horizontal_margin"
-                      android:text="@{safeUnbox(activityViewModel.refreshing) ? @string/fragment_articles_list_no_articles_and_sync_lbl : @string/fragment_articles_list_no_articles_lbl}"
+                      android:text="@{safeUnbox(fragmentViewModel.refreshing) ? @string/fragment_articles_list_no_articles_and_sync_lbl : @string/fragment_articles_list_no_articles_lbl}"
                       android:visibility="@{safeUnbox(fragmentViewModel.haveZeroArticles) ? View.VISIBLE : View.GONE}"
                     />