Browse Source

minor cleanup in OfflineFirstChatRepository

fix to show unreadMessagesPopup at bottom (typingIndicatorWrapper must not be gone but invisible)

align unreadMessagesPopup to not overlap typingIndicator

fix to hide scrollDown button when unreadMessagesPopup is shown

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 10 months ago
parent
commit
5bccdada7c

+ 4 - 3
app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

@@ -989,7 +989,8 @@ class ChatActivity :
                 super.onScrollStateChanged(recyclerView, newState)
                 super.onScrollStateChanged(recyclerView, newState)
 
 
                 if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                 if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
-                    if (layoutManager!!.findFirstCompletelyVisibleItemPosition() > 0) {
+                    if (layoutManager!!.findFirstCompletelyVisibleItemPosition() > 0 &&
+                        !binding.unreadMessagesPopup.isShown) {
                         binding.scrollDownButton.visibility = View.VISIBLE
                         binding.scrollDownButton.visibility = View.VISIBLE
                     } else {
                     } else {
                         binding.scrollDownButton.visibility = View.GONE
                         binding.scrollDownButton.visibility = View.GONE
@@ -999,7 +1000,7 @@ class ChatActivity :
                         if (layoutManager!!.findFirstCompletelyVisibleItemPosition() < newMessagesCount) {
                         if (layoutManager!!.findFirstCompletelyVisibleItemPosition() < newMessagesCount) {
                             newMessagesCount = 0
                             newMessagesCount = 0
 
 
-                            if (binding.unreadMessagesPopup.isShown == true) {
+                            if (binding.unreadMessagesPopup.isShown) {
                                 binding.unreadMessagesPopup.hide()
                                 binding.unreadMessagesPopup.hide()
                             }
                             }
                         }
                         }
@@ -1280,7 +1281,7 @@ class ChatActivity :
                     .setInterpolator(AccelerateDecelerateInterpolator())
                     .setInterpolator(AccelerateDecelerateInterpolator())
                     .duration = TYPING_INDICATOR_ANIMATION_DURATION
                     .duration = TYPING_INDICATOR_ANIMATION_DURATION
             } else {
             } else {
-                binding.typingIndicatorWrapper.visibility = View.GONE
+                binding.typingIndicatorWrapper.visibility = View.INVISIBLE
                 binding.typingIndicatorWrapper.y += DisplayUtils.convertDpToPixel(18f, context)
                 binding.typingIndicatorWrapper.y += DisplayUtils.convertDpToPixel(18f, context)
             }
             }
         }
         }

+ 5 - 39
app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt

@@ -89,7 +89,6 @@ class OfflineFirstChatRepository @Inject constructor(
         this.conversationModel = conversationModel
         this.conversationModel = conversationModel
         this.credentials = credentials
         this.credentials = credentials
         this.urlForChatting = urlForChatting
         this.urlForChatting = urlForChatting
-        // internalConversationId = userProvider.currentUser.blockingGet().id!!.toString() + "@" + conversationModel.token
         internalConversationId = conversationModel.internalId
         internalConversationId = conversationModel.internalId
     }
     }
 
 
@@ -134,26 +133,11 @@ class OfflineFirstChatRepository @Inject constructor(
                 lastKnown = beforeMessageId.toInt()
                 lastKnown = beforeMessageId.toInt()
             )
             )
             withNetworkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
             withNetworkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
-            // withNetworkParams.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken)
 
 
             val loadFromServer = hasToLoadPreviousMessagesFromServer(beforeMessageId)
             val loadFromServer = hasToLoadPreviousMessagesFromServer(beforeMessageId)
 
 
-            if (loadFromServer) {
-                if (monitor.isOnline.first()) {
-                    sync(withNetworkParams)
-                } else {
-                    // TODO: handle how user is informed about gaps when being offline. Something like:
-                    // val offlineChatMessage = ChatMessage(
-                    //     message = "you are offline. Some messages might be missing here."
-                    // )
-                    // val list = mutableListOf<ChatMessage>()
-                    // list.add(offlineChatMessage)
-                    //
-                    // if (list.isNotEmpty()) {
-                    //     val pair = Pair(false, list)
-                    //     _messageFlow.emit(pair)
-                    // }
-                }
+            if (loadFromServer && monitor.isOnline.first()) {
+                sync(withNetworkParams)
             }
             }
 
 
             showLast100MessagesBefore(internalConversationId, beforeMessageId)
             showLast100MessagesBefore(internalConversationId, beforeMessageId)
