Skip to content

Commit

Permalink
refactor: Remove Appearing, handle automatically with lifecycles
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Dec 23, 2024
1 parent 2e50012 commit 96c55ef
Show file tree
Hide file tree
Showing 39 changed files with 351 additions and 461 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ import io.ktor.client.request.post
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.http.parameters
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlin.time.Duration.Companion.milliseconds

interface StravnikWalletApi : WalletApi

Expand Down Expand Up @@ -185,19 +185,21 @@ internal class StravnikWalletApiImpl(
{
log.e(it) { "Finding regex failed" }
log.e { "The error body was:\n$this" }
return ApiError.WalletError.TotallyBroken(
"Rexex did not match:\n" +
this@processBody
).left()
return ApiError.WalletError
.TotallyBroken(
"Rexex did not match:\n" +
this@processBody,
).left()
},
)
}?.replace(',', '.')
?.replace(" ", "")
?.replace(" ", "")
?.toFloatOrNull()
?.right()
?: ApiError.WalletError.TotallyBroken(
"Failed to parse number:\n" +
this@processBody
).left()
?: ApiError.WalletError
.TotallyBroken(
"Failed to parse number:\n" +
this@processBody,
).left()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,31 @@

package cz.lastaapps.api.agata.test

import arrow.core.Either.Right
import cz.lastaapps.menza.api.agata.api.CafeteriaApiImpl
import cz.lastaapps.menza.api.agata.api.StravnikWalletApiImpl
import cz.lastaapps.menza.api.agata.data.createAgataClient
import cz.lastaapps.menza.api.agata.data.model.AgataBEConfig
import cz.lastaapps.menza.api.agata.data.model.dto.DishTypeDto
import cz.lastaapps.menza.api.agata.data.model.dto.ServingPlaceDto
import cz.lastaapps.menza.api.agata.data.model.dto.SubsystemDto
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.types.shouldBeInstanceOf
import io.ktor.client.HttpClient
import io.ktor.client.plugins.logging.LogLevel.BODY
import io.ktor.client.plugins.logging.Logging

