Skip to content
65 changes: 34 additions & 31 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -652,55 +656,54 @@ class ChatActivity :
this.lifecycle.removeObserver(chatViewModel)
}

@OptIn(ExperimentalCoroutinesApi::class)
@SuppressLint("NotifyDataSetChanged", "SetTextI18n", "ResourceAsColor")
@Suppress("LongMethod")
private fun initObservers() {
Log.d(TAG, "initObservers Called")

this.lifecycleScope.launch {
lifecycleScope.launch {
chatViewModel.getConversationFlow
.collect { conversationModel ->
.onEach { conversationModel ->
currentConversation = conversationModel
chatViewModel.updateConversation(
currentConversation!!
)

chatViewModel.updateConversation(conversationModel)
logConversationInfos("GetRoomSuccessState")

if (adapter == null) {
initAdapter()
binding.messagesListView.setAdapter(adapter)
layoutManager = binding.messagesListView.layoutManager as LinearLayoutManager?
layoutManager = binding.messagesListView.layoutManager as? LinearLayoutManager
}

chatViewModel.getCapabilities(conversationUser!!, roomToken, currentConversation!!)

chatViewModel.getCapabilities(conversationUser!!, roomToken, conversationModel)
}
.flatMapLatest { conversationModel ->
if (conversationModel.lastPinnedId != null &&
conversationModel.lastPinnedId != 0L &&
conversationModel.lastPinnedId != conversationModel.hiddenPinnedId
) {
chatViewModel
.getIndividualMessageFromServer(
credentials!!,
conversationUser?.baseUrl!!,
roomToken,
conversationModel.lastPinnedId.toString()
chatViewModel.getIndividualMessageFromServer(
credentials!!,
conversationUser?.baseUrl!!,
roomToken,
conversationModel.lastPinnedId.toString()
)
} else {
flowOf(null)
}
}
.collectLatest { message ->
if (message != null) {
binding.pinnedMessageContainer.visibility = View.VISIBLE
binding.pinnedMessageComposeView.setContent {
PinnedMessageView(
message,
viewThemeUtils,
currentConversation,
scrollToMessageWithIdWithOffset = ::scrollToMessageWithIdWithOffset,
hidePinnedMessage = ::hidePinnedMessage,
unPinMessage = ::unPinMessage
)
.collect { message ->
message?.let {
binding.pinnedMessageContainer.visibility = View.VISIBLE
binding.pinnedMessageComposeView.setContent {
PinnedMessageView(
message,
viewThemeUtils,
currentConversation,
scrollToMessageWithIdWithOffset = ::scrollToMessageWithIdWithOffset,
hidePinnedMessage = ::hidePinnedMessage,
unPinMessage = ::unPinMessage
)
}
}
}
}
} else {
binding.pinnedMessageContainer.visibility = View.GONE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import java.io.File
Expand Down Expand Up @@ -887,7 +889,7 @@ class ChatViewModel @Inject constructor(
} else {
emit(null)
}
}
}.flowOn(Dispatchers.IO)

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

Expand Down
Loading
Loading