Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import io.writeopia.ui.model.DrawState
import io.writeopia.ui.utils.Spans
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -101,6 +102,7 @@ class NoteEditorKmpViewModel(

KeyboardEvent.CANCEL -> {
writeopiaManager.clearSelection()
aiJob?.cancel()
}

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

private var aiJob: Job? = null

@OptIn(ExperimentalCoroutinesApi::class)
override val toDrawWithDecoration: StateFlow<DrawState> by lazy {
val infoFlow = documentId.flatMapLatest {
Expand Down Expand Up @@ -441,7 +445,7 @@ class NoteEditorKmpViewModel(
override fun askAiBySelection() {
if (ollamaRepository == null) return

viewModelScope.launch(Dispatchers.Default) {
aiJob = viewModelScope.launch(Dispatchers.Default) {
PromptService.promptBySelection(writeopiaManager, ollamaRepository)
}
}
Expand Down Expand Up @@ -543,7 +547,7 @@ class NoteEditorKmpViewModel(
private fun documentPrompt(promptFn: (String, String, String) -> Flow<ResultData<String>>) {
if (ollamaRepository == null) return

viewModelScope.launch(Dispatchers.Default) {
aiJob = viewModelScope.launch(Dispatchers.Default) {
PromptService.documentPrompt(promptFn, writeopiaManager, ollamaRepository)
}
}
Expand Down