From 1bcdeb2d4698dedd881d9a3be3392e6b7cca1bee Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Tue, 18 Feb 2025 10:48:17 +0100 Subject: [PATCH] init other data earlier than ConversationModel because setData was called too late (after sendTempChatMessages was called from messageInputFragment, internalConversationId was not initialized in repository). With this change, internalConversationId, credentials and urlForChatting are initialized earlier which should avoid the issue. However conversationModel is still initialized quite late and should be accessed with caution. This could may be improved by better architecture.. Signed-off-by: Marcel Hibbe --- .../com/nextcloud/talk/chat/ChatActivity.kt | 21 ++++++++++--------- .../talk/chat/data/ChatMessageRepository.kt | 4 +++- .../network/OfflineFirstChatRepository.kt | 9 +++++--- .../talk/chat/viewmodels/ChatViewModel.kt | 8 +++++-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 2e9012241..01160ae16 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -440,10 +440,17 @@ class ChatActivity : conversationUser = currentUserProvider.currentUser.blockingGet() handleIntent(intent) - messageInputFragment = getMessageInputFragment() - chatViewModel = ViewModelProvider(this, viewModelFactory)[ChatViewModel::class.java] + val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken) + val credentials = ApiUtils.getCredentials(conversationUser!!.username, conversationUser!!.token) + chatViewModel.initData( + credentials!!, + urlForChatting, + roomToken + ) + + messageInputFragment = getMessageInputFragment() messageInputViewModel = ViewModelProvider(this, viewModelFactory)[MessageInputViewModel::class.java] messageInputViewModel.setData(chatViewModel.getChatRepository()) @@ -576,14 +583,8 @@ class ChatActivity : chatViewModel.getConversationFlow .onEach { conversationModel -> currentConversation = conversationModel - - val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken) - val credentials = ApiUtils.getCredentials(conversationUser!!.username, conversationUser!!.token) - - chatViewModel.setData( - currentConversation!!, - credentials!!, - urlForChatting + chatViewModel.updateConversation( + currentConversation!! ) logConversationInfos("GetRoomSuccessState") diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt index 2cc6979ff..8e2e3f3ed 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt @@ -44,7 +44,9 @@ interface ChatMessageRepository : LifecycleAwareManager { val removeMessageFlow: Flow - fun setData(conversationModel: ConversationModel, credentials: String, urlForChatting: String) + fun initData(credentials: String, urlForChatting: String, roomToken: String) + + fun updateConversation(conversationModel: ConversationModel) fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle) diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index bea8a9a02..35f36bc69 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -118,11 +118,14 @@ class OfflineFirstChatRepository @Inject constructor( private lateinit var credentials: String private lateinit var urlForChatting: String - override fun setData(conversationModel: ConversationModel, credentials: String, urlForChatting: String) { - this.conversationModel = conversationModel + override fun initData(credentials: String, urlForChatting: String, roomToken: String) { + internalConversationId = currentUser.id.toString() + "@" + roomToken this.credentials = credentials this.urlForChatting = urlForChatting - internalConversationId = conversationModel.internalId + } + + override fun updateConversation(conversationModel: ConversationModel) { + this.conversationModel = conversationModel } override fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle) { diff --git a/app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt b/app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt index a4a8ad538..f3097250a 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt @@ -243,8 +243,12 @@ class ChatViewModel @Inject constructor( val reactionDeletedViewState: LiveData get() = _reactionDeletedViewState - fun setData(conversationModel: ConversationModel, credentials: String, urlForChatting: String) { - chatRepository.setData(conversationModel, credentials, urlForChatting) + fun initData(credentials: String, urlForChatting: String, roomToken: String) { + chatRepository.initData(credentials, urlForChatting, roomToken) + } + + fun updateConversation(currentConversation: ConversationModel) { + chatRepository.updateConversation(currentConversation) } fun getRoom(token: String) {