Draft: handle sharing so it doesn't destroy the previous draft

This commit is contained in:
ganfra 2020-09-23 13:32:07 +02:00 committed by Benoit Marty
parent aa0520d47d
commit 91b81af5a8
5 changed files with 106 additions and 104 deletions

View File

@ -75,11 +75,7 @@ internal class DraftRepository @Inject constructor(@SessionDatabase private val
}
private fun deleteDraftFromDb(realm: Realm, roomId: String) {
UserDraftsEntity.where(realm, roomId).findFirst()?.let { userDraftsEntity ->
if (userDraftsEntity.userDrafts.isNotEmpty()) {
userDraftsEntity.userDrafts.removeAt(userDraftsEntity.userDrafts.size - 1)
}
}
UserDraftsEntity.where(realm, roomId).findFirst()?.userDrafts?.clear()
}
private fun saveDraftInDb(realm: Realm, draft: UserDraft, roomId: String) {

View File

@ -51,7 +51,7 @@ sealed class RoomDetailAction : VectorViewModelAction {
data class EnterEditMode(val eventId: String, val text: String) : RoomDetailAction()
data class EnterQuoteMode(val eventId: String, val text: String) : RoomDetailAction()
data class EnterReplyMode(val eventId: String, val text: String) : RoomDetailAction()
data class ExitSpecialMode(val text: String) : RoomDetailAction()
data class EnterRegularMode(val text: String, val fromSharing: Boolean) : RoomDetailAction()
data class ResendMessage(val eventId: String) : RoomDetailAction()
data class RemoveFailedEcho(val eventId: String) : RoomDetailAction()

View File

@ -485,8 +485,7 @@ class RoomDetailFragment @Inject constructor(
if (savedInstanceState == null) {
when (val sharedData = roomDetailArgs.sharedData) {
is SharedData.Text -> {
// Save a draft to set the shared text to the composer
roomDetailViewModel.handle(RoomDetailAction.SaveDraft(sharedData.text))
roomDetailViewModel.handle(RoomDetailAction.EnterRegularMode(sharedData.text, fromSharing = true))
}
is SharedData.Attachments -> {
// open share edition
@ -1014,7 +1013,7 @@ class RoomDetailFragment @Inject constructor(
}
override fun onCloseRelatedMessage() {
roomDetailViewModel.handle(RoomDetailAction.ExitSpecialMode(composerLayout.text.toString()))
roomDetailViewModel.handle(RoomDetailAction.EnterRegularMode(composerLayout.text.toString(), false))
}
override fun onRichContentSelected(contentUri: Uri): Boolean {
@ -1147,12 +1146,8 @@ class RoomDetailFragment @Inject constructor(
private fun renderSendMessageResult(sendMessageResult: RoomDetailViewEvents.SendMessageResult) {
when (sendMessageResult) {
is RoomDetailViewEvents.MessageSent -> {
updateComposerText("")
}
is RoomDetailViewEvents.SlashCommandHandled -> {
sendMessageResult.messageRes?.let { showSnackWithMessage(getString(it)) }
updateComposerText("")
}
is RoomDetailViewEvents.SlashCommandError -> {
displayCommandError(getString(R.string.command_problem_with_parameters, sendMessageResult.command.command))

View File

@ -164,7 +164,7 @@ class RoomDetailViewModel @AssistedInject constructor(
getUnreadState()
observeSyncState()
observeEventDisplayedActions()
observeDrafts()
getDraftIfAny()
observeUnreadState()
observeMyRoomMember()
observeActiveRoomWidgets()
@ -240,7 +240,7 @@ class RoomDetailViewModel @AssistedInject constructor(
is RoomDetailAction.RedactAction -> handleRedactEvent(action)
is RoomDetailAction.UndoReaction -> handleUndoReact(action)
is RoomDetailAction.UpdateQuickReactAction -> handleUpdateQuickReaction(action)
is RoomDetailAction.ExitSpecialMode -> handleExitSpecialMode(action)
is RoomDetailAction.EnterRegularMode -> handleEnterRegularMode(action)
is RoomDetailAction.EnterEditMode -> handleEditAction(action)
is RoomDetailAction.EnterQuoteMode -> handleQuoteAction(action)
is RoomDetailAction.EnterReplyMode -> handleReplyAction(action)
@ -451,46 +451,49 @@ class RoomDetailViewModel @AssistedInject constructor(
*/
private fun handleSaveDraft(action: RoomDetailAction.SaveDraft) {
withState {
when (it.sendMode) {
is SendMode.REGULAR -> room.saveDraft(UserDraft.REGULAR(action.draft), NoOpMatrixCallback())
is SendMode.REPLY -> room.saveDraft(UserDraft.REPLY(it.sendMode.timelineEvent.root.eventId!!, action.draft), NoOpMatrixCallback())
is SendMode.QUOTE -> room.saveDraft(UserDraft.QUOTE(it.sendMode.timelineEvent.root.eventId!!, action.draft), NoOpMatrixCallback())
is SendMode.EDIT -> room.saveDraft(UserDraft.EDIT(it.sendMode.timelineEvent.root.eventId!!, action.draft), NoOpMatrixCallback())
}.exhaustive
when {
it.sendMode is SendMode.REGULAR && !it.sendMode.fromSharing -> {
room.saveDraft(UserDraft.REGULAR(action.draft), NoOpMatrixCallback())
}
it.sendMode is SendMode.REPLY -> {
room.saveDraft(UserDraft.REPLY(it.sendMode.timelineEvent.root.eventId!!, action.draft), NoOpMatrixCallback())
}
it.sendMode is SendMode.QUOTE -> {
room.saveDraft(UserDraft.QUOTE(it.sendMode.timelineEvent.root.eventId!!, action.draft), NoOpMatrixCallback())
}
it.sendMode is SendMode.EDIT -> {
room.saveDraft(UserDraft.EDIT(it.sendMode.timelineEvent.root.eventId!!, action.draft), NoOpMatrixCallback())
}
}
}
}
private fun observeDrafts() {
room.rx().liveDrafts()
.subscribe {
Timber.d("Draft update --> SetState")
private fun getDraftIfAny() {
val currentDraft = room.getDraft() ?: return
setState {
val draft = it.lastOrNull() ?: UserDraft.REGULAR("")
copy(
// Create a sendMode from a draft and retrieve the TimelineEvent
sendMode = when (draft) {
is UserDraft.REGULAR -> SendMode.REGULAR(draft.text)
sendMode = when (currentDraft) {
is UserDraft.REGULAR -> SendMode.REGULAR(currentDraft.text, false)
is UserDraft.QUOTE -> {
room.getTimeLineEvent(draft.linkedEventId)?.let { timelineEvent ->
SendMode.QUOTE(timelineEvent, draft.text)
room.getTimeLineEvent(currentDraft.linkedEventId)?.let { timelineEvent ->
SendMode.QUOTE(timelineEvent, currentDraft.text)
}
}
is UserDraft.REPLY -> {
room.getTimeLineEvent(draft.linkedEventId)?.let { timelineEvent ->
SendMode.REPLY(timelineEvent, draft.text)
room.getTimeLineEvent(currentDraft.linkedEventId)?.let { timelineEvent ->
SendMode.REPLY(timelineEvent, currentDraft.text)
}
}
is UserDraft.EDIT -> {
room.getTimeLineEvent(draft.linkedEventId)?.let { timelineEvent ->
SendMode.EDIT(timelineEvent, draft.text)
room.getTimeLineEvent(currentDraft.linkedEventId)?.let { timelineEvent ->
SendMode.EDIT(timelineEvent, currentDraft.text)
}
}
} ?: SendMode.REGULAR("")
} ?: SendMode.REGULAR("", fromSharing = false)
)
}
}
.disposeOnClear()
}
private fun handleUserIsTyping(action: RoomDetailAction.UserIsTyping) {
if (vectorPreferences.sendTypingNotifs()) {
@ -740,12 +743,16 @@ class RoomDetailViewModel @AssistedInject constructor(
}
}
private fun popDraft() {
setState {
copy(sendMode = SendMode.REGULAR(""))
}
private fun popDraft() = withState {
if (it.sendMode is SendMode.REGULAR && it.sendMode.fromSharing) {
// If we were sharing, we want to get back our last value from draft
getDraftIfAny()
} else {
// Otherwise we clear the composer and remove the draft from db
setState { copy(sendMode = SendMode.REGULAR("", false)) }
room.deleteDraft(NoOpMatrixCallback())
}
}
private fun handleJoinToAnotherRoomSlashCommand(command: ParsedCommand.JoinRoom) {
session.joinRoom(command.roomAlias, command.reason, emptyList(), object : MatrixCallback<Unit> {
@ -935,9 +942,8 @@ class RoomDetailViewModel @AssistedInject constructor(
}
}
private fun handleExitSpecialMode(action: RoomDetailAction.ExitSpecialMode) = withState {
room.deleteDraft(NoOpMatrixCallback())
setState { copy(sendMode = SendMode.REGULAR(action.text)) }
private fun handleEnterRegularMode(action: RoomDetailAction.EnterRegularMode) = setState {
copy(sendMode = SendMode.REGULAR(action.text, action.fromSharing))
}
private fun handleOpenOrDownloadFile(action: RoomDetailAction.DownloadOrOpen) {

View File

@ -37,7 +37,12 @@ import org.matrix.android.sdk.api.session.widgets.model.Widget
* Depending on the state the bottom toolbar will change (icons/preview/actions...)
*/
sealed class SendMode(open val text: String) {
data class REGULAR(override val text: String) : SendMode(text)
data class REGULAR(
override val text: String,
val fromSharing: Boolean,
// This is necessary for forcing refresh on selectSubscribe
private val ts: Long = System.currentTimeMillis()
) : SendMode(text)
data class QUOTE(val timelineEvent: TimelineEvent, override val text: String) : SendMode(text)
data class EDIT(val timelineEvent: TimelineEvent, override val text: String) : SendMode(text)
data class REPLY(val timelineEvent: TimelineEvent, override val text: String) : SendMode(text)
@ -58,7 +63,7 @@ data class RoomDetailViewState(
val asyncRoomSummary: Async<RoomSummary> = Uninitialized,
val activeRoomWidgets: Async<List<Widget>> = Uninitialized,
val typingMessage: String? = null,
val sendMode: SendMode = SendMode.REGULAR(""),
val sendMode: SendMode = SendMode.REGULAR("", false),
val tombstoneEvent: Event? = null,
val tombstoneEventHandling: Async<String> = Uninitialized,
val syncState: SyncState = SyncState.Idle,