Skip to content

Commit 600da37

Browse files
committed
Use Flows instead of suspend function to create a buildable list of strings.
1 parent 2a35d3a commit 600da37

File tree

10 files changed

+551
-400
lines changed

10 files changed

+551
-400
lines changed

step4/androidApp/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ dependencies {
4545
implementation("androidx.compose.ui:ui-tooling-preview:1.4.3")
4646
implementation("androidx.compose.foundation:foundation:1.4.3")
4747
implementation("androidx.compose.material:material:1.4.3")
48-
implementation("androidx.activity:activity-compose:1.7.1")
48+
implementation("androidx.activity:activity-compose:1.7.2")
49+
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
50+
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1")
4951
}

step4/androidApp/src/main/java/com/jetbrains/simplelogin/kotlinmultiplatformsandbox/MainActivity.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package com.jetbrains.simplelogin.kotlinmultiplatformsandbox
22

3-
import Greeting
43
import android.os.Bundle
54
import androidx.activity.ComponentActivity
65
import androidx.activity.compose.setContent
6+
import androidx.activity.viewModels
77
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.lazy.LazyColumn
9+
import androidx.compose.foundation.lazy.items
810
import androidx.compose.material.MaterialTheme
911
import androidx.compose.material.Surface
1012
import androidx.compose.material.Text
11-
import androidx.compose.runtime.*
1213
import androidx.compose.ui.Modifier
14+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1315

1416
class MainActivity : ComponentActivity() {
17+
18+
private val viewModel: MainViewModel by viewModels()
19+
1520
override fun onCreate(savedInstanceState: Bundle?) {
1621
super.onCreate(savedInstanceState)
1722
setContent {
@@ -20,15 +25,16 @@ class MainActivity : ComponentActivity() {
2025
modifier = Modifier.fillMaxSize(),
2126
color = MaterialTheme.colors.background
2227
) {
23-
var text by remember { mutableStateOf("Loading") }
24-
LaunchedEffect(true) {
25-
text = try {
26-
Greeting().greet()
27-
} catch (e: Exception) {
28-
e.localizedMessage ?: "error"
28+
val greetings = viewModel.greetingList.collectAsStateWithLifecycle()
29+
if (greetings.value.isEmpty()) {
30+
Text("Loading")
31+
} else {
32+
LazyColumn {
33+
items(greetings.value) {
34+
Text(it)
35+
}
2936
}
3037
}
31-
Text(text)
3238
}
3339
}
3440
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.jetbrains.simplelogin.kotlinmultiplatformsandbox
2+
3+
import Greeting
4+
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.viewModelScope
6+
import kotlinx.coroutines.flow.SharingStarted
7+
import kotlinx.coroutines.flow.StateFlow
8+
import kotlinx.coroutines.flow.stateIn
9+
10+
class MainViewModel : ViewModel() {
11+
12+
val greetingList: StateFlow<List<String>> = Greeting().greet().stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
13+
14+
}

0 commit comments

Comments
 (0)