Browse Source

Rename and reduce complexity of function 'processMessages'

Signed-off-by: Tim Krüger <t@timkrueger.me>
Tim Krüger 2 years ago
parent
commit
0c06c8b2eb
1 changed files with 38 additions and 30 deletions
  1. 38 30
      app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

+ 38 - 30
app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

@@ -2206,7 +2206,7 @@ class ChatController(args: Bundle) :
                             } else if (response.code() == HTTP_CODE_PRECONDITION_FAILED) {
                                 futurePreconditionFailed = true
                             } else {
-                                processMessages(response, true)
+                                processMessagesResponse(response, true)
                             }
                         } catch (npe: NullPointerException) {
                             // view binding can be null
@@ -2248,7 +2248,7 @@ class ChatController(args: Bundle) :
                             if (response.code() == HTTP_CODE_PRECONDITION_FAILED) {
                                 pastPreconditionFailed = true
                             } else {
-                                processMessages(response, false)
+                                processMessagesResponse(response, false)
                             }
                         } catch (e: NullPointerException) {
                             // view binding can be null
@@ -2293,7 +2293,7 @@ class ChatController(args: Bundle) :
         }
     }
 
-    private fun processMessages(response: Response<*>, isFromTheFuture: Boolean) {
+    private fun processMessagesResponse(response: Response<*>, isFromTheFuture: Boolean) {
 
         val xChatLastCommonRead = response.headers()["X-Chat-Last-Common-Read"]?.let {
             Integer.parseInt(it)
@@ -2305,47 +2305,55 @@ class ChatController(args: Bundle) :
             val chatOverall = response.body() as ChatOverall?
             val chatMessageList = handleSystemMessages(chatOverall?.ocs!!.data!!)
 
-            if (chatMessageList.isNotEmpty() &&
-                ChatMessage.SystemMessageType.CLEARED_CHAT == chatMessageList[0].systemMessageType
-            ) {
-                adapter?.clear()
-                adapter?.notifyDataSetChanged()
-            }
-
+            processMessages(chatMessageList, isFromTheFuture, xChatLastCommonRead)
+        } else if (response.code() == HTTP_CODE_NOT_MODIFIED && !isFromTheFuture) {
             if (isFirstMessagesProcessing) {
                 cancelNotificationsForCurrentConversation()
 
                 isFirstMessagesProcessing = false
                 binding.progressBar.visibility = View.GONE
-
-                binding.messagesListView.visibility = View.VISIBLE
             }
 
-            if (isFromTheFuture) {
-                processMessagesFromTheFuture(chatMessageList)
-            } else {
-                processMessagesNotFromTheFuture(chatMessageList)
+            historyRead = true
+
+            if (!lookingIntoFuture && inConversation) {
+                pullChatMessages(1)
             }
+        }
+    }
 
-            updateReadStatusOfAllMessages(xChatLastCommonRead)
+    private fun processMessages(
+        chatMessageList: List<ChatMessage>,
+        isFromTheFuture: Boolean,
+        xChatLastCommonRead: Int?
+    ) {
+        if (chatMessageList.isNotEmpty() &&
+            ChatMessage.SystemMessageType.CLEARED_CHAT == chatMessageList[0].systemMessageType
+        ) {
+            adapter?.clear()
             adapter?.notifyDataSetChanged()
+        }
 
-            if (inConversation) {
-                pullChatMessages(1, 1, xChatLastCommonRead)
-            }
-        } else if (response.code() == HTTP_CODE_NOT_MODIFIED && !isFromTheFuture) {
-            if (isFirstMessagesProcessing) {
-                cancelNotificationsForCurrentConversation()
+        if (isFirstMessagesProcessing) {
+            cancelNotificationsForCurrentConversation()
 
-                isFirstMessagesProcessing = false
-                binding.progressBar.visibility = View.GONE
-            }
+            isFirstMessagesProcessing = false
+            binding.progressBar.visibility = View.GONE
 
-            historyRead = true
+            binding.messagesListView.visibility = View.VISIBLE
+        }
 
-            if (!lookingIntoFuture && inConversation) {
-                pullChatMessages(1)
-            }
+        if (isFromTheFuture) {
+            processMessagesFromTheFuture(chatMessageList)
+        } else {
+            processMessagesNotFromTheFuture(chatMessageList)
+        }
+
+        updateReadStatusOfAllMessages(xChatLastCommonRead)
+        adapter?.notifyDataSetChanged()
+
+        if (inConversation) {
+            pullChatMessages(1, 1, xChatLastCommonRead)
         }
     }