Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General improvements #68

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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