@@ -232,7 +232,11 @@ import io.reactivex.disposables.Disposable
232232import io.reactivex.schedulers.Schedulers
233233import kotlinx.coroutines.CoroutineScope
234234import kotlinx.coroutines.Dispatchers
235+ import kotlinx.coroutines.ExperimentalCoroutinesApi
235236import kotlinx.coroutines.flow.collect
237+ import kotlinx.coroutines.flow.collectLatest
238+ import kotlinx.coroutines.flow.flatMapLatest
239+ import kotlinx.coroutines.flow.flowOf
236240import kotlinx.coroutines.flow.onEach
237241import kotlinx.coroutines.launch
238242import 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 }
0 commit comments