Skip to content

Commit

Permalink
Use a StateFlow for isEditorEmpty and better rewrite flows in general
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Jul 11, 2024
1 parent 32b4d2f commit b7f8e48
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

internal class JsBridge(
Expand All @@ -47,10 +51,10 @@ internal class JsBridge(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
val editorStatusesFlow: Flow<EditorStatuses> = _editorStatusesFlow
val editorStatusesFlow: SharedFlow<EditorStatuses> = _editorStatusesFlow.asSharedFlow()

private var _isEmptyFlow: MutableSharedFlow<Boolean> = MutableSharedFlow()
var isEmptyFlow: Flow<Boolean> = _isEmptyFlow
private var _isEmptyFlow: MutableStateFlow<Boolean> = MutableStateFlow(true)
var isEmptyFlow: StateFlow<Boolean> = _isEmptyFlow.asStateFlow()

fun toggleBold() = execCommand(StatusCommand.BOLD)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.invoke
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
Expand Down Expand Up @@ -106,15 +107,15 @@ class RichHtmlEditorWebView @JvmOverloads constructor(
*
* @see subscribeToStates
*/
val editorStatusesFlow: Flow<EditorStatuses> by jsBridge::editorStatusesFlow
val editorStatusesFlow: SharedFlow<EditorStatuses> by jsBridge::editorStatusesFlow

/**
* Describes if the content of the editor is empty or not.
*
* With some user inputs, the editor could visually look empty but still have a <br> or other tags inside it. In this case
* [isEmptyFlow] will still return false, so this value might not be suited for all usages.
*/
val isEmptyFlow: Flow<Boolean> by jsBridge::isEmptyFlow
val isEmptyFlow: StateFlow<Boolean> by jsBridge::isEmptyFlow

private var htmlExportCallback: MutableList<((html: String) -> Unit)> = mutableListOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.infomaniak.lib.richhtmleditor.Justification
import com.infomaniak.lib.richhtmleditor.sample.databinding.CreateLinkTextInputBinding
import com.infomaniak.lib.richhtmleditor.sample.databinding.FragmentEditorSampleBinding
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import java.io.BufferedReader

Expand Down Expand Up @@ -65,11 +67,9 @@ class EditorSampleFragment : Fragment() {
isVisible = true
setOnFocusChangeListener { _, hasFocus -> setToolbarEnabledStatus(hasFocus) }

lifecycleScope.launch {
isEmptyFlow.collect { isEditorEmpty ->
placeholder.isVisible = isEditorEmpty
}
}
isEmptyFlow
.onEach { isEditorEmpty -> placeholder.isVisible = isEditorEmpty }
.launchIn(lifecycleScope)
}

setEditorButtonClickListeners()
Expand Down

0 comments on commit b7f8e48

Please sign in to comment.