multiplatform: better draft (#2926)

* multiplatform: better draft

* added context item to check for emptyness

* show draft in preview when chat is active

* state sync

* clear draft when sending message
This commit is contained in:
Stanislav Dmitrenko 2023-08-15 14:04:24 +03:00 committed by GitHub
parent 5a9ed86d1b
commit 21dcb3b856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 21 deletions

View File

@ -112,7 +112,7 @@ data class ComposeState(
}
val empty: Boolean
get() = message.isEmpty() && preview is ComposePreview.NoPreview
get() = message.isEmpty() && preview is ComposePreview.NoPreview && contextItem is ComposeContextItem.NoContextItem
companion object {
fun saver(): Saver<MutableState<ComposeState>, *> = Saver(
@ -283,6 +283,20 @@ fun ComposeView(
cancelledLinks.clear()
}
fun clearPrevDraft(prevChatId: String?) {
if (chatModel.draftChatId.value == prevChatId) {
chatModel.draft.value = null
chatModel.draftChatId.value = null
}
}
fun clearCurrentDraft() {
if (chatModel.draftChatId.value == chat.id) {
chatModel.draft.value = null
chatModel.draftChatId.value = null
}
}
fun clearState(live: Boolean = false) {
if (live) {
composeState.value = composeState.value.copy(inProgress = false)
@ -378,6 +392,7 @@ fun ComposeView(
if (liveMessage != null) composeState.value = cs.copy(liveMessage = null)
sending()
}
clearCurrentDraft()
if (cs.contextItem is ComposeContextItem.EditingItem) {
val ei = cs.contextItem.chatItem
@ -705,13 +720,6 @@ fun ComposeView(
}
}
fun clearCurrentDraft() {
if (chatModel.draftChatId.value == chat.id) {
chatModel.draft.value = null
chatModel.draftChatId.value = null
}
}
LaunchedEffect(rememberUpdatedState(chat.userCanSend).value) {
if (!chat.userCanSend) {
clearCurrentDraft()
@ -719,23 +727,26 @@ fun ComposeView(
}
}
DisposableEffectOnGone {
KeyChangeEffect(chatModel.chatId.value) { prevChatId ->
val cs = composeState.value
if (cs.liveMessage != null && (cs.message.isNotEmpty() || cs.liveMessage.sent)) {
sendMessage(null)
resetLinkPreview()
clearCurrentDraft()
clearPrevDraft(prevChatId)
deleteUnusedFiles()
} else if (composeState.value.inProgress) {
clearCurrentDraft()
} else if (!composeState.value.empty) {
} else if (cs.inProgress) {
clearPrevDraft(prevChatId)
} else if (!cs.empty) {
if (cs.preview is ComposePreview.VoicePreview && !cs.preview.finished) {
composeState.value = cs.copy(preview = cs.preview.copy(finished = true))
}
chatModel.draft.value = composeState.value
chatModel.draftChatId.value = chat.id
chatModel.draftChatId.value = prevChatId
composeState.value = ComposeState(useLinkPreviews = useLinkPreviews)
} else if (chatModel.draftChatId.value == chatModel.chatId.value && chatModel.draft.value != null) {
composeState.value = chatModel.draft.value ?: ComposeState(useLinkPreviews = useLinkPreviews)
} else {
clearCurrentDraft()
clearPrevDraft(prevChatId)
deleteUnusedFiles()
}
chatModel.removeLiveDummy()

View File

@ -335,15 +335,16 @@ fun DisposableEffectOnRotate(always: () -> Unit = {}, whenDispose: () -> Unit =
* */
@Composable
@NonRestartableComposable
fun KeyChangeEffect(
key1: Any?,
block: suspend CoroutineScope.() -> Unit
fun <T> KeyChangeEffect(
key1: T?,
block: suspend CoroutineScope.(prevKey: T?) -> Unit
) {
val initialKey = remember { key1 }
var prevKey by remember { mutableStateOf(key1) }
var anyChange by remember { mutableStateOf(false) }
LaunchedEffect(key1) {
if (anyChange || key1 != initialKey) {
block()
if (anyChange || key1 != prevKey) {
block(prevKey)
prevKey = key1
anyChange = true
}
}