You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2. In the `composeApp/src/androidMain/kotlin/com/jetbrains/greeting/greetingkmp` directory,
329
+
1. In the `composeApp/src/androidMain/.../greetingkmp` directory,
340
330
create a new `MainViewModel` Kotlin class:
341
331
342
332
```kotlin
@@ -349,7 +339,7 @@ The view model will manage the data from the activity and won't disappear when t
349
339
350
340
This class extends Android's `ViewModel` class, which ensures the correct behavior regarding lifecycle and configuration changes.
351
341
352
-
3. Create a `greetingList` value of the [StateFlow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/)
342
+
2. Create a `greetingList` value of the [StateFlow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/)
353
343
type and its backing property:
354
344
355
345
```kotlin
@@ -365,7 +355,7 @@ The view model will manage the data from the activity and won't disappear when t
365
355
* `StateFlow` here extends the `Flow` interfacebut has a single value or state.
366
356
*Theprivate backing property `_greetingList` ensures that only clients of thisclasscan access the read-only `greetingList` property.
367
357
368
-
4. In the `init` function of the ViewModel, collect all the strings from the `Greeting().greet()` flow:
358
+
3. In the `init` function of the ViewModel, collect all the strings from the `Greeting().greet()` flow:
369
359
370
360
```kotlin
371
361
import androidx.lifecycle.viewModelScope
@@ -388,7 +378,7 @@ The view model will manage the data from the activity and won't disappear when t
388
378
Since the `collect()` function is suspended, the `launch` coroutine is used within the view model's scope.
389
379
This means that the launch coroutine will run only during the correct phases of the view model's lifecycle.
390
380
391
-
5. Inside the `collect` trailing lambda, update the value of `_greetingList` to append the collected `phrase` to the list of phrases in `list`:
381
+
4. Inside the `collect` trailing lambda, update the value of `_greetingList` to append the collected `phrase` to the list of phrases in `list`:
392
382
393
383
```kotlin
394
384
import kotlinx.coroutines.flow.update
@@ -418,6 +408,7 @@ The view model will manage the data from the activity and won't disappear when t
0 commit comments