浏览代码

MessageSearchActivity: add loading animation + swipe to refresh

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey 2 年之前
父节点
当前提交
0d21ce4f17

+ 14 - 3
app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchActivity.kt

@@ -84,6 +84,10 @@ class MessageSearchActivity : BaseActivity() {
         val roomToken = intent.getStringExtra(BundleKeys.KEY_ROOM_TOKEN)!!
         viewModel.initialize(user, roomToken)
         setupStateObserver()
+
+        binding.swipeRefreshLayout.setOnRefreshListener {
+            viewModel.refresh(searchView.query?.toString())
+        }
     }
 
     private fun setupActionBar() {
@@ -109,8 +113,8 @@ class MessageSearchActivity : BaseActivity() {
     private fun setupStateObserver() {
         viewModel.state.observe(this) { state ->
             when (state) {
-                MessageSearchViewModel.EmptyState -> showEmpty()
                 MessageSearchViewModel.InitialState -> showInitial()
+                MessageSearchViewModel.EmptyState -> showEmpty()
                 is MessageSearchViewModel.LoadedState -> showLoaded(state)
                 MessageSearchViewModel.LoadingState -> showLoading()
                 MessageSearchViewModel.ErrorState -> showError()
@@ -119,15 +123,20 @@ class MessageSearchActivity : BaseActivity() {
     }
 
     private fun showError() {
+        displayLoading(false)
         Toast.makeText(this, "Error while searching", Toast.LENGTH_SHORT).show()
     }
 
     private fun showLoading() {
-        // TODO
-        Toast.makeText(this, "LOADING", Toast.LENGTH_LONG).show()
+        displayLoading(true)
+    }
+
+    private fun displayLoading(loading: Boolean) {
+        binding.swipeRefreshLayout.isRefreshing = loading
     }
 
     private fun showLoaded(state: MessageSearchViewModel.LoadedState) {
+        displayLoading(false)
         binding.emptyContainer.emptyListView.visibility = View.GONE
         binding.messageSearchRecycler.visibility = View.VISIBLE
         setAdapterItems(state)
@@ -179,12 +188,14 @@ class MessageSearchActivity : BaseActivity() {
     }
 
     private fun showInitial() {
+        displayLoading(false)
         binding.messageSearchRecycler.visibility = View.GONE
         binding.emptyContainer.emptyListViewHeadline.text = "Start typing to search..."
         binding.emptyContainer.emptyListView.visibility = View.VISIBLE
     }
 
     private fun showEmpty() {
+        displayLoading(false)
         binding.messageSearchRecycler.visibility = View.GONE
         binding.emptyContainer.emptyListViewHeadline.text = "No search results"
         binding.emptyContainer.emptyListView.visibility = View.VISIBLE

+ 4 - 0
app/src/main/java/com/nextcloud/talk/messagesearch/MessageSearchViewModel.kt

@@ -107,6 +107,10 @@ class MessageSearchViewModel @Inject constructor(private val unifiedSearchReposi
         _state.value = ErrorState
     }
 
+    fun refresh(query: String?) {
+        query?.let { onQueryTextChange(it) }
+    }
+
     companion object {
         private val TAG = MessageSearchViewModel::class.simpleName
         private const val MIN_CHARS_FOR_SEARCH = 2

+ 15 - 7
app/src/main/res/layout/activity_message_search.xml

@@ -41,8 +41,7 @@
             app:navigationIconTint="@color/fontAppbar"
             app:popupTheme="@style/appActionBarPopupMenu"
             app:titleTextColor="@color/fontAppbar"
-            tools:title="@string/nc_app_product_name">
-        </com.google.android.material.appbar.MaterialToolbar>
+            tools:title="@string/nc_app_product_name"></com.google.android.material.appbar.MaterialToolbar>
 
     </com.google.android.material.appbar.AppBarLayout>
 
@@ -52,12 +51,21 @@
         android:visibility="gone"
         tools:visibility="visible" />
 
-    <androidx.recyclerview.widget.RecyclerView
-        android:id="@+id/message_search_recycler"
+
+    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
+        android:id="@+id/swipe_refresh_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
-        app:layout_behavior="@string/appbar_scrolling_view_behavior"
-        tools:listitem="@layout/rv_item_search_message" />
+        app:layout_behavior="com.nextcloud.talk.utils.FABAwareScrollingViewBehavior">
+
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/message_search_recycler"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
+            app:layout_behavior="@string/appbar_scrolling_view_behavior"
+            tools:listitem="@layout/rv_item_search_message" />
+
+    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
 
 </androidx.coordinatorlayout.widget.CoordinatorLayout>