Skip to content

Commit

Permalink
Provide an EditorReloaded class to easily handle the editor's html re…
Browse files Browse the repository at this point in the history
…loading through config changes
  • Loading branch information
LunarX committed Jun 14, 2024
1 parent 1e35a50 commit 4459236
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.infomaniak.lib.richhtmleditor

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class EditorReloader(private val viewModelScope: CoroutineScope) {

private var needToReloadHtml: Boolean = false
private var savedHtml = MutableStateFlow<String?>(null)

suspend fun load(editor: RichHtmlEditorWebView, defaultHtml: String) {
if (needToReloadHtml) {
savedHtml.collect {
if (it == null) return@collect

resetSavedHtml()
editor.setHtml(it)
}
} else {
editor.setHtml(defaultHtml)
}

enableHtmlReload()
}

fun save(editor: RichHtmlEditorWebView) {
editor.exportHtml {
viewModelScope.launch { savedHtml.emit(it) }
}
}

private suspend fun resetSavedHtml() {
savedHtml.emit(null)
}

private fun enableHtmlReload() {
needToReloadHtml = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class EditorSampleFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?): Unit = with(binding) {
super.onViewCreated(view, savedInstanceState)

setEditorHtml()

setToolbarEnabledStatus(false)

setEditorContent()
editor.apply {
// You can add custom scripts and css such as:
// addCss(readAsset("editor_custom_css.css"))
Expand All @@ -50,20 +50,10 @@ class EditorSampleFragment : Fragment() {
observeEditorStatusUpdates()
}

private fun setEditorHtml() = with(binding) {
if (editorSampleViewModel.needToReloadHtml) {
lifecycleScope.launch {
editorSampleViewModel.savedHtml.collect {
if (it == null) return@collect

editorSampleViewModel.resetSavedHtml()
editor.setHtml(it)
}
}
} else {
editor.setHtml(readAsset("example1.html"))
private fun setEditorContent() {
lifecycleScope.launch {
editorSampleViewModel.editorReloader.load(binding.editor, readAsset("example1.html"))
}
editorSampleViewModel.enableHtmlReload()
}

private fun setEditorButtonClickListeners() = with(binding) {
Expand Down Expand Up @@ -106,9 +96,7 @@ class EditorSampleFragment : Fragment() {
}

override fun onSaveInstanceState(outState: Bundle) {
binding.editor.exportHtml {
editorSampleViewModel.saveHtml(it)
}
editorSampleViewModel.editorReloader.save(binding.editor)
super.onSaveInstanceState(outState)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,8 @@ package com.infomaniak.lib.richhtmleditor.sample

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import com.infomaniak.lib.richhtmleditor.EditorReloader

class EditorSampleViewModel : ViewModel() {
var needToReloadHtml: Boolean = false
private set
private var _savedHtml = MutableStateFlow<String?>(null)
val savedHtml: StateFlow<String?> = _savedHtml

fun saveHtml(html: String) {
viewModelScope.launch { _savedHtml.emit(html) }
}

fun enableHtmlReload() {
needToReloadHtml = true
}

fun resetSavedHtml() {
viewModelScope.launch {
_savedHtml.emit(null)
}
}
val editorReloader = EditorReloader(viewModelScope)
}

0 comments on commit 4459236

Please sign in to comment.