Coverage Summary for Class: HomeStoreImpl (com.stslex93.notes.feature.home.ui.store)
Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
HomeStoreImpl |
0%
(0/13)
|
0%
(0/16)
|
0%
(0/50)
|
0%
(0/231)
|
HomeStoreImpl$notes$3$1 |
0%
(0/1)
|
|
0%
(0/1)
|
0%
(0/7)
|
HomeStoreImpl$onNoteCreateClick$1 |
0%
(0/2)
|
|
0%
(0/3)
|
0%
(0/29)
|
HomeStoreImpl$special$$inlined$flatMapLatest$1 |
0%
(0/1)
|
|
HomeStoreImpl$special$$inlined$map$1 |
0%
(0/2)
|
|
HomeStoreImpl$special$$inlined$map$1$2 |
0%
(0/1)
|
|
HomeStoreImpl$special$$inlined$map$1$2$1 |
|
HomeStoreImpl$special$$inlined$map$2 |
0%
(0/2)
|
|
HomeStoreImpl$special$$inlined$map$2$2 |
0%
(0/1)
|
|
HomeStoreImpl$special$$inlined$map$2$2$1 |
|
Total |
0%
(0/23)
|
0%
(0/16)
|
0%
(0/54)
|
0%
(0/267)
|
package com.stslex93.notes.feature.home.ui.store
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.map
import com.stslex93.notes.core.ui.Extensions.addItem
import com.stslex93.notes.core.ui.Extensions.removeItem
import com.stslex93.notes.core.ui.base.store.BaseStoreImpl
import com.stslex93.notes.feature.home.domain.interactor.HomeInteractor
import com.stslex93.notes.feature.home.ui.model.Note
import com.stslex93.notes.feature.home.ui.model.toUI
import com.stslex93.notes.feature.home.ui.store.HomeStore.Action
import com.stslex93.notes.feature.home.ui.store.HomeStore.Event
import com.stslex93.notes.feature.home.ui.store.HomeStore.State
import kotlinx.collections.immutable.persistentSetOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject
class HomeStoreImpl @Inject constructor(
private val interactor: HomeInteractor
) : HomeStore, BaseStoreImpl<State, Event, Action>() {
override val initialState = State(
query = "",
selectedNotes = persistentSetOf(),
notes = ::notes
)
override val state: MutableStateFlow<State> = MutableStateFlow(initialState)
@OptIn(ExperimentalCoroutinesApi::class)
private val notes: StateFlow<PagingData<Note>>
get() = state
.map { currentState ->
currentState.query
}
.distinctUntilChanged()
.flatMapLatest(interactor::searchNotes)
.map { pagingData ->
pagingData.map { note -> note.toUI() }
}
.cachedIn(scope)
.flowOn(Dispatchers.IO)
.stateIn(
scope = scope,
started = SharingStarted.Lazily,
initialValue = PagingData.empty()
)
override fun processAction(action: Action) {
when (action) {
is Action.OnNoteClick -> onNoteClickAction(action)
is Action.OnNoteLongClick -> onNoteLongClickAction(action)
is Action.QueryInput -> onQueryInput(action)
is Action.OnNoteFloatingButtonClick -> onNoteCreateClick()
is Action.ClearSelection -> clearSelection()
is Action.OnLabelEditClick -> onLabelEditClick()
}
}
private fun onLabelEditClick() {
val selectedIds = state.value.selectedNotes
clearSelection()
sendEvent(Event.Navigation.EditLabel(selectedIds))
}
private fun clearSelection() {
updateState { currentState ->
currentState.copy(
selectedNotes = persistentSetOf(),
query = ""
)
}
}
private fun onNoteCreateClick() {
val items = state.value.selectedNotes
if (items.isNotEmpty()) {
scope.launch(Dispatchers.IO) {
interactor.deleteNotes(items.toList())
updateState { currentState ->
currentState.copy(selectedNotes = persistentSetOf())
}
}
} else {
sendEvent(Event.Navigation.CreateNote)
}
}
private fun onQueryInput(action: Action.QueryInput) {
updateState { currentState ->
currentState.copy(
query = action.query
)
}
}
private fun onNoteLongClickAction(action: Action.OnNoteLongClick) {
updateSelectedNotes(action.id)
}
private fun onNoteClickAction(action: Action.OnNoteClick) {
if (state.value.selectedNotes.isNotEmpty()) {
updateSelectedNotes(action.id)
} else {
sendEvent(
Event.Navigation.EditNote(action.id)
)
}
}
private fun updateSelectedNotes(id: Int) {
updateState { currentState ->
val selectedItems = if (currentState.selectedNotes.contains(id)) {
currentState.selectedNotes.removeItem(id)
} else {
currentState.selectedNotes.addItem(id)
}
currentState.copy(
selectedNotes = selectedItems,
)
}
}
}