11package dev.johnoreilly.climatetrace.viewmodel
22
3- import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState
4- import com.rickclephas.kmp.observableviewmodel.MutableStateFlow
3+ import androidx.compose.runtime.Composable
4+ import androidx.compose.runtime.LaunchedEffect
5+ import androidx.compose.runtime.getValue
6+ import androidx.compose.runtime.mutableStateOf
7+ import androidx.compose.runtime.remember
8+ import androidx.compose.runtime.setValue
9+ import app.cash.molecule.RecompositionMode
10+ import app.cash.molecule.launchMolecule
511import com.rickclephas.kmp.observableviewmodel.ViewModel
612import com.rickclephas.kmp.observableviewmodel.coroutineScope
13+ import com.rickclephas.kmp.observableviewmodel.launch
714import dev.johnoreilly.climatetrace.data.ClimateTraceRepository
815import dev.johnoreilly.climatetrace.remote.Country
916import dev.johnoreilly.climatetrace.remote.CountryAssetEmissionsInfo
1017import dev.johnoreilly.climatetrace.remote.CountryEmissionsInfo
11- import kotlinx.coroutines.flow.asStateFlow
12- import kotlinx.coroutines.launch
18+ import kotlinx.coroutines.flow.Flow
19+ import kotlinx.coroutines.flow.MutableSharedFlow
20+ import kotlinx.coroutines.flow.StateFlow
1321import org.koin.core.component.KoinComponent
1422import org.koin.core.component.inject
1523
1624
17-
1825sealed class CountryDetailsUIState {
1926 object NoCountrySelected : CountryDetailsUIState()
2027 object Loading : CountryDetailsUIState()
@@ -27,39 +34,66 @@ sealed class CountryDetailsUIState {
2734 ) : CountryDetailsUIState()
2835}
2936
37+ sealed interface CountryDetailsEvents {
38+ data class SetCountry (val country : Country ): CountryDetailsEvents
39+ data class SetYear (val year : String ): CountryDetailsEvents
40+ }
3041
3142open class CountryDetailsViewModel : ViewModel (), KoinComponent {
3243 private val climateTraceRepository: ClimateTraceRepository by inject()
3344
34- private val _viewState = MutableStateFlow <CountryDetailsUIState >(viewModelScope, CountryDetailsUIState .NoCountrySelected )
35- @NativeCoroutinesState
36- val viewState = _viewState .asStateFlow()
37-
45+ private val events = MutableSharedFlow <CountryDetailsEvents >()
3846
39- @NativeCoroutinesState
40- val selectedYear = MutableStateFlow <String >(viewModelScope, " 2022" )
41-
42- @NativeCoroutinesState
43- val selectedCountry = MutableStateFlow <Country ?>(viewModelScope, null )
47+ val viewState: StateFlow <CountryDetailsUIState > = viewModelScope.coroutineScope.launchMolecule(mode = RecompositionMode .Immediate ) {
48+ CountryDetailsPresenter (events)
49+ }
4450
4551 fun setYear (year : String ) {
46- selectedYear.value = year
47- fetchCountryDetails()
52+ viewModelScope.launch {
53+ events.emit(CountryDetailsEvents .SetYear (year))
54+ }
4855 }
4956
5057 fun setCountry (country : Country ) {
51- selectedCountry.value = country
52- fetchCountryDetails()
58+ viewModelScope.launch {
59+ events.emit(CountryDetailsEvents .SetCountry (country))
60+ }
5361 }
5462
55- fun fetchCountryDetails () {
56- selectedCountry.value?.let { country ->
57- _viewState .value = CountryDetailsUIState .Loading
58- viewModelScope.coroutineScope.launch {
59- val countryEmissionInfo = climateTraceRepository.fetchCountryEmissionsInfo(country.alpha3, selectedYear.value).firstOrNull()
60- val countryAssetEmissionsList = climateTraceRepository.fetchCountryAssetEmissionsInfo(country.alpha3)
61- _viewState .value = CountryDetailsUIState .Success (country, selectedYear.value, countryEmissionInfo, countryAssetEmissionsList)
63+ @Composable
64+ fun CountryDetailsPresenter (events : Flow <CountryDetailsEvents >): CountryDetailsUIState {
65+ var uiState by remember { mutableStateOf<CountryDetailsUIState >(CountryDetailsUIState .NoCountrySelected ) }
66+ var selectedCountry by remember { mutableStateOf<Country ?>(null ) }
67+ var selectedYear by remember { mutableStateOf<String >(" 2022" ) }
68+
69+ var countryEmissionInfo by remember { mutableStateOf<CountryEmissionsInfo ?>(null ) }
70+ var countryAssetEmissionsList by remember { mutableStateOf<List <CountryAssetEmissionsInfo >>(emptyList()) }
71+
72+ LaunchedEffect (Unit ) {
73+ events.collect { event ->
74+ when (event) {
75+ is CountryDetailsEvents .SetCountry -> {
76+ selectedCountry = event.country
77+ selectedCountry?.let { country ->
78+ uiState = CountryDetailsUIState .Loading
79+ countryEmissionInfo = climateTraceRepository.fetchCountryEmissionsInfo(country.alpha3, selectedYear).firstOrNull()
80+ countryAssetEmissionsList = climateTraceRepository.fetchCountryAssetEmissionsInfo(country.alpha3)
81+ uiState = CountryDetailsUIState .Success (country, selectedYear, countryEmissionInfo, countryAssetEmissionsList)
82+ }
83+ }
84+
85+ is CountryDetailsEvents .SetYear -> {
86+ selectedCountry?.let { country ->
87+ selectedYear = event.year
88+ countryEmissionInfo = climateTraceRepository.fetchCountryEmissionsInfo(country.alpha3, selectedYear).firstOrNull()
89+ uiState = CountryDetailsUIState .Success (country, selectedYear, countryEmissionInfo, countryAssetEmissionsList)
90+ }
91+ }
92+ }
6293 }
6394 }
95+
96+ return uiState
6497 }
6598}
99+
0 commit comments