WIP move data to viewModel

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2025-01-21 14:06:44 +01:00
parent d450c470fe
commit b412ff7bdb
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
7 changed files with 183 additions and 211 deletions

View File

@ -227,4 +227,7 @@ interface NcApiCoroutines {
@Header("Authorization") authorization: String, @Header("Authorization") authorization: String,
@Url url: String @Url url: String
): UserAbsenceOverall ): UserAbsenceOverall
@GET
suspend fun getRoom(@Header("Authorization") authorization: String?, @Url url: String?): RoomOverall
} }

View File

@ -311,7 +311,6 @@ class ChatActivity :
var adapter: TalkMessagesListAdapter<ChatMessage>? = null var adapter: TalkMessagesListAdapter<ChatMessage>? = null
var mentionAutocomplete: Autocomplete<*>? = null var mentionAutocomplete: Autocomplete<*>? = null
var layoutManager: LinearLayoutManager? = null var layoutManager: LinearLayoutManager? = null
var pullChatMessagesPending = false
var startCallFromNotification: Boolean = false var startCallFromNotification: Boolean = false
var startCallFromRoomSwitch: Boolean = false var startCallFromRoomSwitch: Boolean = false
@ -434,20 +433,22 @@ class ChatActivity :
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
chatViewModel = ViewModelProvider(this, viewModelFactory)[ChatViewModel::class.java]
binding = ActivityChatBinding.inflate(layoutInflater) binding = ActivityChatBinding.inflate(layoutInflater)
setupActionBar() // setupActionBar()
setContentView(binding.root) setContentView(binding.root)
setupSystemColors() setupSystemColors()
conversationUser = currentUserProvider.currentUser.blockingGet() conversationUser = currentUserProvider.currentUser.blockingGet() // TODO: -> ViewModel
handleIntent(intent) handleIntent(intent) // TODO: -> ViewModel
messageInputFragment = getMessageInputFragment() messageInputFragment = getMessageInputFragment()
chatViewModel = ViewModelProvider(this, viewModelFactory)[ChatViewModel::class.java] chatViewModel.getRoom(roomToken)
messageInputViewModel = ViewModelProvider(this, viewModelFactory)[MessageInputViewModel::class.java] messageInputViewModel = ViewModelProvider(this, viewModelFactory)[MessageInputViewModel::class.java]
messageInputViewModel.setData(chatViewModel.getChatRepository()) messageInputViewModel.setData(chatViewModel.getChatRepository()) // TODO: -> ViewModel
this.lifecycleScope.launch { this.lifecycleScope.launch {
delay(DELAY_TO_SHOW_PROGRESS_BAR) delay(DELAY_TO_SHOW_PROGRESS_BAR)
@ -462,8 +463,6 @@ class ChatActivity :
chatViewModel.applyPlaybackSpeedPreferences(playbackSpeedPreferences) chatViewModel.applyPlaybackSpeedPreferences(playbackSpeedPreferences)
} }
initObservers()
if (savedInstanceState != null) { if (savedInstanceState != null) {
// Restore value of members from saved state // Restore value of members from saved state
var voiceMessageId = savedInstanceState.getString(CURRENT_AUDIO_MESSAGE_KEY, "") var voiceMessageId = savedInstanceState.getString(CURRENT_AUDIO_MESSAGE_KEY, "")
@ -517,7 +516,7 @@ class ChatActivity :
} }
} }
private fun handleIntent(intent: Intent) { private fun handleIntent(intent: Intent) { // TODO: -> ViewModel
val extras: Bundle? = intent.extras val extras: Bundle? = intent.extras
roomToken = extras?.getString(KEY_ROOM_TOKEN).orEmpty() roomToken = extras?.getString(KEY_ROOM_TOKEN).orEmpty()
@ -578,53 +577,16 @@ class ChatActivity :
private fun initObservers() { private fun initObservers() {
Log.d(TAG, "initObservers Called") Log.d(TAG, "initObservers Called")
this.lifecycleScope.launch {
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
)
logConversationInfos("GetRoomSuccessState")
if (adapter == null) {
initAdapter()
binding.messagesListView.setAdapter(adapter)
layoutManager = binding.messagesListView.layoutManager as LinearLayoutManager?
}
chatViewModel.getCapabilities(conversationUser!!, roomToken, currentConversation!!)
}.collect()
}
chatViewModel.getRoomViewState.observe(this) { state ->
when (state) {
is ChatViewModel.GetRoomSuccessState -> {
// unused atm
}
is ChatViewModel.GetRoomErrorState -> {
Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show()
}
else -> {}
}
}
chatViewModel.getCapabilitiesViewState.observe(this) { state -> chatViewModel.getCapabilitiesViewState.observe(this) { state ->
when (state) { when (state) {
is ChatViewModel.GetCapabilitiesUpdateState -> { is ChatViewModel.GetCapabilitiesUpdateState -> {
if (currentConversation != null) { if (chatViewModel.currentConversation != null) {
spreedCapabilities = state.spreedCapabilities spreedCapabilities = state.spreedCapabilities
chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1)) chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!) participantPermissions = ParticipantPermissions(
spreedCapabilities,
chatViewModel.currentConversation!!
)
invalidateOptionsMenu() invalidateOptionsMenu()
checkShowCallButtons() checkShowCallButtons()
@ -639,10 +601,21 @@ class ChatActivity :
} }
is ChatViewModel.GetCapabilitiesInitialLoadState -> { is ChatViewModel.GetCapabilitiesInitialLoadState -> {
if (currentConversation != null) { setupActionBar()
if (adapter == null) {
initAdapter()
binding.messagesListView.setAdapter(adapter)
layoutManager = binding.messagesListView.layoutManager as LinearLayoutManager?
}
if (chatViewModel.currentConversation != null) {
spreedCapabilities = state.spreedCapabilities spreedCapabilities = state.spreedCapabilities
chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1)) chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
participantPermissions = ParticipantPermissions(spreedCapabilities, currentConversation!!) participantPermissions = ParticipantPermissions(
spreedCapabilities,
chatViewModel.currentConversation!!
)
supportFragmentManager.commit { supportFragmentManager.commit {
setReorderingAllowed(true) // optimizes out redundant replace operations setReorderingAllowed(true) // optimizes out redundant replace operations
@ -662,15 +635,16 @@ class ChatActivity :
setActionBarTitle() setActionBarTitle()
checkShowCallButtons() checkShowCallButtons()
checkLobbyState() checkLobbyState()
if (currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL && if (chatViewModel.currentConversation?.type ==
currentConversation?.status == "dnd" ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL &&
chatViewModel.currentConversation?.status == "dnd"
) { ) {
conversationUser?.let { user -> conversationUser?.let { user ->
val credentials = ApiUtils.getCredentials(user.username, user.token) val credentials = ApiUtils.getCredentials(user.username, user.token)
chatViewModel.outOfOfficeStatusOfUser( chatViewModel.outOfOfficeStatusOfUser(
credentials!!, credentials!!,
user.baseUrl!!, user.baseUrl!!,
currentConversation!!.name chatViewModel.currentConversation!!.name
) )
} }
} }
@ -705,11 +679,13 @@ class ChatActivity :
chatViewModel.joinRoomViewState.observe(this) { state -> chatViewModel.joinRoomViewState.observe(this) { state ->
when (state) { when (state) {
is ChatViewModel.JoinRoomSuccessState -> { is ChatViewModel.JoinRoomSuccessState -> {
currentConversation = state.conversationModel chatViewModel.currentConversation = state.conversationModel
sessionIdAfterRoomJoined = currentConversation!!.sessionId sessionIdAfterRoomJoined = chatViewModel.currentConversation!!.sessionId
ApplicationWideCurrentRoomHolder.getInstance().session = currentConversation!!.sessionId ApplicationWideCurrentRoomHolder.getInstance().session =
ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken = currentConversation!!.token chatViewModel.currentConversation!!.sessionId
ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken =
chatViewModel.currentConversation!!.token
ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser
logConversationInfos("joinRoomWithPassword#onNext") logConversationInfos("joinRoomWithPassword#onNext")
@ -744,7 +720,7 @@ class ChatActivity :
getRoomInfoTimerHandler?.removeCallbacksAndMessages(null) getRoomInfoTimerHandler?.removeCallbacksAndMessages(null)
} }
if (webSocketInstance != null && currentConversation != null) { if (webSocketInstance != null && chatViewModel.currentConversation != null) {
webSocketInstance?.joinRoomWithRoomTokenAndSession( webSocketInstance?.joinRoomWithRoomTokenAndSession(
"", "",
sessionIdAfterRoomJoined sessionIdAfterRoomJoined
@ -1159,17 +1135,15 @@ class ChatActivity :
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
logConversationInfos("onResume") initObservers()
pullChatMessagesPending = false // logConversationInfos("onResume")
webSocketInstance?.getSignalingMessageReceiver()?.addListener(localParticipantMessageListener) webSocketInstance?.getSignalingMessageReceiver()?.addListener(localParticipantMessageListener)
webSocketInstance?.getSignalingMessageReceiver()?.addListener(conversationMessageListener) webSocketInstance?.getSignalingMessageReceiver()?.addListener(conversationMessageListener)
cancelNotificationsForCurrentConversation() cancelNotificationsForCurrentConversation()
chatViewModel.getRoom(roomToken)
actionBar?.show() actionBar?.show()
setupSwipeToReply() setupSwipeToReply()
@ -1211,8 +1185,8 @@ class ChatActivity :
} }
}) })
loadAvatarForStatusBar() // loadAvatarForStatusBar()
setActionBarTitle() // setActionBarTitle()
viewThemeUtils.material.colorToolbarOverflowIcon(binding.chatToolbar) viewThemeUtils.material.colorToolbarOverflowIcon(binding.chatToolbar)
} }
@ -1245,7 +1219,7 @@ class ChatActivity :
val senderId = if (!conversationUser!!.userId.equals("?")) { val senderId = if (!conversationUser!!.userId.equals("?")) {
"users/" + conversationUser!!.userId "users/" + conversationUser!!.userId
} else { } else {
currentConversation?.actorType + "/" + currentConversation?.actorId chatViewModel.currentConversation?.actorType + "/" + chatViewModel.currentConversation?.actorId
} }
Log.d(TAG, "Initialize TalkMessagesListAdapter with senderId: $senderId") Log.d(TAG, "Initialize TalkMessagesListAdapter with senderId: $senderId")
@ -1322,7 +1296,7 @@ class ChatActivity :
val payload = MessagePayload( val payload = MessagePayload(
roomToken, roomToken,
ConversationUtils.isParticipantOwnerOrModerator(currentConversation!!), ConversationUtils.isParticipantOwnerOrModerator(chatViewModel.currentConversation!!),
profileBottomSheet profileBottomSheet
) )
@ -1525,14 +1499,14 @@ class ChatActivity :
} }
private fun loadAvatarForStatusBar() { private fun loadAvatarForStatusBar() {
if (currentConversation == null) { if (chatViewModel.currentConversation == null) {
return return
} }
if (isOneToOneConversation()) { if (isOneToOneConversation()) {
var url = ApiUtils.getUrlForAvatar( var url = ApiUtils.getUrlForAvatar(
conversationUser!!.baseUrl!!, conversationUser!!.baseUrl!!,
currentConversation!!.name, chatViewModel.currentConversation!!.name,
true true
) )
@ -1549,7 +1523,7 @@ class ChatActivity :
if (drawable != null && avatarSize > 0) { if (drawable != null && avatarSize > 0) {
val bitmap = drawable.toBitmap(avatarSize, avatarSize) val bitmap = drawable.toBitmap(avatarSize, avatarSize)
val status = StatusDrawable( val status = StatusDrawable(
currentConversation!!.status, chatViewModel.currentConversation!!.status,
null, null,
size, size,
0, 0,
@ -1561,7 +1535,7 @@ class ChatActivity :
binding.chatToolbar.findViewById<ImageView>(R.id.chat_toolbar_status) binding.chatToolbar.findViewById<ImageView>(R.id.chat_toolbar_status)
.setImageDrawable(status) .setImageDrawable(status)
binding.chatToolbar.findViewById<ImageView>(R.id.chat_toolbar_status).contentDescription = binding.chatToolbar.findViewById<ImageView>(R.id.chat_toolbar_status).contentDescription =
currentConversation?.status chatViewModel.currentConversation?.status
binding.chatToolbar.findViewById<FrameLayout>(R.id.chat_toolbar_avatar_container) binding.chatToolbar.findViewById<FrameLayout>(R.id.chat_toolbar_avatar_container)
.visibility = View.VISIBLE .visibility = View.VISIBLE
} else { } else {
@ -1599,19 +1573,19 @@ class ChatActivity :
} }
fun isOneToOneConversation() = fun isOneToOneConversation() =
currentConversation != null && chatViewModel.currentConversation != null &&
currentConversation?.type != null && chatViewModel.currentConversation?.type != null &&
currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
private fun isGroupConversation() = private fun isGroupConversation() =
currentConversation != null && chatViewModel.currentConversation != null &&
currentConversation?.type != null && chatViewModel.currentConversation?.type != null &&
currentConversation?.type == ConversationEnums.ConversationType.ROOM_GROUP_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_GROUP_CALL
private fun isPublicConversation() = private fun isPublicConversation() =
currentConversation != null && chatViewModel.currentConversation != null &&
currentConversation?.type != null && chatViewModel.currentConversation?.type != null &&
currentConversation?.type == ConversationEnums.ConversationType.ROOM_PUBLIC_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_PUBLIC_CALL
private fun updateRoomTimerHandler(delay: Long = -1) { private fun updateRoomTimerHandler(delay: Long = -1) {
val delayForRecursiveCall = if (shouldShowLobby()) { val delayForRecursiveCall = if (shouldShowLobby()) {
@ -1634,7 +1608,7 @@ class ChatActivity :
private fun switchToRoom(token: String, startCallAfterRoomSwitch: Boolean, isVoiceOnlyCall: Boolean) { private fun switchToRoom(token: String, startCallAfterRoomSwitch: Boolean, isVoiceOnlyCall: Boolean) {
if (conversationUser != null) { if (conversationUser != null) {
runOnUiThread { runOnUiThread {
if (currentConversation?.objectType == ConversationEnums.ObjectType.ROOM) { if (chatViewModel.currentConversation?.objectType == ConversationEnums.ObjectType.ROOM) {
Snackbar.make( Snackbar.make(
binding.root, binding.root,
context.resources.getString(R.string.switch_to_main_room), context.resources.getString(R.string.switch_to_main_room),
@ -2105,7 +2079,7 @@ class ChatActivity :
private fun checkShowCallButtons() { private fun checkShowCallButtons() {
if (isReadOnlyConversation() || if (isReadOnlyConversation() ||
shouldShowLobby() || shouldShowLobby() ||
ConversationUtils.isNoteToSelfConversation(currentConversation) ConversationUtils.isNoteToSelfConversation(chatViewModel.currentConversation)
) { ) {
disableCallButtons() disableCallButtons()
} else { } else {
@ -2125,10 +2099,11 @@ class ChatActivity :
} }
private fun shouldShowLobby(): Boolean { private fun shouldShowLobby(): Boolean {
if (currentConversation != null) { if (chatViewModel.currentConversation != null) {
return CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.WEBINARY_LOBBY) && return CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.WEBINARY_LOBBY) &&
currentConversation?.lobbyState == ConversationEnums.LobbyState.LOBBY_STATE_MODERATORS_ONLY && chatViewModel.currentConversation?.lobbyState ==
!ConversationUtils.canModerate(currentConversation!!, spreedCapabilities) && ConversationEnums.LobbyState.LOBBY_STATE_MODERATORS_ONLY &&
!ConversationUtils.canModerate(chatViewModel.currentConversation!!, spreedCapabilities) &&
!participantPermissions.canIgnoreLobby() !participantPermissions.canIgnoreLobby()
} }
return false return false
@ -2161,13 +2136,13 @@ class ChatActivity :
} }
private fun isReadOnlyConversation(): Boolean = private fun isReadOnlyConversation(): Boolean =
currentConversation?.conversationReadOnlyState != null && chatViewModel.currentConversation?.conversationReadOnlyState != null &&
currentConversation?.conversationReadOnlyState == chatViewModel.currentConversation?.conversationReadOnlyState ==
ConversationEnums.ConversationReadOnlyState.CONVERSATION_READ_ONLY ConversationEnums.ConversationReadOnlyState.CONVERSATION_READ_ONLY
private fun checkLobbyState() { private fun checkLobbyState() {
if (currentConversation != null && if (chatViewModel.currentConversation != null &&
ConversationUtils.isLobbyViewApplicable(currentConversation!!, spreedCapabilities) && ConversationUtils.isLobbyViewApplicable(chatViewModel.currentConversation!!, spreedCapabilities) &&
shouldShowLobby() shouldShowLobby()
) { ) {
showLobbyView() showLobbyView()
@ -2188,11 +2163,11 @@ class ChatActivity :
sb.append(resources!!.getText(R.string.nc_lobby_waiting)) sb.append(resources!!.getText(R.string.nc_lobby_waiting))
.append("\n\n") .append("\n\n")
if (currentConversation?.lobbyTimer != null && if (chatViewModel.currentConversation?.lobbyTimer != null &&
currentConversation?.lobbyTimer != chatViewModel.currentConversation?.lobbyTimer !=
0L 0L
) { ) {
val timestampMS = (currentConversation?.lobbyTimer ?: 0) * DateConstants.SECOND_DIVIDER val timestampMS = (chatViewModel.currentConversation?.lobbyTimer ?: 0) * DateConstants.SECOND_DIVIDER
val stringWithStartDate = String.format( val stringWithStartDate = String.format(
resources!!.getString(R.string.nc_lobby_start_date), resources!!.getString(R.string.nc_lobby_start_date),
dateUtils.getLocalDateTimeStringFromTimestamp(timestampMS) dateUtils.getLocalDateTimeStringFromTimestamp(timestampMS)
@ -2203,7 +2178,7 @@ class ChatActivity :
.append("\n\n") .append("\n\n")
} }
sb.append(currentConversation!!.description) sb.append(chatViewModel.currentConversation!!.description)
binding.lobby.lobbyTextView.text = sb.toString() binding.lobby.lobbyTextView.text = sb.toString()
} }
@ -2503,7 +2478,7 @@ class ChatActivity :
if (token == "") room = roomToken else room = token if (token == "") room = roomToken else room = token
chatViewModel.uploadFile(fileUri, room, currentConversation?.displayName!!, metaData) chatViewModel.uploadFile(fileUri, room, chatViewModel.currentConversation?.displayName!!, metaData)
} }
private fun showLocalFilePicker() { private fun showLocalFilePicker() {
@ -2560,7 +2535,7 @@ class ChatActivity :
} }
private fun validSessionId(): Boolean = private fun validSessionId(): Boolean =
currentConversation != null && chatViewModel.currentConversation != null &&
sessionIdAfterRoomJoined?.isNotEmpty() == true && sessionIdAfterRoomJoined?.isNotEmpty() == true &&
sessionIdAfterRoomJoined != "0" sessionIdAfterRoomJoined != "0"
@ -2628,32 +2603,32 @@ class ChatActivity :
viewThemeUtils.platform.colorTextView(title, ColorRole.ON_SURFACE) viewThemeUtils.platform.colorTextView(title, ColorRole.ON_SURFACE)
title.text = title.text =
if (currentConversation?.displayName != null) { if (chatViewModel.currentConversation?.displayName != null) {
try { try {
EmojiCompat.get().process(currentConversation?.displayName as CharSequence).toString() EmojiCompat.get().process(chatViewModel.currentConversation?.displayName as CharSequence).toString()
} catch (e: java.lang.IllegalStateException) { } catch (e: java.lang.IllegalStateException) {
Log.e(TAG, "setActionBarTitle failed $e") Log.e(TAG, "setActionBarTitle failed $e")
currentConversation?.displayName chatViewModel.currentConversation?.displayName
} }
} else { } else {
"" ""
} }
if (currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL) { if (chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL) {
var statusMessage = "" var statusMessage = ""
if (currentConversation?.statusIcon != null) { if (chatViewModel.currentConversation?.statusIcon != null) {
statusMessage += currentConversation?.statusIcon statusMessage += chatViewModel.currentConversation?.statusIcon
} }
if (currentConversation?.statusMessage != null) { if (chatViewModel.currentConversation?.statusMessage != null) {
statusMessage += currentConversation?.statusMessage statusMessage += chatViewModel.currentConversation?.statusMessage
} }
statusMessageViewContents(statusMessage) statusMessageViewContents(statusMessage)
} else { } else {
if (currentConversation?.type == ConversationEnums.ConversationType.ROOM_GROUP_CALL || if (chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_GROUP_CALL ||
currentConversation?.type == ConversationEnums.ConversationType.ROOM_PUBLIC_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_PUBLIC_CALL
) { ) {
var descriptionMessage = "" var descriptionMessage = ""
descriptionMessage += currentConversation?.description descriptionMessage += chatViewModel.currentConversation?.description
statusMessageViewContents(descriptionMessage) statusMessageViewContents(descriptionMessage)
} }
} }
@ -2689,7 +2664,7 @@ class ChatActivity :
private fun joinRoomWithPassword() { private fun joinRoomWithPassword() {
// if ApplicationWideCurrentRoomHolder contains a session (because a call is active), then keep the sessionId // if ApplicationWideCurrentRoomHolder contains a session (because a call is active), then keep the sessionId
if (ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken == if (ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken ==
currentConversation!!.token chatViewModel.currentConversation!!.token
) { ) {
sessionIdAfterRoomJoined = ApplicationWideCurrentRoomHolder.getInstance().session sessionIdAfterRoomJoined = ApplicationWideCurrentRoomHolder.getInstance().session
@ -2733,11 +2708,11 @@ class ChatActivity :
} }
private fun setupWebsocket() { private fun setupWebsocket() {
if (currentConversation == null || conversationUser == null) { if (chatViewModel.currentConversation == null || conversationUser == null) {
return return
} }
if (currentConversation!!.remoteServer?.isNotEmpty() == true) { if (chatViewModel.currentConversation!!.remoteServer?.isNotEmpty() == true) {
val apiVersion = ApiUtils.getSignalingApiVersion(conversationUser!!, intArrayOf(ApiUtils.API_V3, 2, 1)) val apiVersion = ApiUtils.getSignalingApiVersion(conversationUser!!, intArrayOf(ApiUtils.API_V3, 2, 1))
ncApi.getSignalingSettings( ncApi.getSignalingSettings(
credentials, credentials,
@ -2918,9 +2893,12 @@ class ChatActivity :
chatMessage.isGrouped = groupMessages(chatMessage, previousChatMessage) chatMessage.isGrouped = groupMessages(chatMessage, previousChatMessage)
} }
chatMessage.isOneToOneConversation = chatMessage.isOneToOneConversation =
(currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL) (
chatViewModel.currentConversation?.type ==
ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
)
chatMessage.isFormerOneToOneConversation = chatMessage.isFormerOneToOneConversation =
(currentConversation?.type == ConversationEnums.ConversationType.FORMER_ONE_TO_ONE) (chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.FORMER_ONE_TO_ONE)
Log.d(TAG, "chatMessage to add:" + chatMessage.message) Log.d(TAG, "chatMessage to add:" + chatMessage.message)
it.addToStart(chatMessage, scrollToBottom) it.addToStart(chatMessage, scrollToBottom)
} }
@ -2965,9 +2943,9 @@ class ChatActivity :
val chatMessage = chatMessageList[i] val chatMessage = chatMessageList[i]
chatMessage.isOneToOneConversation = chatMessage.isOneToOneConversation =
currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
chatMessage.isFormerOneToOneConversation = chatMessage.isFormerOneToOneConversation =
(currentConversation?.type == ConversationEnums.ConversationType.FORMER_ONE_TO_ONE) (chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.FORMER_ONE_TO_ONE)
chatMessage.activeUser = conversationUser chatMessage.activeUser = conversationUser
chatMessage.token = roomToken chatMessage.token = roomToken
} }
@ -3120,7 +3098,7 @@ class ChatActivity :
withUrl = urlForChatting, withUrl = urlForChatting,
withCredentials = credentials!!, withCredentials = credentials!!,
withMessageLimit = MESSAGE_PULL_LIMIT, withMessageLimit = MESSAGE_PULL_LIMIT,
roomToken = currentConversation!!.token roomToken = chatViewModel.currentConversation!!.token
) )
} }
@ -3157,9 +3135,9 @@ class ChatActivity :
val searchItem = menu.findItem(R.id.conversation_search) val searchItem = menu.findItem(R.id.conversation_search)
searchItem.isVisible = CapabilitiesUtil.isUnifiedSearchAvailable(spreedCapabilities) && searchItem.isVisible = CapabilitiesUtil.isUnifiedSearchAvailable(spreedCapabilities) &&
currentConversation!!.remoteServer.isNullOrEmpty() chatViewModel.currentConversation!!.remoteServer.isNullOrEmpty()
if (currentConversation!!.remoteServer != null || if (chatViewModel.currentConversation!!.remoteServer != null ||
!CapabilitiesUtil.isSharedItemsAvailable(spreedCapabilities) !CapabilitiesUtil.isSharedItemsAvailable(spreedCapabilities)
) { ) {
menu.removeItem(R.id.shared_items) menu.removeItem(R.id.shared_items)
@ -3233,18 +3211,18 @@ class ChatActivity :
private fun showSharedItems() { private fun showSharedItems() {
val intent = Intent(this, SharedItemsActivity::class.java) val intent = Intent(this, SharedItemsActivity::class.java)
intent.putExtra(KEY_CONVERSATION_NAME, currentConversation?.displayName) intent.putExtra(KEY_CONVERSATION_NAME, chatViewModel.currentConversation?.displayName)
intent.putExtra(KEY_ROOM_TOKEN, roomToken) intent.putExtra(KEY_ROOM_TOKEN, roomToken)
intent.putExtra( intent.putExtra(
SharedItemsActivity.KEY_USER_IS_OWNER_OR_MODERATOR, SharedItemsActivity.KEY_USER_IS_OWNER_OR_MODERATOR,
ConversationUtils.isParticipantOwnerOrModerator(currentConversation!!) ConversationUtils.isParticipantOwnerOrModerator(chatViewModel.currentConversation!!)
) )
startActivity(intent) startActivity(intent)
} }
private fun startMessageSearch() { private fun startMessageSearch() {
val intent = Intent(this, MessageSearchActivity::class.java) val intent = Intent(this, MessageSearchActivity::class.java)
intent.putExtra(KEY_CONVERSATION_NAME, currentConversation?.displayName) intent.putExtra(KEY_CONVERSATION_NAME, chatViewModel.currentConversation?.displayName)
intent.putExtra(KEY_ROOM_TOKEN, roomToken) intent.putExtra(KEY_ROOM_TOKEN, roomToken)
startMessageSearchForResult.launch(intent) startMessageSearchForResult.launch(intent)
} }
@ -3293,8 +3271,7 @@ class ChatActivity :
private fun isInfoMessageAboutDeletion(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean = private fun isInfoMessageAboutDeletion(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
currentMessage.value.parentMessageId != null && currentMessage.value.parentMessageId != null &&
currentMessage.value.systemMessageType == ChatMessage currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.MESSAGE_DELETED
.SystemMessageType.MESSAGE_DELETED
private fun isReactionsMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean = private fun isReactionsMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.REACTION || currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.REACTION ||
@ -3303,17 +3280,16 @@ class ChatActivity :
private fun isEditMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean = private fun isEditMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
currentMessage.value.parentMessageId != null && currentMessage.value.parentMessageId != null &&
currentMessage.value.systemMessageType == ChatMessage currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.MESSAGE_EDITED
.SystemMessageType.MESSAGE_EDITED
private fun isPollVotedMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean = private fun isPollVotedMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.POLL_VOTED currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.POLL_VOTED
private fun startACall(isVoiceOnlyCall: Boolean, callWithoutNotification: Boolean) { private fun startACall(isVoiceOnlyCall: Boolean, callWithoutNotification: Boolean) {
currentConversation?.let { chatViewModel.currentConversation?.let {
if (conversationUser != null) { if (conversationUser != null) {
val pp = ParticipantPermissions(spreedCapabilities, it) val pp = ParticipantPermissions(spreedCapabilities, it)
if (!pp.canStartCall() && currentConversation?.hasCall == false) { if (!pp.canStartCall() && chatViewModel.currentConversation?.hasCall == false) {
Snackbar.make(binding.root, R.string.startCallForbidden, Snackbar.LENGTH_LONG).show() Snackbar.make(binding.root, R.string.startCallForbidden, Snackbar.LENGTH_LONG).show()
} else { } else {
ApplicationWideCurrentRoomHolder.getInstance().isDialing = true ApplicationWideCurrentRoomHolder.getInstance().isDialing = true
@ -3327,7 +3303,7 @@ class ChatActivity :
} }
private fun getIntentForCall(isVoiceOnlyCall: Boolean, callWithoutNotification: Boolean): Intent? { private fun getIntentForCall(isVoiceOnlyCall: Boolean, callWithoutNotification: Boolean): Intent? {
currentConversation?.let { chatViewModel.currentConversation?.let {
val bundle = Bundle() val bundle = Bundle()
bundle.putString(KEY_ROOM_TOKEN, roomToken) bundle.putString(KEY_ROOM_TOKEN, roomToken)
bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, roomPassword) bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, roomPassword)
@ -3411,7 +3387,7 @@ class ChatActivity :
this, this,
message, message,
conversationUser, conversationUser,
currentConversation, chatViewModel.currentConversation,
isShowMessageDeletionButton(message), isShowMessageDeletionButton(message),
participantPermissions.hasChatPermission(), participantPermissions.hasChatPermission(),
spreedCapabilities spreedCapabilities
@ -3612,7 +3588,7 @@ class ChatActivity :
val lon = data["longitude"]!! val lon = data["longitude"]!!
metaData = metaData =
"{\"type\":\"geo-location\",\"id\":\"geo:$lat,$lon\",\"latitude\":\"$lat\"," + "{\"type\":\"geo-location\",\"id\":\"geo:$lat,$lon\",\"latitude\":\"$lat\"," +
"\"longitude\":\"$lon\",\"name\":\"$name\"}" "\"longitude\":\"$lon\",\"name\":\"$name\"}"
} }
shareToNotes(shareUri, roomToken, message, objectId, metaData) shareToNotes(shareUri, roomToken, message, objectId, metaData)
@ -3693,8 +3669,8 @@ class ChatActivity :
conversationUser?.userId?.isNotEmpty() == true && conversationUser?.userId?.isNotEmpty() == true &&
conversationUser!!.userId != "?" && conversationUser!!.userId != "?" &&
message.user.id.startsWith("users/") && message.user.id.startsWith("users/") &&
message.user.id.substring(ACTOR_LENGTH) != currentConversation?.actorId && message.user.id.substring(ACTOR_LENGTH) != chatViewModel.currentConversation?.actorId &&
currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL || chatViewModel.currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL ||
isShowMessageDeletionButton(message) || isShowMessageDeletionButton(message) ||
// delete // delete
ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() || ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() ||
@ -3710,7 +3686,7 @@ class ChatActivity :
messageTemp.message = getString(R.string.message_deleted_by_you) messageTemp.message = getString(R.string.message_deleted_by_you)
messageTemp.isOneToOneConversation = messageTemp.isOneToOneConversation =
currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
messageTemp.activeUser = conversationUser messageTemp.activeUser = conversationUser
adapter?.update(messageTemp) adapter?.update(messageTemp)
@ -3728,7 +3704,7 @@ class ChatActivity :
} }
messageTemp.isOneToOneConversation = messageTemp.isOneToOneConversation =
currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
messageTemp.activeUser = conversationUser messageTemp.activeUser = conversationUser
adapter?.update(messageTemp) adapter?.update(messageTemp)
@ -3740,7 +3716,7 @@ class ChatActivity :
// TODO is this needed? // TODO is this needed?
messageTemp.isOneToOneConversation = messageTemp.isOneToOneConversation =
currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL chatViewModel.currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
messageTemp.activeUser = conversationUser messageTemp.activeUser = conversationUser
adapter?.update(messageTemp) adapter?.update(messageTemp)
@ -3815,7 +3791,7 @@ class ChatActivity :
val isUserAllowedByPrivileges = if (message.actorId == conversationUser!!.userId) { val isUserAllowedByPrivileges = if (message.actorId == conversationUser!!.userId) {
true true
} else { } else {
ConversationUtils.canModerate(currentConversation!!, spreedCapabilities) ConversationUtils.canModerate(chatViewModel.currentConversation!!, spreedCapabilities)
} }
return isUserAllowedByPrivileges return isUserAllowedByPrivileges
} }
@ -3877,8 +3853,8 @@ class ChatActivity :
@Subscribe(threadMode = ThreadMode.BACKGROUND) @Subscribe(threadMode = ThreadMode.BACKGROUND)
fun onMessageEvent(userMentionClickEvent: UserMentionClickEvent) { fun onMessageEvent(userMentionClickEvent: UserMentionClickEvent) {
if (currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL || if (chatViewModel.currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL ||
currentConversation?.name != userMentionClickEvent.userId chatViewModel.currentConversation?.name != userMentionClickEvent.userId
) { ) {
var apiVersion = 1 var apiVersion = 1
// FIXME Fix API checking with guests? // FIXME Fix API checking with guests?
@ -3980,7 +3956,10 @@ class ChatActivity :
Log.d(TAG, " | method: $methodName") Log.d(TAG, " | method: $methodName")
Log.d(TAG, " | ChatActivity: " + System.identityHashCode(this).toString()) Log.d(TAG, " | ChatActivity: " + System.identityHashCode(this).toString())
Log.d(TAG, " | roomToken: $roomToken") Log.d(TAG, " | roomToken: $roomToken")
Log.d(TAG, " | currentConversation?.displayName: ${currentConversation?.displayName}") Log.d(
TAG,
" | chatViewModel.currentConversation?.displayName: ${chatViewModel.currentConversation?.displayName}"
)
Log.d(TAG, " | sessionIdAfterRoomJoined: $sessionIdAfterRoomJoined") Log.d(TAG, " | sessionIdAfterRoomJoined: $sessionIdAfterRoomJoined")
Log.d(TAG, " |-----------------------------------------------") Log.d(TAG, " |-----------------------------------------------")
} }

View File

@ -21,6 +21,7 @@ import retrofit2.Response
@Suppress("LongParameterList", "TooManyFunctions") @Suppress("LongParameterList", "TooManyFunctions")
interface ChatNetworkDataSource { interface ChatNetworkDataSource {
fun getRoom(user: User, roomToken: String): Observable<ConversationModel> fun getRoom(user: User, roomToken: String): Observable<ConversationModel>
suspend fun getRoomCoroutines(user: User, roomToken: String): ConversationModel
fun getCapabilities(user: User, roomToken: String): Observable<SpreedCapability> fun getCapabilities(user: User, roomToken: String): Observable<SpreedCapability>
fun joinRoom(user: User, roomToken: String, roomPassword: String): Observable<ConversationModel> fun joinRoom(user: User, roomToken: String, roomPassword: String): Observable<ConversationModel>
fun setReminder( fun setReminder(

View File

@ -36,6 +36,18 @@ class RetrofitChatNetwork(
).map { ConversationModel.mapToConversationModel(it.ocs?.data!!, user) } ).map { ConversationModel.mapToConversationModel(it.ocs?.data!!, user) }
} }
override suspend fun getRoomCoroutines(user: User, roomToken: String): ConversationModel {
val credentials: String = ApiUtils.getCredentials(user.username, user.token)!!
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1))
val conversation = ncApiCoroutines.getRoom(
credentials,
ApiUtils.getUrlForRoom(apiVersion, user.baseUrl!!, roomToken)
).ocs?.data!!
return ConversationModel.mapToConversationModel(conversation, user)
}
override fun getCapabilities(user: User, roomToken: String): Observable<SpreedCapability> { override fun getCapabilities(user: User, roomToken: String): Observable<SpreedCapability> {
val credentials: String = ApiUtils.getCredentials(user.username, user.token)!! val credentials: String = ApiUtils.getCredentials(user.username, user.token)!!
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1)) val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1))

View File

@ -38,6 +38,7 @@ import com.nextcloud.talk.models.json.reminder.Reminder
import com.nextcloud.talk.models.json.userAbsence.UserAbsenceData import com.nextcloud.talk.models.json.userAbsence.UserAbsenceData
import com.nextcloud.talk.repositories.reactions.ReactionsRepository import com.nextcloud.talk.repositories.reactions.ReactionsRepository
import com.nextcloud.talk.ui.PlaybackSpeed import com.nextcloud.talk.ui.PlaybackSpeed
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.ConversationUtils import com.nextcloud.talk.utils.ConversationUtils
import com.nextcloud.talk.utils.bundle.BundleKeys import com.nextcloud.talk.utils.bundle.BundleKeys
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
@ -67,6 +68,12 @@ class ChatViewModel @Inject constructor(
) : ViewModel(), ) : ViewModel(),
DefaultLifecycleObserver { DefaultLifecycleObserver {
var chatApiVersion: Int = 1
val currentUser: User = userProvider.currentUser.blockingGet()
lateinit var currentConversation: ConversationModel
enum class LifeCycleFlag { enum class LifeCycleFlag {
PAUSED, PAUSED,
RESUMED, RESUMED,
@ -145,13 +152,6 @@ class ChatViewModel @Inject constructor(
val getLastReadMessageFlow = chatRepository.lastReadMessageFlow val getLastReadMessageFlow = chatRepository.lastReadMessageFlow
val getConversationFlow = conversationRepository.conversationFlow
.onEach {
_getRoomViewState.value = GetRoomSuccessState
}.catch {
_getRoomViewState.value = GetRoomErrorState
}
val getGeneralUIFlow = chatRepository.generalUIFlow val getGeneralUIFlow = chatRepository.generalUIFlow
sealed interface ViewState sealed interface ViewState
@ -172,14 +172,6 @@ class ChatViewModel @Inject constructor(
val getNoteToSelfAvailability: LiveData<ViewState> val getNoteToSelfAvailability: LiveData<ViewState>
get() = _getNoteToSelfAvailability get() = _getNoteToSelfAvailability
object GetRoomStartState : ViewState
object GetRoomErrorState : ViewState
object GetRoomSuccessState : ViewState
private val _getRoomViewState: MutableLiveData<ViewState> = MutableLiveData(GetRoomStartState)
val getRoomViewState: LiveData<ViewState>
get() = _getRoomViewState
object GetCapabilitiesStartState : ViewState object GetCapabilitiesStartState : ViewState
object GetCapabilitiesErrorState : ViewState object GetCapabilitiesErrorState : ViewState
open class GetCapabilitiesInitialLoadState(val spreedCapabilities: SpreedCapability) : ViewState open class GetCapabilitiesInitialLoadState(val spreedCapabilities: SpreedCapability) : ViewState
@ -243,16 +235,31 @@ class ChatViewModel @Inject constructor(
val reactionDeletedViewState: LiveData<ViewState> val reactionDeletedViewState: LiveData<ViewState>
get() = _reactionDeletedViewState get() = _reactionDeletedViewState
fun setData(conversationModel: ConversationModel, credentials: String, urlForChatting: String) {
chatRepository.setData(conversationModel, credentials, urlForChatting)
}
fun getRoom(token: String) { fun getRoom(token: String) {
_getRoomViewState.value = GetRoomStartState viewModelScope.launch {
conversationRepository.getRoom(token) conversationRepository.getRoom(token).collect { conversation ->
currentConversation = conversation!!
// val chatApiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, currentUser.baseUrl, token)
val credentials = ApiUtils.getCredentials(currentUser.username, currentUser.token)
chatRepository.setData(currentConversation, credentials!!, urlForChatting)
// logConversationInfos("GetRoomSuccessState")
// if (adapter == null) { // do later when capabilities are fetched?
// initAdapter()
// binding.messagesListView.setAdapter(adapter)
// layoutManager = binding.messagesListView.layoutManager as LinearLayoutManager?
// }
getCapabilities(currentUser, currentConversation)
}
}
} }
fun getCapabilities(user: User, token: String, conversationModel: ConversationModel) { fun getCapabilities(user: User, conversationModel: ConversationModel) {
Log.d(TAG, "Remote server ${conversationModel.remoteServer}") Log.d(TAG, "Remote server ${conversationModel.remoteServer}")
if (conversationModel.remoteServer.isNullOrEmpty()) { if (conversationModel.remoteServer.isNullOrEmpty()) {
if (_getCapabilitiesViewState.value == GetCapabilitiesStartState) { if (_getCapabilitiesViewState.value == GetCapabilitiesStartState) {
@ -263,7 +270,7 @@ class ChatViewModel @Inject constructor(
_getCapabilitiesViewState.value = GetCapabilitiesUpdateState(user.capabilities!!.spreedCapability!!) _getCapabilitiesViewState.value = GetCapabilitiesUpdateState(user.capabilities!!.spreedCapability!!)
} }
} else { } else {
chatNetworkDataSource.getCapabilities(user, token) chatNetworkDataSource.getCapabilities(user, conversationModel.token)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(object : Observer<SpreedCapability> { ?.subscribe(object : Observer<SpreedCapability> {
@ -362,7 +369,6 @@ class ChatViewModel @Inject constructor(
override fun onNext(t: GenericOverall) { override fun onNext(t: GenericOverall) {
_leaveRoomViewState.value = LeaveRoomSuccessState(funToCallWhenLeaveSuccessful) _leaveRoomViewState.value = LeaveRoomSuccessState(funToCallWhenLeaveSuccessful)
_getCapabilitiesViewState.value = GetCapabilitiesStartState _getCapabilitiesViewState.value = GetCapabilitiesStartState
_getRoomViewState.value = GetRoomStartState
} }
}) })
} }

View File

@ -18,11 +18,6 @@ interface OfflineConversationsRepository {
*/ */
val roomListFlow: Flow<List<ConversationModel>> val roomListFlow: Flow<List<ConversationModel>>
/**
* Stream of a single conversation, for use in each conversations settings.
*/
val conversationFlow: Flow<ConversationModel>
/** /**
* Loads rooms from local storage. If the rooms are not found, then it * Loads rooms from local storage. If the rooms are not found, then it
* synchronizes the database with the server, before retrying exactly once. Only * synchronizes the database with the server, before retrying exactly once. Only
@ -35,5 +30,5 @@ interface OfflineConversationsRepository {
* Called once onStart to emit a conversation to [conversationFlow] * Called once onStart to emit a conversation to [conversationFlow]
* to be handled asynchronously. * to be handled asynchronously.
*/ */
fun getRoom(roomToken: String): Job fun getRoom(roomToken: String): Flow<ConversationModel?>
} }

View File

@ -20,9 +20,7 @@ import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.domain.ConversationModel import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.utils.CapabilitiesUtil.isUserStatusAvailable import com.nextcloud.talk.utils.CapabilitiesUtil.isUserStatusAvailable
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -30,9 +28,9 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject import javax.inject.Inject
class OfflineFirstConversationsRepository @Inject constructor( class OfflineFirstConversationsRepository @Inject constructor(
@ -46,10 +44,6 @@ class OfflineFirstConversationsRepository @Inject constructor(
get() = _roomListFlow get() = _roomListFlow
private val _roomListFlow: MutableSharedFlow<List<ConversationModel>> = MutableSharedFlow() private val _roomListFlow: MutableSharedFlow<List<ConversationModel>> = MutableSharedFlow()
override val conversationFlow: Flow<ConversationModel>
get() = _conversationFlow
private val _conversationFlow: MutableSharedFlow<ConversationModel> = MutableSharedFlow()
private val scope = CoroutineScope(Dispatchers.IO) private val scope = CoroutineScope(Dispatchers.IO)
private var user: User = currentUserProviderNew.currentUser.blockingGet() private var user: User = currentUserProviderNew.currentUser.blockingGet()
@ -67,41 +61,23 @@ class OfflineFirstConversationsRepository @Inject constructor(
} }
} }
override fun getRoom(roomToken: String): Job = override fun getRoom(roomToken: String): Flow<ConversationModel?> =
scope.launch { flow {
chatNetworkDataSource.getRoom(user, roomToken) try {
.subscribeOn(Schedulers.io()) val conversationModel = chatNetworkDataSource.getRoomCoroutines(user, roomToken)
?.observeOn(AndroidSchedulers.mainThread()) emit(conversationModel)
?.subscribe(object : Observer<ConversationModel> { val entityList = listOf(conversationModel.asEntity())
override fun onSubscribe(p0: Disposable) { dao.upsertConversations(entityList)
// unused atm } catch (e: Exception) {
} // In case network is offline or call fails
val id = user.id!!
override fun onError(e: Throwable) { val model = getConversation(id, roomToken)
runBlocking { if (model != null) {
// In case network is offline or call fails emit(model)
val id = user.id!! } else {
val model = getConversation(id, roomToken) Log.e(TAG, "Conversation model not found on device database")
if (model != null) { }
_conversationFlow.emit(model) }
} else {
Log.e(TAG, "Conversation model not found on device database")
}
}
}
override fun onComplete() {
// unused atm
}
override fun onNext(model: ConversationModel) {
runBlocking {
_conversationFlow.emit(model)
val entityList = listOf(model.asEntity())
dao.upsertConversations(entityList)
}
}
})
} }
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")