Skip to content

Commit 4e18332

Browse files
committed
Refactor: Use stateIn to create the state flow in MainViewModel
Converts the `StateFlow` in `MainViewModel` to be created using the `stateIn` operator. This change makes the initial `sayHello()` call declarative as part of the flow's stream initialization. Additionally, this commit updates several dependencies: - `kotlinGradlePlugin` from 2.2.20 to 2.2.21 - `androidxComposeBom` from 2025.10.01 to 2025.11.00 - `okHttp` from 5.2.1 to 5.3.0 The `build.gradle.kts` files are simplified by removing redundant version references for the `hilt`, `google-services`, and `crashlytics` plugins, now sourcing them directly from the version catalog.
1 parent f85a6ad commit 4e18332

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

app/src/main/java/com/santimattius/basic/skeleton/MainViewModel.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
55
import dagger.hilt.android.lifecycle.HiltViewModel
66
import kotlinx.coroutines.flow.MutableStateFlow
7+
import kotlinx.coroutines.flow.SharingStarted
78
import kotlinx.coroutines.flow.StateFlow
89
import kotlinx.coroutines.flow.asStateFlow
10+
import kotlinx.coroutines.flow.onStart
11+
import kotlinx.coroutines.flow.stateIn
912
import kotlinx.coroutines.flow.update
1013
import kotlinx.coroutines.launch
1114
import javax.inject.Inject
@@ -19,8 +22,13 @@ data class MainUiState(
1922
@HiltViewModel
2023
class MainViewModel @Inject constructor() : ViewModel() {
2124
private val _state = MutableStateFlow(MainUiState())
22-
val state: StateFlow<MainUiState>
23-
get() = _state.asStateFlow()
25+
val state: StateFlow<MainUiState> = _state.onStart {
26+
sayHello()
27+
}.stateIn(
28+
scope = viewModelScope,
29+
started = SharingStarted.WhileSubscribed(5000),
30+
initialValue = MainUiState()
31+
)
2432

2533
fun sayHello() {
2634
_state.update { it.copy(isLoading = true) }

gradle/libs.versions.toml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
[versions]
22
# Plugins
33
androidGradlePlugin = "8.13.0"
4-
kotlinGradlePlugin = "2.2.20"
5-
hiltGradlePlugin = "2.57.2"
4+
kotlinGradlePlugin = "2.2.21"
65

76
detektGradlePlugin = "1.23.8"
8-
googleServicePlugin = "4.4.4"
9-
crashlyticsPlugin = "3.0.6"
107
ksp = "2.3.0"
118
googleSecretsPlugin = "2.0.1"
129
automatticMeasureBuilds = "3.2.2"
1310
# Core
14-
androidxComposeBom = "2025.10.01"
11+
androidxComposeBom = "2025.11.00"
1512
activityCompose = "1.11.0"
1613

1714
coreKtx = "1.17.0"
1815
lifecycle = "2.9.4"
1916

2017
retrofit = "3.0.0"
21-
okHttp = "5.2.1"
18+
okHttp = "5.3.0"
2219
coroutine = "1.10.2"
2320
gson = "2.13.2"
2421
hilt = "2.57.2"
@@ -86,10 +83,8 @@ retrofit = ["retrofit_core", "retrofit_gson"]
8683
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
8784
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
8885
kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlinGradlePlugin" }
89-
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltGradlePlugin" }
86+
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
9087
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detektGradlePlugin" }
91-
google-services = { id = "com.google.gms.google-services", version.ref = "googleServicePlugin" }
92-
crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "crashlyticsPlugin" }
9388
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
9489
google-secrets-gradle-plugin = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "googleSecretsPlugin" }
9590
automattic-measure-builds = { id = "com.automattic.android.measure-builds", version.ref = "automatticMeasureBuilds" }

0 commit comments

Comments
 (0)