瀏覽代碼

extract methods from fetchRooms method

reduce complexity for codacy score..

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 2 年之前
父節點
當前提交
42e6d9b52f
共有 1 個文件被更改,包括 52 次插入44 次删除
  1. 52 44
      app/src/main/java/com/nextcloud/talk/controllers/ConversationsListController.kt

+ 52 - 44
app/src/main/java/com/nextcloud/talk/controllers/ConversationsListController.kt

@@ -523,51 +523,9 @@ class ConversationsListController(bundle: Bundle) :
                     adapterWasNull = false
                     binding.loadingContent.visibility = View.GONE
                 }
-                if (ocs!!.data!!.isNotEmpty()) {
-                    if (binding.emptyLayout.visibility != View.GONE) {
-                        binding.emptyLayout.visibility = View.GONE
-                    }
-                    if (binding.swipeRefreshLayoutView.visibility != View.VISIBLE) {
-                        binding.swipeRefreshLayoutView.visibility = View.VISIBLE
-                    }
-                } else {
-                    if (binding.emptyLayout.visibility != View.VISIBLE) {
-                        binding.emptyLayout.visibility = View.VISIBLE
-                    }
-                    if (binding.swipeRefreshLayoutView.visibility != View.GONE) {
-                        binding.swipeRefreshLayoutView.visibility = View.GONE
-                    }
-                }
+                initOverallLayout(ocs!!.data!!.isNotEmpty())
                 for (conversation in ocs.data!!) {
-                    if (bundle.containsKey(KEY_FORWARD_HIDE_SOURCE_ROOM) && conversation.roomId == bundle.getString(
-                            KEY_FORWARD_HIDE_SOURCE_ROOM
-                        )
-                    ) {
-                        continue
-                    }
-                    val headerTitle: String = resources!!.getString(R.string.conversations)
-                    var genericTextHeaderItem: GenericTextHeaderItem
-                    if (!callHeaderItems.containsKey(headerTitle)) {
-                        genericTextHeaderItem = GenericTextHeaderItem(headerTitle, viewThemeUtils)
-                        callHeaderItems[headerTitle] = genericTextHeaderItem
-                    }
-                    if (activity != null) {
-                        val conversationItem = ConversationItem(
-                            conversation,
-                            currentUser,
-                            activity,
-                            viewThemeUtils
-                        )
-                        conversationItems.add(conversationItem)
-                        val conversationItemWithHeader = ConversationItem(
-                            conversation,
-                            currentUser,
-                            activity,
-                            callHeaderItems[headerTitle],
-                            viewThemeUtils
-                        )
-                        conversationItemsWithHeader.add(conversationItemWithHeader)
-                    }
+                    addToConversationItems(conversation)
                 }
                 sortConversations(conversationItems)
                 sortConversations(conversationItemsWithHeader)
@@ -593,6 +551,56 @@ class ConversationsListController(bundle: Bundle) :
             }
     }
 
+    private fun initOverallLayout(isConversationListNotEmpty: Boolean) {
+        if (isConversationListNotEmpty) {
+            if (binding.emptyLayout.visibility != View.GONE) {
+                binding.emptyLayout.visibility = View.GONE
+            }
+            if (binding.swipeRefreshLayoutView.visibility != View.VISIBLE) {
+                binding.swipeRefreshLayoutView.visibility = View.VISIBLE
+            }
+        } else {
+            if (binding.emptyLayout.visibility != View.VISIBLE) {
+                binding.emptyLayout.visibility = View.VISIBLE
+            }
+            if (binding.swipeRefreshLayoutView.visibility != View.GONE) {
+                binding.swipeRefreshLayoutView.visibility = View.GONE
+            }
+        }
+    }
+
+    private fun addToConversationItems(conversation: Conversation) {
+        if (bundle.containsKey(KEY_FORWARD_HIDE_SOURCE_ROOM) && conversation.roomId == bundle.getString(
+                KEY_FORWARD_HIDE_SOURCE_ROOM
+            )
+        ) {
+            return
+        }
+        val headerTitle: String = resources!!.getString(R.string.conversations)
+        val genericTextHeaderItem: GenericTextHeaderItem
+        if (!callHeaderItems.containsKey(headerTitle)) {
+            genericTextHeaderItem = GenericTextHeaderItem(headerTitle, viewThemeUtils)
+            callHeaderItems[headerTitle] = genericTextHeaderItem
+        }
+        if (activity != null) {
+            val conversationItem = ConversationItem(
+                conversation,
+                currentUser,
+                activity,
+                viewThemeUtils
+            )
+            conversationItems.add(conversationItem)
+            val conversationItemWithHeader = ConversationItem(
+                conversation,
+                currentUser,
+                activity,
+                callHeaderItems[headerTitle],
+                viewThemeUtils
+            )
+            conversationItemsWithHeader.add(conversationItemWithHeader)
+        }
+    }
+
     private fun showErrorDialog() {
         val dialogBuilder = MaterialAlertDialogBuilder(binding.floatingActionButton.context)
             .setIcon(