class StravnikWalletTest : StringSpec(
{
fun api() = StravnikWalletApiImpl(
HttpClient {
install(Logging) {
level = BODY
}
},
)
class StravnikWalletTest :
StringSpec(
{
fun api() =
StravnikWalletApiImpl(
HttpClient {
install(Logging) {
level = BODY
}
},
)

"getBalance" {
val username = ""
val password = ""
api()
.getBalance(username, password)
.shouldBeRight()
}
},
)
"getBalance" {
val username = ""
val password = ""
api()
.getBalance(username, password)
.shouldBeRight()
}
},
)
2 changes: 0 additions & 2 deletions app/src/main/kotlin/cz/lastaapps/menza/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.arkivanov.decompose.defaultComponentContext
import cz.lastaapps.core.ui.vm.HandleAppear
import cz.lastaapps.menza.features.root.ui.navigation.DefaultRootComponent
import cz.lastaapps.menza.features.root.ui.navigation.RootContent
import cz.lastaapps.menza.ui.ApplyAppTheme
Expand All @@ -51,7 +50,6 @@ class MainActivity : ComponentActivity() {

setContent {
val viewModel = rootComponent.viewModel
HandleAppear(viewModel)

ApplyProviders {
ApplyAppTheme(viewModel, this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import cz.lastaapps.core.domain.error.DomainError
import cz.lastaapps.core.ui.vm.HandleAppear
import cz.lastaapps.menza.features.info.ui.vm.InfoState
import cz.lastaapps.menza.features.info.ui.vm.InfoViewModel
import cz.lastaapps.menza.features.info.ui.widgets.AddressList
Expand Down Expand Up @@ -77,7 +76,6 @@ private fun InfoEffects(
viewModel: InfoViewModel,
hostState: SnackbarHostState,
) {
HandleAppear(viewModel)
HandleError(viewModel, hostState)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ import cz.lastaapps.api.core.domain.sync.mapSync
import cz.lastaapps.api.main.domain.usecase.GetInfoUC
import cz.lastaapps.api.main.domain.usecase.SyncInfoUC
import cz.lastaapps.core.domain.error.DomainError
import cz.lastaapps.core.ui.vm.Appearing
import cz.lastaapps.core.ui.vm.ErrorHolder
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.localLogger
import cz.lastaapps.menza.features.main.domain.usecase.GetSelectedMenzaUC
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

internal class InfoViewModel(
Expand All @@ -48,38 +50,33 @@ internal class InfoViewModel(
private val getInfo: GetInfoUC,
private val syncInfo: SyncInfoUC,
) : StateViewModel<InfoState>(InfoState(), context),
Appearing,
ErrorHolder {
override var hasAppeared: Boolean = false

private val log = localLogger()

override fun onAppeared() =
launchVM {
launchVM {
getSelectedMenza().collectLatest {
log.i { "Registered a new: $it" }
override suspend fun whileSubscribed(scope: CoroutineScope) {
getSelectedMenza()
.onEach {
log.i { "Registered a new: $it" }

updateState {
copy(
selectedMenza = it.toOption(),
items = null,
)
}
syncJob?.cancel()
if (it != null) {
coroutineScope {
this.launch {
load(it, false)
}
getInfo(it).collectLatest { items ->
updateState { copy(items = items) }
}
updateState {
copy(
selectedMenza = it.toOption(),
items = null,
)
}
syncJob?.cancel()
if (it != null) {
coroutineScope {
this.launch {
load(it, false)
}
getInfo(it).collectLatest { items ->
updateState { copy(items = items) }
}
}
}
}
}
}.launchIn(scope)
}

private var syncJob: Job? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.arkivanov.decompose.extensions.compose.stack.animation.scale
import com.arkivanov.decompose.extensions.compose.stack.animation.stackAnimation
import com.arkivanov.decompose.extensions.compose.subscribeAsState
import com.arkivanov.essenty.backhandler.BackCallback
import cz.lastaapps.core.ui.vm.HandleAppear
import cz.lastaapps.menza.R
import cz.lastaapps.menza.features.info.ui.component.InfoContent
import cz.lastaapps.menza.features.main.ui.component.DrawerContent
Expand Down Expand Up @@ -77,7 +76,6 @@ internal fun MainContent(
val mainViewModel: MainViewModel = component.viewModel

val state by mainViewModel.flowState
HandleAppear(mainViewModel)
if (!state.isReady) return

val drawerInitial = if (state.selectedMenza == null) Open else Closed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import cz.lastaapps.api.core.domain.model.Menza
import cz.lastaapps.core.ui.vm.HandleAppear
import cz.lastaapps.menza.R
import cz.lastaapps.menza.features.main.ui.vm.MenzaSelectionState
import cz.lastaapps.menza.features.main.ui.vm.MenzaSelectionViewModel
Expand Down Expand Up @@ -86,7 +85,6 @@ internal fun MenzaSelectionScreen(

@Composable
private fun MenzaSelectionListEffects(viewModel: MenzaSelectionViewModel) {
HandleAppear(viewModel)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ import cz.lastaapps.api.main.domain.usecase.wallet.WalletGetBalanceUC
import cz.lastaapps.api.main.domain.usecase.wallet.WalletLogoutUC
import cz.lastaapps.api.main.domain.usecase.wallet.WalletRefreshUC
import cz.lastaapps.core.domain.error.DomainError
import cz.lastaapps.core.ui.vm.Appearing
import cz.lastaapps.core.ui.vm.ErrorHolder
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.localLogger
import cz.lastaapps.core.util.providers.LinkOpener
import cz.lastaapps.menza.features.main.domain.usecase.GetAddMoneyUrlUC
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.mapLatest

internal class AgataWalletViewModel(
Expand All @@ -48,22 +49,20 @@ internal class AgataWalletViewModel(
private val getAddMoneyUrlUC: GetAddMoneyUrlUC,
private val openLink: LinkOpener,
) : StateViewModel<AgataWalletState>(AgataWalletState(), vmContext),
Appearing,
ErrorHolder {
override var hasAppeared: Boolean = false

private val log = localLogger()

override fun onAppeared() =
launchVM {
walletGetBalanceUC()
.mapLatest { balance ->
log.i { "New balance: $balance" }
processBalance(balance)
}.launchInVM()
override suspend fun onFirstAppearance() {
launchVM { load(false) }
}

load(false)
}
override suspend fun whileSubscribed(scope: CoroutineScope) {
walletGetBalanceUC()
.mapLatest { balance ->
log.i { "New balance: $balance" }
processBalance(balance)
}.launchIn(scope)
}

private fun processBalance(balance: UserBalance?) =
if (balance != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cz.lastaapps.menza.features.main.ui.vm
import cz.lastaapps.api.core.domain.model.Menza
import cz.lastaapps.api.main.domain.usecase.SyncMenzaListUC
import cz.lastaapps.api.main.domain.usecase.wallet.WalletRefreshUC
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
Expand All @@ -31,7 +30,11 @@ import cz.lastaapps.menza.features.main.domain.usecase.GetSelectedMenzaUC
import cz.lastaapps.menza.features.main.domain.usecase.IsFlipUC
import cz.lastaapps.menza.features.settings.domain.usecase.settings.GetAppSettingsUC
import cz.lastaapps.menza.features.settings.domain.usecase.settings.GetSettingsEverOpenedUC
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

internal class MainViewModel(
context: VMContext,
Expand All @@ -42,34 +45,36 @@ internal class MainViewModel(
private val isFlip: IsFlipUC,
private val checkLowBalanceUC: CheckLowBalanceUC,
private val refreshWallet: WalletRefreshUC,
) : StateViewModel<MainState>(MainState(), context),
Appearing {
override var hasAppeared: Boolean = false
) : StateViewModel<MainState>(MainState(), context) {
override suspend fun onFirstAppearance() {
updateState { copy(isFlip = isFlip()) }
}

override fun onAppeared() {
launchVM {
override suspend fun whileSubscribed(scope: CoroutineScope) {
scope.launch {
syncMenzaListUC()
}
launchVM {
getSelectedMenza().collectLatest {
getSelectedMenza()
.onEach {
updateState { copy(selectedMenza = it, isReady = true) }
}
}
launchVM {
getSettingsOpened().collectLatest {
}.launchIn(scope)

getSettingsOpened()
.onEach {
updateState { copy(settingsViewed = it) }
}
}
updateState { copy(isFlip = isFlip()) }
launchVM {
}.launchIn(scope)

scope.launch {
refreshWallet(false)
checkLowBalanceUC().collectLatest {
updateState { copy(showLowBalance = it) }
}
}
getAppSettings().collectLatestInVM {
updateState { copy(alternativeNavigation = it.alternativeNavigation) }
}

getAppSettings()
.onEach {
updateState { copy(alternativeNavigation = it.alternativeNavigation) }
}.launchIn(scope)
}

fun dismissLowBalance() = updateState { copy(showLowBalance = false) }
Expand Down
Loading

0 comments on commit 96c55ef

Please sign in to comment.