-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide an EditorReloaded class to easily handle the editor's html re…
…loading through config changes
- Loading branch information
Showing
3 changed files
with
48 additions
and
39 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
rich-html-editor/src/main/java/com/infomaniak/lib/richhtmleditor/EditorReloader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters