From 9f018f141780b45b48909e773f3c81aa4af64d8c Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sun, 8 Dec 2024 21:33:25 -0800 Subject: [PATCH] Remove the textChanges Flow --- .../spellcheck/rememberSpellCheckState.kt | 13 --------- .../ui/SpellCheckedRichTextEditor.kt | 18 ++++++++++++ .../richeditor/model/RichTextState.kt | 12 -------- .../common/richeditor/RichEditorContent.kt | 29 ++++++++++--------- 4 files changed, 34 insertions(+), 38 deletions(-) diff --git a/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/rememberSpellCheckState.kt b/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/rememberSpellCheckState.kt index 06731cda..b7250b17 100644 --- a/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/rememberSpellCheckState.kt +++ b/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/rememberSpellCheckState.kt @@ -3,12 +3,8 @@ package com.mohamedrejeb.richeditor.compose.spellcheck import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope import com.darkrockstudios.symspellkt.api.SpellChecker -import com.mohamedrejeb.richeditor.compose.spellcheck.utils.debounceUntilQuiescent import com.mohamedrejeb.richeditor.model.rememberRichTextState -import kotlinx.coroutines.launch -import kotlin.time.Duration.Companion.seconds @Composable public fun rememberSpellCheckState(spellChecker: SpellChecker?): SpellCheckState { @@ -23,14 +19,5 @@ public fun rememberSpellCheckState(spellChecker: SpellChecker?): SpellCheckState } } - val scope = rememberCoroutineScope() - LaunchedEffect(Unit) { - scope.launch { - richTextState.textChanges.debounceUntilQuiescent(1.seconds).collect { richTextState -> - state.onTextChange(richTextState) - } - } - } - return state } \ No newline at end of file diff --git a/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/ui/SpellCheckedRichTextEditor.kt b/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/ui/SpellCheckedRichTextEditor.kt index 49cc020b..1aa45b47 100644 --- a/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/ui/SpellCheckedRichTextEditor.kt +++ b/richeditor-compose-spellcheck/src/commonMain/kotlin/com/mohamedrejeb/richeditor/compose/spellcheck/ui/SpellCheckedRichTextEditor.kt @@ -5,9 +5,11 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color @@ -18,9 +20,14 @@ import com.mohamedrejeb.richeditor.compose.spellcheck.SpellCheckMenuState import com.mohamedrejeb.richeditor.compose.spellcheck.SpellCheckState import com.mohamedrejeb.richeditor.compose.spellcheck.SpellCheckTextContextMenuProvider import com.mohamedrejeb.richeditor.compose.spellcheck.rememberSpellCheckState +import com.mohamedrejeb.richeditor.compose.spellcheck.utils.debounceUntilQuiescent +import com.mohamedrejeb.richeditor.model.RichTextState import com.mohamedrejeb.richeditor.ui.BasicRichTextEditor import com.mohamedrejeb.richeditor.ui.InteractionType import com.mohamedrejeb.richeditor.ui.RichSpanClickListener +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.launch +import kotlin.time.Duration.Companion.seconds @Composable public fun SpellCheckedRichTextEditor( @@ -43,6 +50,16 @@ public fun SpellCheckedRichTextEditor( ) { val menuState by remember(spellCheckState) { mutableStateOf(SpellCheckMenuState(spellCheckState)) } + val changesFlow = MutableStateFlow(RichTextState()) + val scope = rememberCoroutineScope() + LaunchedEffect(Unit) { + scope.launch { + changesFlow.debounceUntilQuiescent(1.seconds).collect { richTextState -> + spellCheckState.onTextChange(richTextState) + } + } + } + SpellCheckTextContextMenuProvider( modifier = modifier, spellCheckMenuState = menuState, @@ -61,6 +78,7 @@ public fun SpellCheckedRichTextEditor( interactionSource = interactionSource, state = spellCheckState.richTextState, cursorBrush = cursorBrush, + onRichTextChangedListener = { changesFlow.tryEmit(it) }, onRichSpanClick = { span, range, click, type -> return@BasicRichTextEditor if (type == InteractionType.SecondaryClick || type == InteractionType.Tap) { val correction = spellCheckState.handleSpanClick(span, range, click) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt index a8e8e6f5..cf67558d 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt @@ -58,16 +58,6 @@ public class RichTextState internal constructor( internal val inlineContentMap = mutableStateMapOf() internal val usedInlineContentMapKeys = mutableSetOf() - private val _textChanges = MutableSharedFlow( - extraBufferCapacity = 1, - onBufferOverflow = BufferOverflow.DROP_OLDEST - ) - public val textChanges: SharedFlow = _textChanges - - private fun emitTextChange() { - _textChanges.tryEmit(this) - } - /** * The annotated string representing the rich text. */ @@ -1151,8 +1141,6 @@ public class RichTextState internal constructor( // Update text field value updateTextFieldValue() - - emitTextChange() } /** diff --git a/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt b/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt index 4f25f8c6..aba2afa7 100644 --- a/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt +++ b/sample/common/src/commonMain/kotlin/com/mohamedrejeb/richeditor/sample/common/richeditor/RichEditorContent.kt @@ -12,8 +12,22 @@ import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack -import androidx.compose.material3.* -import androidx.compose.runtime.* +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.unit.DpOffset @@ -29,7 +43,6 @@ import com.mohamedrejeb.richeditor.ui.InteractionType import com.mohamedrejeb.richeditor.ui.material3.OutlinedRichTextEditor import com.mohamedrejeb.richeditor.ui.material3.RichText import com.mohamedrejeb.richeditor.ui.material3.RichTextEditor -import kotlinx.coroutines.* @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -39,16 +52,6 @@ fun RichEditorContent() { val basicRichTextState = rememberRichTextState() val richTextState = rememberRichTextState() val outlinedRichTextState = rememberRichTextState() - val scope = rememberCoroutineScope() - - LaunchedEffect(Unit) { - scope.launch { - outlinedRichTextState.textChanges.collect { state -> - println("Text changed!") - println(state.toText()) - } - } - } LaunchedEffect(Unit) { richTextState.setHtml(