Skip to content

Commit 1e9925a

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Update NoteEditorKmpViewModel.kt (Writeopia#431)
Co-authored-by: Leandro Ferreira <[email protected]>
1 parent ff66058 commit 1e9925a

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/viewmodel/NoteEditorKmpViewModel.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import io.writeopia.ui.model.DrawState
4242
import io.writeopia.ui.utils.Spans
4343
import kotlinx.coroutines.Dispatchers
4444
import kotlinx.coroutines.ExperimentalCoroutinesApi
45+
import kotlinx.coroutines.Job
4546
import kotlinx.coroutines.delay
4647
import kotlinx.coroutines.flow.Flow
4748
import kotlinx.coroutines.flow.MutableStateFlow
@@ -101,6 +102,7 @@ class NoteEditorKmpViewModel(
101102

102103
KeyboardEvent.CANCEL -> {
103104
writeopiaManager.clearSelection()
105+
aiJob?.cancel()
104106
}
105107

106108
KeyboardEvent.UNDO -> {
@@ -173,6 +175,8 @@ class NoteEditorKmpViewModel(
173175
.map { info -> !info.isFavorite }
174176
.stateIn(viewModelScope, started = SharingStarted.Lazily, initialValue = false)
175177

178+
private var aiJob: Job? = null
179+
176180
@OptIn(ExperimentalCoroutinesApi::class)
177181
override val toDrawWithDecoration: StateFlow<DrawState> by lazy {
178182
val infoFlow = documentId.flatMapLatest {
@@ -441,7 +445,7 @@ class NoteEditorKmpViewModel(
441445
override fun askAiBySelection() {
442446
if (ollamaRepository == null) return
443447

444-
viewModelScope.launch(Dispatchers.Default) {
448+
aiJob = viewModelScope.launch(Dispatchers.Default) {
445449
PromptService.promptBySelection(writeopiaManager, ollamaRepository)
446450
}
447451
}
@@ -543,7 +547,7 @@ class NoteEditorKmpViewModel(
543547
private fun documentPrompt(promptFn: (String, String, String) -> Flow<ResultData<String>>) {
544548
if (ollamaRepository == null) return
545549

546-
viewModelScope.launch(Dispatchers.Default) {
550+
aiJob = viewModelScope.launch(Dispatchers.Default) {
547551
PromptService.documentPrompt(promptFn, writeopiaManager, ollamaRepository)
548552
}
549553
}

0 commit comments

Comments
 (0)