Skip to content

Commit

Permalink
Remove the textChanges Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Dec 9, 2024
1 parent c057947 commit 9f018f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ public class RichTextState internal constructor(
internal val inlineContentMap = mutableStateMapOf<String, InlineTextContent>()
internal val usedInlineContentMapKeys = mutableSetOf<String>()

private val _textChanges = MutableSharedFlow<RichTextState>(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
public val textChanges: SharedFlow<RichTextState> = _textChanges

private fun emitTextChange() {
_textChanges.tryEmit(this)
}

/**
* The annotated string representing the rich text.
*/
Expand Down Expand Up @@ -1151,8 +1141,6 @@ public class RichTextState internal constructor(

// Update text field value
updateTextFieldValue()

emitTextChange()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 9f018f1

Please sign in to comment.