Skip to content

Commit

Permalink
General improvements (#68)
Browse files Browse the repository at this point in the history
* nav animation improvements
* general improvements
  • Loading branch information
psuzn authored Jan 5, 2024
1 parent d90e709 commit a110626
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun NavHost(navGraph: NavGraph) {
currentEntry?.also { entry ->
AnimatedContent(entry, transitionSpec = navigator.transitionSpec) {
savableStateHolder.SaveableStateProvider(it.id) {
entry.destination.content()
it.destination.content()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package me.sujanpoudel.playdeals.common.ui.screens.home

import io.ktor.util.reflect.instanceOf
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import me.sujanpoudel.playdeals.common.BuildKonfig
import me.sujanpoudel.playdeals.common.Screens
Expand All @@ -27,9 +24,7 @@ class HomeScreenViewModel(
private val forexRepository: ForexRepository,
) : ViewModel() {

private val _state = MutableStateFlow(HomeScreenState(lastUpdatedTime = appPreferences.lastUpdatedTime.value))
val state = _state as StateFlow<HomeScreenState>

val state = state(HomeScreenState(lastUpdatedTime = appPreferences.lastUpdatedTime.value))
val searchTerm = state("")

init {
Expand All @@ -51,7 +46,7 @@ class HomeScreenViewModel(
}
}
.collect { deals ->
_state.update { state ->
state.update { state ->
state.copy(
persistentError = if (deals.isNotEmpty()) null else state.persistentError,
errorOneOff = if (deals.isNotEmpty()) state.persistentError else null,
Expand All @@ -68,17 +63,15 @@ class HomeScreenViewModel(
if (BuildKonfig.MAJOR_RELEASE && appPreferences.getChangelogShownVersion() != BuildKonfig.VERSION_CODE) {
viewModelScope.launch {
delay(1000)
_state.update {
it.copy(
destinationOneOff = Screens.CHANGELOG,
)
state.update {
it.copy(destinationOneOff = Screens.CHANGELOG)
}
}
}
}

fun refreshDeals() {
_state.update { state ->
state.update { state ->
state.copy(
isLoading = state.allDeals.isEmpty(),
isRefreshing = state.allDeals.isNotEmpty(),
Expand All @@ -89,7 +82,7 @@ class HomeScreenViewModel(

viewModelScope.launch {
val result = dealsRepository.refreshDeals()
_state.update { state ->
state.update { state ->
when (result) {
is Result.Error -> state.copy(
isLoading = false,
Expand All @@ -116,19 +109,19 @@ class HomeScreenViewModel(
}

fun clearErrorOneOff() {
_state.update { state ->
state.update { state ->
state.copy(errorOneOff = null)
}
}

fun clearOneOffDestination() {
_state.update { state ->
state.update { state ->
state.copy(destinationOneOff = null)
}
}

fun toggleFilterItem(item: DealFilterOption) {
_state.update { state ->
state.update { state ->
state.copy(
filterOptions =
state.filterOptions.map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.sujanpoudel.playdeals.common.ui.screens.settings

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -28,12 +27,14 @@ class SettingsScreenViewModel(
val forexRates: StateFlow<List<ForexRateEntity>> = forexRepository.forexRatesFlow()
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val preferredConversion = MutableStateFlow(defaultUsdConversion())
val preferredConversion = state(defaultUsdConversion())

init {
viewModelScope.launch {
forexRepository.preferredConversionRateFlow()
.collectLatest { preferredConversion.value = it }
.collectLatest { entity ->
preferredConversion.update { entity }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.sujanpoudel.playdeals.common.viewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlin.jvm.JvmInline

@OptIn(ExperimentalStdlibApi::class)
open class ViewModel() : AutoCloseable {
Expand Down Expand Up @@ -41,11 +42,12 @@ open class ViewModel() : AutoCloseable {

sealed interface VMState<T> : StateFlow<T>

private class VMStateImpl<T> constructor(
@JvmInline
value class VMStateImpl<T>(
private val delegate: MutableStateFlow<T>,
) : MutableStateFlow<T> by delegate, VMState<T>

fun <T> VMState<T>.update(function: (T) -> T) {
protected fun <T> VMState<T>.update(function: (T) -> T) {
(this as VMStateImpl<T> as MutableStateFlow<T>).update(function)
}

Expand Down

0 comments on commit a110626

Please sign in to comment.