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 bd3816542..2c5aaab9d 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -256,6 +256,8 @@ class ChatActivity : lateinit var chatViewModel: ChatViewModel lateinit var messageInputViewModel: MessageInputViewModel + private lateinit var noteToSelfRoomToken: String + private val startSelectContactForResult = registerForActivityResult( ActivityResultContracts .StartActivityForResult() @@ -640,6 +642,27 @@ class ChatActivity : } } + chatViewModel.noteToSelfViewState.observe(this) { state -> + + when (state) { + is ChatViewModel.NoteToSelfErrorState -> { + Snackbar.make(binding.root, "Unable to send message to note to self", Snackbar.LENGTH_LONG).show() + } + ChatViewModel.NoteToSelfStartState -> { + } + is ChatViewModel.NoteToSelfSuccessState -> { + val roomOverall = state.roomOverall + noteToSelfRoomToken = roomOverall.ocs?.data?.token!! + } + + is ChatViewModel.NoteToSelfNotAvailableState -> { + } + + else -> { + } + } + } + chatViewModel.joinRoomViewState.observe(this) { state -> when (state) { is ChatViewModel.JoinRoomSuccessState -> { 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 d8e0d46db..126e8f38b 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 @@ -275,6 +275,14 @@ class ChatViewModel @Inject constructor( val reactionDeletedViewState: LiveData get() = _reactionDeletedViewState + object NoteToSelfStartState : ViewState + class NoteToSelfErrorState(val message: String) : ViewState + class NoteToSelfSuccessState(val roomOverall: RoomOverall) : ViewState + + private val _noteToSelfViewState: MutableLiveData = MutableLiveData(NoteToSelfStartState) + val noteToSelfViewState: LiveData + get() = _noteToSelfViewState + fun initData(credentials: String, urlForChatting: String, roomToken: String) { chatRepository.initData(credentials, urlForChatting, roomToken) } @@ -539,7 +547,24 @@ class ChatViewModel @Inject constructor( fun checkForNoteToSelf(user: User) { chatNetworkDataSource.checkForNoteToSelf(user).subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(CheckForNoteToSelfObserver()) + ?. subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + disposableSet.add(d) + } + + override fun onNext(roomOverall: RoomOverall) { + _noteToSelfViewState.value = NoteToSelfSuccessState(roomOverall) + } + + override fun onError(e: Throwable) { + Log.e(TAG, "Error when sharing location to notes $e") + _noteToSelfViewState.value = NoteToSelfErrorState(e.localizedMessage ?: "") + } + + override fun onComplete() { + // unused atm + } + }) } fun shareLocationToNotes(credentials: String, url: String, objectType: String, objectId: String, metadata: String) { @@ -782,24 +807,6 @@ class ChatViewModel @Inject constructor( } } - inner class CheckForNoteToSelfObserver : Observer { - override fun onSubscribe(d: Disposable) { - disposableSet.add(d) - } - - override fun onNext(roomOverall: RoomOverall) { - val room = roomOverall.ocs?.data - } - - override fun onError(e: Throwable) { - Log.d(TAG, "Error when getting rooms for Note to Self Observer $e") - } - - override fun onComplete() { - // unused atm - } - } - @Suppress("Detekt.TooGenericExceptionCaught") fun outOfOfficeStatusOfUser(credentials: String, baseUrl: String, userId: String) { viewModelScope.launch { diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index 173d722bb..db4eac85f 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -25,6 +25,7 @@ import com.nextcloud.talk.R import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.chat.ChatActivity import com.nextcloud.talk.chat.data.model.ChatMessage +import com.nextcloud.talk.chat.viewmodels.ChatViewModel import com.nextcloud.talk.data.network.NetworkMonitor import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.databinding.DialogMessageActionsBinding @@ -69,6 +70,9 @@ class MessageActionsDialog( @Inject lateinit var reactionsRepository: ReactionsRepository + @Inject + lateinit var chatViewModel: ChatViewModel + @Inject lateinit var dateUtils: DateUtils @@ -96,6 +100,8 @@ class MessageActionsDialog( .EDIT_MESSAGES_NOTE_TO_SELF ) && currentConversation?.type == ConversationEnums.ConversationType.NOTE_TO_SELF + private var noteToSelfRoomToken: String = "" + private val isMessageBotOneToOne = (message.actorType == ACTOR_BOTS) && ( message.isOneToOneConversation || message.isFormerOneToOneConversation @@ -118,6 +124,7 @@ class MessageActionsDialog( viewThemeUtils.material.colorBottomSheetBackground(dialogMessageActionsBinding.root) viewThemeUtils.material.colorBottomSheetDragHandle(dialogMessageActionsBinding.bottomSheetDragHandle) + initObservers() initEmojiBar(hasChatPermission) initMenuItemCopy(!message.isDeleted) @@ -126,7 +133,6 @@ class MessageActionsDialog( !message.isDeleted && currentConversation?.type != ConversationEnums.ConversationType.NOTE_TO_SELF ) - initMenuItems(networkMonitor.isOnline.value) } @@ -173,6 +179,27 @@ class MessageActionsDialog( } } + fun initObservers() { + chatViewModel.noteToSelfViewState.observe(this) { state -> + + when (state) { + is ChatViewModel.NoteToSelfErrorState -> { + } + ChatViewModel.NoteToSelfStartState -> { + } + is ChatViewModel.NoteToSelfSuccessState -> { + val roomOverall = state.roomOverall + noteToSelfRoomToken = roomOverall.ocs?.data?.token!! + } + + is ChatViewModel.NoteToSelfNotAvailableState -> { + } + + else -> { + } + } + } + } override fun onStart() { super.onStart() val bottomSheet = findViewById(R.id.design_bottom_sheet) @@ -447,11 +474,11 @@ class MessageActionsDialog( dialogMessageActionsBinding.menuSaveMessage.visibility = getVisibility(visible) } - private fun initMenuAddToNote(visible: Boolean, roomToken: String = "") { + private fun initMenuAddToNote(visible: Boolean) { if (visible) { + chatViewModel.checkForNoteToSelf(user!!) dialogMessageActionsBinding.menuShareToNote.setOnClickListener { - chatActivity.chatViewModel.checkForNoteToSelf(user!!) - chatActivity.shareToNotes(message, roomToken) + chatActivity.shareToNotes(message, noteToSelfRoomToken) dismiss() } }