@@ -2,10 +2,7 @@ package me.sujanpoudel.playdeals.common.ui.screens.home
2
2
3
3
import io.ktor.util.reflect.instanceOf
4
4
import kotlinx.coroutines.delay
5
- import kotlinx.coroutines.flow.MutableStateFlow
6
- import kotlinx.coroutines.flow.StateFlow
7
5
import kotlinx.coroutines.flow.combine
8
- import kotlinx.coroutines.flow.update
9
6
import kotlinx.coroutines.launch
10
7
import me.sujanpoudel.playdeals.common.BuildKonfig
11
8
import me.sujanpoudel.playdeals.common.Screens
@@ -27,9 +24,7 @@ class HomeScreenViewModel(
27
24
private val forexRepository : ForexRepository ,
28
25
) : ViewModel() {
29
26
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))
33
28
val searchTerm = state(" " )
34
29
35
30
init {
@@ -51,7 +46,7 @@ class HomeScreenViewModel(
51
46
}
52
47
}
53
48
.collect { deals ->
54
- _state .update { state ->
49
+ state .update { state ->
55
50
state.copy(
56
51
persistentError = if (deals.isNotEmpty()) null else state.persistentError,
57
52
errorOneOff = if (deals.isNotEmpty()) state.persistentError else null ,
@@ -68,17 +63,15 @@ class HomeScreenViewModel(
68
63
if (BuildKonfig .MAJOR_RELEASE && appPreferences.getChangelogShownVersion() != BuildKonfig .VERSION_CODE ) {
69
64
viewModelScope.launch {
70
65
delay(1000 )
71
- _state .update {
72
- it.copy(
73
- destinationOneOff = Screens .CHANGELOG ,
74
- )
66
+ state.update {
67
+ it.copy(destinationOneOff = Screens .CHANGELOG )
75
68
}
76
69
}
77
70
}
78
71
}
79
72
80
73
fun refreshDeals () {
81
- _state .update { state ->
74
+ state .update { state ->
82
75
state.copy(
83
76
isLoading = state.allDeals.isEmpty(),
84
77
isRefreshing = state.allDeals.isNotEmpty(),
@@ -89,7 +82,7 @@ class HomeScreenViewModel(
89
82
90
83
viewModelScope.launch {
91
84
val result = dealsRepository.refreshDeals()
92
- _state .update { state ->
85
+ state .update { state ->
93
86
when (result) {
94
87
is Result .Error -> state.copy(
95
88
isLoading = false ,
@@ -116,19 +109,19 @@ class HomeScreenViewModel(
116
109
}
117
110
118
111
fun clearErrorOneOff () {
119
- _state .update { state ->
112
+ state .update { state ->
120
113
state.copy(errorOneOff = null )
121
114
}
122
115
}
123
116
124
117
fun clearOneOffDestination () {
125
- _state .update { state ->
118
+ state .update { state ->
126
119
state.copy(destinationOneOff = null )
127
120
}
128
121
}
129
122
130
123
fun toggleFilterItem (item : DealFilterOption ) {
131
- _state .update { state ->
124
+ state .update { state ->
132
125
state.copy(
133
126
filterOptions =
134
127
state.filterOptions.map {
0 commit comments