Coverage Summary for Class: EditStoreImpl (com.stslex93.notes.feature.edit.ui.store)
Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
EditStoreImpl |
0%
(0/8)
|
0%
(0/14)
|
0%
(0/31)
|
0%
(0/157)
|
EditStoreImpl$initStore$1 |
0%
(0/2)
|
|
0%
(0/3)
|
0%
(0/9)
|
EditStoreImpl$onActionSaveNote$1 |
0%
(0/1)
|
|
0%
(0/5)
|
0%
(0/33)
|
Total |
0%
(0/11)
|
0%
(0/14)
|
0%
(0/39)
|
0%
(0/199)
|
package com.stslex93.notes.feature.edit.ui.store
import com.stslex93.notes.core.ui.base.store.BaseStoreImpl
import com.stslex93.notes.feature.edit.domain.interactor.NoteEditInteractor
import com.stslex93.notes.feature.edit.ui.model.Note
import com.stslex93.notes.feature.edit.ui.model.toDomain
import com.stslex93.notes.feature.edit.ui.model.toPresentation
import com.stslex93.notes.feature.edit.ui.store.EditStore.Action
import com.stslex93.notes.feature.edit.ui.store.EditStore.Event
import com.stslex93.notes.feature.edit.ui.store.EditStore.State
import kotlinx.collections.immutable.persistentSetOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
class EditStoreImpl @Inject constructor(
private val interactor: NoteEditInteractor
) : EditStore, BaseStoreImpl<State, Event, Action>() {
override val initialState: State = State(
note = Note(
uuid = 0,
title = "",
content = "",
labels = persistentSetOf(),
timestamp = System.currentTimeMillis()
)
)
override val state: MutableStateFlow<State> = MutableStateFlow(initialState)
override fun processAction(action: Action) {
when (action) {
is Action.Init -> initStore(action)
is Action.InputContent -> onActionInputContent(action)
is Action.InputTitle -> onActionInputTitle(action)
is Action.SaveNote -> onActionSaveNote()
}
}
private fun onActionSaveNote() {
val note = state.value.note
if (note.title.isBlank() && note.content.isBlank()) return
scope.launch(Dispatchers.IO) {
interactor.insert(
note.copy(
title = note.title.trimEnd(),
content = note.content.trimEnd()
).toDomain()
)
}
}
private fun onActionInputTitle(action: Action.InputTitle) {
updateState { currentState ->
currentState.copy(
note = currentState.note.copy(
title = action.title
)
)
}
}
private fun onActionInputContent(action: Action.InputContent) {
updateState { currentState ->
currentState.copy(
note = currentState.note.copy(
content = action.content
)
)
}
}
private fun initStore(action: Action.Init) {
if (action.isEdit.not() || action.id <= -1) return
interactor.getNote(action.id)
.onEach { note ->
updateState { currentState ->
currentState.copy(
note = note.toPresentation()
)
}
}
.flowOn(Dispatchers.IO)
.launchIn(scope)
}
}