diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/Editor.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/Editor.kt index 0fe176b69c..026d4701ff 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/Editor.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/Editor.kt @@ -1,5 +1,6 @@ package com.anytypeio.anytype.presentation.editor +import com.anytypeio.anytype.core_models.Document import com.anytypeio.anytype.core_models.Id import com.anytypeio.anytype.domain.editor.Editor import com.anytypeio.anytype.domain.editor.Editor.Focus @@ -26,6 +27,7 @@ interface Editor { } class Storage { + val document: DocumentProvider = DocumentProvider.Default() val views: Store> = Store.Screen() val focus: Store = Store.Focus() val details: Store.Details = Store.Details() @@ -60,4 +62,16 @@ interface Editor { data class OnSelectionChanged(val id: Id, val selection: IntRange) : Text() } } + + interface DocumentProvider { + fun get(): Document + fun update(document: Document) + fun clear() + class Default : DocumentProvider { + var doc: Document = emptyList() + override fun get(): Document = doc + override fun update(document: Document) { this.doc = document } + override fun clear() { this.doc = emptyList() } + } + } } \ No newline at end of file diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt index 556c77bfba..b845657367 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/editor/EditorViewModel.kt @@ -201,7 +201,7 @@ class EditorViewModel( /** * Current document */ - var blocks: Document = emptyList() + val blocks: Document get() = orchestrator.stores.document.get() private val _focus: MutableLiveData = MutableLiveData() val focus: LiveData = _focus @@ -323,7 +323,7 @@ class EditorViewModel( // do nothing } } - blocks = reduce(blocks, event) + orchestrator.stores.document.update(reduce(blocks, event)) } if (BuildConfig.DEBUG) { Timber.d("Blocks after handling events: $blocks") @@ -415,12 +415,13 @@ class EditorViewModel( range = selection.second ) - blocks = blocks.map { block -> + val update = blocks.map { block -> if (block.id != target.id) block else new } + orchestrator.stores.document.update(update) refresh() @@ -436,12 +437,13 @@ class EditorViewModel( private fun rerenderingBlocks(block: Block) = viewModelScope.launch { - blocks = blocks.map { + val update = blocks.map { if (it.id != block.id) it else block } + orchestrator.stores.document.update(update) refresh() } @@ -543,12 +545,13 @@ class EditorViewModel( .onEach { if (it == pendingTextUpdate) pendingTextUpdate = null } .filterNotNull() .onEach { update -> - blocks = blocks.map { block -> + val updated = blocks.map { block -> if (block.id == update.target) { block.updateText(update) } else block } + orchestrator.stores.document.update(updated) } .map { update -> Intent.Text.UpdateText( @@ -986,7 +989,8 @@ class EditorViewModel( val block = blocks.first { it.id == target } val content = block.content() - blocks = blocks.updateTextContent(target, text, marks) + val update = blocks.updateTextContent(target, text, marks) + orchestrator.stores.document.update(update) viewModelScope.launch { orchestrator.proxies.saves.send(null) @@ -1030,10 +1034,12 @@ class EditorViewModel( marks = marks ) - blocks = blocks.replace( + val update = blocks.replace( replacement = { old -> old.copy(content = content) } ) { block -> block.id == id } + orchestrator.stores.document.update(update) + if (content.isList() || content.isToggle()) { handleEndlineEnterPressedEventForListItem(content, id) } else { @@ -1113,7 +1119,7 @@ class EditorViewModel( ) { Timber.d("onNonEmptyBlockBackspaceClicked, id:[$id] text:[$text] marks:[$marks]") - blocks = blocks.map { block -> + val update = blocks.map { block -> if (block.id == id) { block.copy( content = block.content().copy( @@ -1126,6 +1132,8 @@ class EditorViewModel( } } + orchestrator.stores.document.update(update) + viewModelScope.launch { orchestrator.proxies.saves.send(null) orchestrator.proxies.changes.send(null) @@ -1551,13 +1559,15 @@ class EditorViewModel( param = null ) - blocks = blocks.map { block -> + val update = blocks.map { block -> if (block.id != target.id) block else new } + orchestrator.stores.document.update(update) + viewModelScope.launch { refresh() } viewModelScope.launch { @@ -2009,7 +2019,7 @@ class EditorViewModel( Timber.d("onCheckboxClicked, view:[$view]") - blocks = blocks.map { block -> + val update = blocks.map { block -> if (block.id == view.id) { block.copy( content = block.content().copy( @@ -2021,6 +2031,8 @@ class EditorViewModel( } } + orchestrator.stores.document.update(update) + val store = orchestrator.stores.views viewModelScope.launch { @@ -2049,7 +2061,7 @@ class EditorViewModel( Timber.d("onTitleCheckboxClicked, view:[$view]") - blocks = blocks.map { block -> + val update = blocks.map { block -> if (block.id == view.id) { block.copy( content = block.content().copy( @@ -2061,6 +2073,8 @@ class EditorViewModel( } } + orchestrator.stores.document.update(update) + val store = orchestrator.stores.views viewModelScope.launch { @@ -4177,12 +4191,14 @@ class EditorViewModel( text: String, marks: List ) { - blocks = blocks.updateTextContent( + val update = blocks.updateTextContent( target = targetId, text = text, marks = marks ) + orchestrator.stores.document.update(update) + //send new text to Middleware viewModelScope.launch { orchestrator.proxies.saves.send(null) @@ -5072,13 +5088,15 @@ class EditorViewModel( mentionTrigger = mentionTrigger ) - blocks = blocks.map { block -> + val update = blocks.map { block -> if (block.id != target.id) block else new } + orchestrator.stores.document.update(update) + viewModelScope.launch { val position = mentionFrom + name.length + 1 orchestrator.stores.focus.update(