Skip to content

Commit

Permalink
fix: Dishes ignoring the current selection - onEach was blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Jul 2, 2024
1 parent 0b295e7 commit 2fe1e67
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 40 deletions.
1 change: 1 addition & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand Down Expand Up @@ -34,7 +34,7 @@ class GetSelectedMenzaUC internal constructor(
suspend operator fun invoke() =
combine(
repo.getSelectedMenza(),
getMenzaList()
getMenzaList(),
) { selected, all ->
if (selected == null) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,24 @@
package cz.lastaapps.menza.features.main.ui.vm

import cz.lastaapps.api.core.domain.model.Menza
import cz.lastaapps.api.core.domain.repo.MenzaRepo
import cz.lastaapps.api.core.domain.sync.getData
import cz.lastaapps.api.main.domain.usecase.GetMenzaListUC
import cz.lastaapps.api.main.domain.usecase.GetRequestParamsUC
import cz.lastaapps.core.ui.vm.Appearing
import cz.lastaapps.core.ui.vm.StateViewModel
import cz.lastaapps.core.ui.vm.VMContext
import cz.lastaapps.core.ui.vm.VMState
import cz.lastaapps.core.util.extensions.flattenSensible
import cz.lastaapps.menza.features.main.domain.usecase.GetSelectedMenzaUC
import cz.lastaapps.menza.features.main.domain.usecase.SelectMenzaUC
import cz.lastaapps.menza.features.settings.domain.usecase.menzaorder.GetOrderedVisibleMenzaListUC
import cz.lastaapps.menza.features.settings.domain.usecase.menzaorder.IsMenzaOrderFromTopUC
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

internal class MenzaSelectionViewModel(
context: VMContext,
private val getMenzaList: GetOrderedVisibleMenzaListUC,
private val isMenzaOrderFromTop: IsMenzaOrderFromTopUC,
private val getSelectedMenza: GetSelectedMenzaUC,
private val selectMenza: SelectMenzaUC,
private val getParams: GetRequestParamsUC,
private val menzaRepo: MenzaRepo,
private val getMenzaListStupid: GetMenzaListUC,
) : StateViewModel<MenzaSelectionState>(MenzaSelectionState(), context), Appearing {
override var hasAppeared: Boolean = false

Expand All @@ -61,24 +47,10 @@ internal class MenzaSelectionViewModel(
}.launchInVM()
getMenzaList().onEach {
updateState { copy(menzaList = it) }
println("FINDME REAL data: ${it.size}")
}.launchInVM()
isMenzaOrderFromTop().onEach {
updateState { copy(fromTop = it) }
}.launchInVM()


getParams().onEach {
println("FINDME GOT params $it")
}.launchInVM()

// getParams().map { menzaRepo.getData(it) }.flattenSensible().onEach {
menzaRepo.getData(getParams()).onEach {
println("FINDME DATA in meaningful way: ${it.size}")
}.launchInVM()
getMenzaListStupid().onEach {
println("FINDME ALMOST in meaningful way: ${it.size}")
}.launchInVM()
}

fun selectMenza(menza: Menza) = launchVM {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand Down Expand Up @@ -30,7 +30,6 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

Expand Down Expand Up @@ -90,7 +89,6 @@ internal class OrderRepoImpl(
.fold(flow {emit(persistentListOf<MenzaOrder>())}) { acu, item ->
combine(acu, item) { a, i -> a.add(i) }
}
.onEach { lock.withLock {} }
.mapLatest { data ->
data.zip(list) { o, m -> m to o }
}.map { data ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,10 @@ import cz.lastaapps.menza.features.today.domain.model.TodayUserSettings
import cz.lastaapps.menza.features.today.domain.usecase.GetTodayUserSettingsUC
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

internal class DishListViewModel(
Expand All @@ -75,7 +70,7 @@ internal class DishListViewModel(
private val log = localLogger()

override fun onAppeared() = launchVM {
getSelectedMenzaUC().onEach { newMenza ->
getSelectedMenzaUC().collectLatestInVM { newMenza ->
log.i { "Registered a new: $newMenza" }

updateState {
Expand All @@ -95,7 +90,7 @@ internal class DishListViewModel(
}
}
}
}.launchInVM()
}

getUserSettingsUC().onEach {
updateState { copy(userSettings = it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@JvmInline
Expand All @@ -46,10 +47,15 @@ abstract class BaseViewModel(
launchJob(block)
}

// TODO convert to context receiver
protected fun <T> Flow<T>.collectLatestInVM(action: suspend (T) -> Unit) =
launchVM { collectLatest(action) }

protected fun launchJob(block: suspend CoroutineScope.() -> Unit) =
viewModelScope.launch(block = block)

protected fun <T> Flow<T>.launchInVM() {
viewModelScope.launch { collect() }
}
}

0 comments on commit 2fe1e67

Please sign in to comment.