Skip to content

Commit 3ebb4dd

Browse files
authored
Merge pull request #5807 from nextcloud/pr-5774-1
Simplifying pinned messages
2 parents c74d4ea + 8ab7068 commit 3ebb4dd

4 files changed

Lines changed: 183 additions & 100 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ import io.reactivex.disposables.Disposable
232232
import io.reactivex.schedulers.Schedulers
233233
import kotlinx.coroutines.CoroutineScope
234234
import kotlinx.coroutines.Dispatchers
235+
import kotlinx.coroutines.ExperimentalCoroutinesApi
235236
import kotlinx.coroutines.flow.collect
237+
import kotlinx.coroutines.flow.collectLatest
238+
import kotlinx.coroutines.flow.flatMapLatest
239+
import kotlinx.coroutines.flow.flowOf
236240
import kotlinx.coroutines.flow.onEach
237241
import kotlinx.coroutines.launch
238242
import kotlinx.coroutines.runBlocking
@@ -652,55 +656,54 @@ class ChatActivity :
652656
this.lifecycle.removeObserver(chatViewModel)
653657
}
654658

659+
@OptIn(ExperimentalCoroutinesApi::class)
655660
@SuppressLint("NotifyDataSetChanged", "SetTextI18n", "ResourceAsColor")
656661
@Suppress("LongMethod")
657662
private fun initObservers() {
658663
Log.d(TAG, "initObservers Called")
659-
660-
this.lifecycleScope.launch {
664+
lifecycleScope.launch {
661665
chatViewModel.getConversationFlow
662-
.collect { conversationModel ->
666+
.onEach { conversationModel ->
663667
currentConversation = conversationModel
664-
chatViewModel.updateConversation(
665-
currentConversation!!
666-
)
667-
668+
chatViewModel.updateConversation(conversationModel)
668669
logConversationInfos("GetRoomSuccessState")
669670

670671
if (adapter == null) {
671672
initAdapter()
672673
binding.messagesListView.setAdapter(adapter)
673-
layoutManager = binding.messagesListView.layoutManager as LinearLayoutManager?
674+
layoutManager = binding.messagesListView.layoutManager as? LinearLayoutManager
674675
}
675676

676-
chatViewModel.getCapabilities(conversationUser!!, roomToken, currentConversation!!)
677-
677+
chatViewModel.getCapabilities(conversationUser!!, roomToken, conversationModel)
678+
}
679+
.flatMapLatest { conversationModel ->
678680
if (conversationModel.lastPinnedId != null &&
679681
conversationModel.lastPinnedId != 0L &&
680682
conversationModel.lastPinnedId != conversationModel.hiddenPinnedId
681683
) {
682-
chatViewModel
683-
.getIndividualMessageFromServer(
684-
credentials!!,
685-
conversationUser?.baseUrl!!,
686-
roomToken,
687-
conversationModel.lastPinnedId.toString()
684+
chatViewModel.getIndividualMessageFromServer(
685+
credentials!!,
686+
conversationUser?.baseUrl!!,
687+
roomToken,
688+
conversationModel.lastPinnedId.toString()
689+
)
690+
} else {
691+
flowOf(null)
692+
}
693+
}
694+
.collectLatest { message ->
695+
if (message != null) {
696+
binding.pinnedMessageContainer.visibility = View.VISIBLE
697+
binding.pinnedMessageComposeView.setContent {
698+
PinnedMessageView(
699+
message,
700+
viewThemeUtils,
701+
currentConversation,
702+
scrollToMessageWithIdWithOffset = ::scrollToMessageWithIdWithOffset,
703+
hidePinnedMessage = ::hidePinnedMessage,
704+
unPinMessage = ::unPinMessage
688705
)
689-
.collect { message ->
690-
message?.let {
691-
binding.pinnedMessageContainer.visibility = View.VISIBLE
692-
binding.pinnedMessageComposeView.setContent {
693-
PinnedMessageView(
694-
message,
695-
viewThemeUtils,
696-
currentConversation,
697-
scrollToMessageWithIdWithOffset = ::scrollToMessageWithIdWithOffset,
698-
hidePinnedMessage = ::hidePinnedMessage,
699-
unPinMessage = ::unPinMessage
700-
)
701-
}
702-
}
703-
}
706+
}
704707
} else {
705708
binding.pinnedMessageContainer.visibility = View.GONE
706709
}

app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ import io.reactivex.Observer
5555
import io.reactivex.android.schedulers.AndroidSchedulers
5656
import io.reactivex.disposables.Disposable
5757
import io.reactivex.schedulers.Schedulers
58+
import kotlinx.coroutines.Dispatchers
5859
import kotlinx.coroutines.flow.Flow
5960
import kotlinx.coroutines.flow.MutableSharedFlow
6061
import kotlinx.coroutines.flow.MutableStateFlow
6162
import kotlinx.coroutines.flow.StateFlow
6263
import kotlinx.coroutines.flow.catch
6364
import kotlinx.coroutines.flow.first
6465
import kotlinx.coroutines.flow.flow
66+
import kotlinx.coroutines.flow.flowOn
6567
import kotlinx.coroutines.flow.onEach
6668
import kotlinx.coroutines.launch
6769
import java.io.File
@@ -887,7 +889,7 @@ class ChatViewModel @Inject constructor(
887889
} else {
888890
emit(null)
889891
}
890-
}
892+
}.flowOn(Dispatchers.IO)
891893

892894
suspend fun getNumberOfThreadReplies(threadId: Long): Int = chatRepository.getNumberOfThreadReplies(threadId)
893895

0 commit comments

Comments
 (0)