Skip to content

Commit a110626

Browse files
authored
General improvements (#68)
* nav animation improvements * general improvements
1 parent d90e709 commit a110626

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

shared/src/commonMain/kotlin/me/sujanpoudel/playdeals/common/navigation/NavHost.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun NavHost(navGraph: NavGraph) {
3434
currentEntry?.also { entry ->
3535
AnimatedContent(entry, transitionSpec = navigator.transitionSpec) {
3636
savableStateHolder.SaveableStateProvider(it.id) {
37-
entry.destination.content()
37+
it.destination.content()
3838
}
3939
}
4040
}

shared/src/commonMain/kotlin/me/sujanpoudel/playdeals/common/ui/screens/home/HomeScreenViewModel.kt

+9-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package me.sujanpoudel.playdeals.common.ui.screens.home
22

33
import io.ktor.util.reflect.instanceOf
44
import kotlinx.coroutines.delay
5-
import kotlinx.coroutines.flow.MutableStateFlow
6-
import kotlinx.coroutines.flow.StateFlow
75
import kotlinx.coroutines.flow.combine
8-
import kotlinx.coroutines.flow.update
96
import kotlinx.coroutines.launch
107
import me.sujanpoudel.playdeals.common.BuildKonfig
118
import me.sujanpoudel.playdeals.common.Screens
@@ -27,9 +24,7 @@ class HomeScreenViewModel(
2724
private val forexRepository: ForexRepository,
2825
) : ViewModel() {
2926

30-
private val _state = MutableStateFlow(HomeScreenState(lastUpdatedTime = appPreferences.lastUpdatedTime.value))
31-
val state = _state as StateFlow<HomeScreenState>
32-
27+
val state = state(HomeScreenState(lastUpdatedTime = appPreferences.lastUpdatedTime.value))
3328
val searchTerm = state("")
3429

3530
init {
@@ -51,7 +46,7 @@ class HomeScreenViewModel(
5146
}
5247
}
5348
.collect { deals ->
54-
_state.update { state ->
49+
state.update { state ->
5550
state.copy(
5651
persistentError = if (deals.isNotEmpty()) null else state.persistentError,
5752
errorOneOff = if (deals.isNotEmpty()) state.persistentError else null,
@@ -68,17 +63,15 @@ class HomeScreenViewModel(
6863
if (BuildKonfig.MAJOR_RELEASE && appPreferences.getChangelogShownVersion() != BuildKonfig.VERSION_CODE) {
6964
viewModelScope.launch {
7065
delay(1000)
71-
_state.update {
72-
it.copy(
73-
destinationOneOff = Screens.CHANGELOG,
74-
)
66+
state.update {
67+
it.copy(destinationOneOff = Screens.CHANGELOG)
7568
}
7669
}
7770
}
7871
}
7972

8073
fun refreshDeals() {
81-
_state.update { state ->
74+
state.update { state ->
8275
state.copy(
8376
isLoading = state.allDeals.isEmpty(),
8477
isRefreshing = state.allDeals.isNotEmpty(),
@@ -89,7 +82,7 @@ class HomeScreenViewModel(
8982

9083
viewModelScope.launch {
9184
val result = dealsRepository.refreshDeals()
92-
_state.update { state ->
85+
state.update { state ->
9386
when (result) {
9487
is Result.Error -> state.copy(
9588
isLoading = false,
@@ -116,19 +109,19 @@ class HomeScreenViewModel(
116109
}
117110

118111
fun clearErrorOneOff() {
119-
_state.update { state ->
112+
state.update { state ->
120113
state.copy(errorOneOff = null)
121114
}
122115
}
123116

124117
fun clearOneOffDestination() {
125-
_state.update { state ->
118+
state.update { state ->
126119
state.copy(destinationOneOff = null)
127120
}
128121
}
129122

130123
fun toggleFilterItem(item: DealFilterOption) {
131-
_state.update { state ->
124+
state.update { state ->
132125
state.copy(
133126
filterOptions =
134127
state.filterOptions.map {

shared/src/commonMain/kotlin/me/sujanpoudel/playdeals/common/ui/screens/settings/SettingsScreenViewModel.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.sujanpoudel.playdeals.common.ui.screens.settings
22

3-
import kotlinx.coroutines.flow.MutableStateFlow
43
import kotlinx.coroutines.flow.SharingStarted
54
import kotlinx.coroutines.flow.StateFlow
65
import kotlinx.coroutines.flow.collectLatest
@@ -28,12 +27,14 @@ class SettingsScreenViewModel(
2827
val forexRates: StateFlow<List<ForexRateEntity>> = forexRepository.forexRatesFlow()
2928
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
3029

31-
val preferredConversion = MutableStateFlow(defaultUsdConversion())
30+
val preferredConversion = state(defaultUsdConversion())
3231

3332
init {
3433
viewModelScope.launch {
3534
forexRepository.preferredConversionRateFlow()
36-
.collectLatest { preferredConversion.value = it }
35+
.collectLatest { entity ->
36+
preferredConversion.update { entity }
37+
}
3738
}
3839
}
3940

shared/src/commonMain/kotlin/me/sujanpoudel/playdeals/common/viewModel/ViewModel.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package me.sujanpoudel.playdeals.common.viewModel
33
import kotlinx.coroutines.flow.MutableStateFlow
44
import kotlinx.coroutines.flow.StateFlow
55
import kotlinx.coroutines.flow.update
6+
import kotlin.jvm.JvmInline
67

78
@OptIn(ExperimentalStdlibApi::class)
89
open class ViewModel() : AutoCloseable {
@@ -41,11 +42,12 @@ open class ViewModel() : AutoCloseable {
4142

4243
sealed interface VMState<T> : StateFlow<T>
4344

44-
private class VMStateImpl<T> constructor(
45+
@JvmInline
46+
value class VMStateImpl<T>(
4547
private val delegate: MutableStateFlow<T>,
4648
) : MutableStateFlow<T> by delegate, VMState<T>
4749

48-
fun <T> VMState<T>.update(function: (T) -> T) {
50+
protected fun <T> VMState<T>.update(function: (T) -> T) {
4951
(this as VMStateImpl<T> as MutableStateFlow<T>).update(function)
5052
}
5153

0 commit comments

Comments
 (0)