@@ -161,8 +145,6 @@ class OfflineFirstChatRepository @Inject constructor(
 
 
     override fun initMessagePolling(): Job =
     override fun initMessagePolling(): Job =
         scope.launch {
         scope.launch {
-            // monitor.isOnline.onEach { online ->
-
             Log.d(TAG, "---- initMessagePolling ------------")
             Log.d(TAG, "---- initMessagePolling ------------")
 
 
             val initialMessageId = chatDao.getNewestMessageId(internalConversationId).toInt()
             val initialMessageId = chatDao.getNewestMessageId(internalConversationId).toInt()
@@ -180,15 +162,10 @@ class OfflineFirstChatRepository @Inject constructor(
             while (!itIsPaused) {
             while (!itIsPaused) {
                 if (!monitor.isOnline.first()) Thread.sleep(500)
                 if (!monitor.isOnline.first()) Thread.sleep(500)
 
 
-                // sync database with server ( This is a long blocking call b/c long polling is set )
+                // sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set)
                 networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
                 networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
-                // withNetworkParams.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken)
-
-                // this@OfflineFirstChatRepository.sync(withNetworkParams)
-                // sync(withNetworkParams)
 
 
                 val resultsFromSync = sync(networkParams)
                 val resultsFromSync = sync(networkParams)
-                // TODO: load from DB?! at least make sure no changes are made here that are not saved to DB then!
 
 
                 Log.d(TAG, "got result from longpolling")
                 Log.d(TAG, "got result from longpolling")
                 if (!resultsFromSync.isNullOrEmpty()) {
                 if (!resultsFromSync.isNullOrEmpty()) {
@@ -209,18 +186,16 @@ class OfflineFirstChatRepository @Inject constructor(
                 //     return@map chatMessage
                 //     return@map chatMessage
                 // }
                 // }
 
 
-                val newestMessage2 = chatDao.getNewestMessageId(internalConversationId).toInt()
-                Log.d(TAG, "newestMessage in loop: $newestMessage2")
+                val newestMessage = chatDao.getNewestMessageId(internalConversationId).toInt()
 
 
                 // update field map vars for next cycle
                 // update field map vars for next cycle
                 fieldMap = getFieldMap(
                 fieldMap = getFieldMap(
                     lookIntoFuture = true,
                     lookIntoFuture = true,
                     includeLastKnown = false,
                     includeLastKnown = false,
                     setReadMarker = true,
                     setReadMarker = true,
-                    lastKnown = newestMessage2
+                    lastKnown = newestMessage
                 )
                 )
             }
             }
-            // }.flowOn(Dispatchers.IO).collect()
         }
         }
 
 
     private suspend fun hasToLoadPreviousMessagesFromServer(
     private suspend fun hasToLoadPreviousMessagesFromServer(
@@ -282,11 +257,6 @@ class OfflineFirstChatRepository @Inject constructor(
         return fieldMap
         return fieldMap
     }
     }
 
 
-    private suspend fun getMessagesFrom(messageIds: List<Long>): List<ChatMessage> =
-        chatDao.getMessagesFromIds(messageIds).map {
-            it.map(ChatMessageEntity::asModel)
-        }.first()
-
     override suspend fun getMessage(messageId: Long, bundle: Bundle):
     override suspend fun getMessage(messageId: Long, bundle: Bundle):
         Flow<ChatMessage> {
         Flow<ChatMessage> {
 
 
@@ -314,8 +284,6 @@ class OfflineFirstChatRepository @Inject constructor(
     @Suppress("UNCHECKED_CAST")
     @Suppress("UNCHECKED_CAST")
     private fun getMessagesFromServer(bundle: Bundle): Pair<Int, List<ChatMessageJson>>? {
     private fun getMessagesFromServer(bundle: Bundle): Pair<Int, List<ChatMessageJson>>? {
         Log.d(TAG, "An online request is made!!!!!!!!!!!!!!!!!!!!")
         Log.d(TAG, "An online request is made!!!!!!!!!!!!!!!!!!!!")
-        // val credentials = bundle.getString(BundleKeys.KEY_CREDENTIALS)
-        // val url = bundle.getString(BundleKeys.KEY_CHAT_URL)
         val fieldMap = bundle.getSerializable(BundleKeys.KEY_FIELD_MAP) as HashMap<String, Int>
         val fieldMap = bundle.getSerializable(BundleKeys.KEY_FIELD_MAP) as HashMap<String, Int>
 
 
         try {
         try {
@@ -393,7 +361,6 @@ class OfflineFirstChatRepository @Inject constructor(
         val lookIntoFuture = fieldMap["lookIntoFuture"] == 1
         val lookIntoFuture = fieldMap["lookIntoFuture"] == 1
 
 
         val statusCode = result.first
         val statusCode = result.first
-        // val statusCode = result.first
 
 
         val hasHistory = getHasHistory(statusCode, lookIntoFuture)
         val hasHistory = getHasHistory(statusCode, lookIntoFuture)
 
 
@@ -487,7 +454,6 @@ class OfflineFirstChatRepository @Inject constructor(
                     // We scroll up and load the system message. Deleting everything is not an option as we
                     // We scroll up and load the system message. Deleting everything is not an option as we
                     // would loose the messages that we want to keep. We only want to
                     // would loose the messages that we want to keep. We only want to
                     // delete the messages and chatBlocks older than the system message.
                     // delete the messages and chatBlocks older than the system message.
-
                     chatDao.deleteMessagesOlderThan(internalConversationId, messageJson.id)
                     chatDao.deleteMessagesOlderThan(internalConversationId, messageJson.id)
                     chatBlocksDao.deleteChatBlocksOlderThan(internalConversationId, messageJson.id)
                     chatBlocksDao.deleteChatBlocksOlderThan(internalConversationId, messageJson.id)
                 }
                 }

+ 1 - 1
app/src/main/res/layout/activity_chat.xml

@@ -166,7 +166,7 @@
             android:layout_marginStart="64dp"
             android:layout_marginStart="64dp"
             android:layout_marginTop="16dp"
             android:layout_marginTop="16dp"
             android:layout_marginEnd="64dp"
             android:layout_marginEnd="64dp"
-            android:layout_marginBottom="26dp"
+            android:layout_marginBottom="45dp"
             android:layout_toStartOf="@+id/scrollDownButton"
             android:layout_toStartOf="@+id/scrollDownButton"
             android:ellipsize="middle"
             android:ellipsize="middle"
             android:minHeight="@dimen/min_size_clickable_area"
             android:minHeight="@dimen/min_size_clickable_area"