Skip to content

Commit adb6021

Browse files
authored
Merge pull request #1 from kotlin-hands-on/step_4_flows
Use Flows instead of suspend function to create a buildable list of s…
2 parents 7af5ea0 + 54e512a commit adb6021

File tree

13 files changed

+689
-453
lines changed

13 files changed

+689
-453
lines changed

step4/androidApp/build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ android {
4040

4141
dependencies {
4242
implementation(project(":shared"))
43-
implementation("androidx.compose.ui:ui:1.5.3")
44-
implementation("androidx.compose.ui:ui-tooling:1.5.3")
45-
implementation("androidx.compose.ui:ui-tooling-preview:1.5.3")
46-
implementation("androidx.compose.foundation:foundation:1.5.3")
47-
implementation("androidx.compose.material:material:1.5.3")
48-
implementation("androidx.activity:activity-compose:1.8.0")
43+
implementation("androidx.compose.ui:ui:1.5.4")
44+
implementation("androidx.compose.ui:ui-tooling:1.5.4")
45+
implementation("androidx.compose.ui:ui-tooling-preview:1.5.4")
46+
implementation("androidx.compose.foundation:foundation:1.5.4")
47+
implementation("androidx.compose.material3:material3:1.1.2")
48+
implementation("androidx.activity:activity-compose:1.8.1")
49+
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
50+
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.2")
4951
}
Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,61 @@
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
7+
import androidx.compose.foundation.layout.Arrangement
8+
import androidx.compose.foundation.layout.PaddingValues
79
import androidx.compose.foundation.layout.fillMaxSize
8-
import androidx.compose.material.MaterialTheme
9-
import androidx.compose.material.Surface
10-
import androidx.compose.material.Text
11-
import androidx.compose.runtime.*
10+
import androidx.compose.foundation.lazy.LazyColumn
11+
import androidx.compose.foundation.lazy.items
12+
import androidx.compose.material3.Divider
13+
import androidx.compose.material3.MaterialTheme
14+
import androidx.compose.material3.Surface
15+
import androidx.compose.material3.Text
16+
import androidx.compose.runtime.Composable
1217
import androidx.compose.ui.Modifier
18+
import androidx.compose.ui.tooling.preview.Preview
19+
import androidx.compose.ui.unit.dp
20+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1321

1422
class MainActivity : ComponentActivity() {
23+
private val mainViewModel: MainViewModel by viewModels()
24+
1525
override fun onCreate(savedInstanceState: Bundle?) {
1626
super.onCreate(savedInstanceState)
1727
setContent {
1828
MyApplicationTheme {
1929
Surface(
2030
modifier = Modifier.fillMaxSize(),
21-
color = MaterialTheme.colors.background
31+
color = MaterialTheme.colorScheme.background
2232
) {
23-
var text by remember { mutableStateOf("Loading") }
24-
LaunchedEffect(true) {
25-
text = try {
26-
Greeting().greet()
27-
} catch (e: Exception) {
28-
e.localizedMessage ?: "error"
29-
}
30-
}
31-
Text(text)
33+
val greetings = mainViewModel.greetingList.collectAsStateWithLifecycle()
34+
GreetingView(phrases = greetings.value)
3235
}
3336
}
3437
}
3538
}
39+
}
40+
41+
@Composable
42+
fun GreetingView(phrases: List<String>) {
43+
LazyColumn(
44+
contentPadding = PaddingValues(20.dp),
45+
verticalArrangement = Arrangement.spacedBy(8.dp),
46+
) {
47+
items(phrases) { phrase ->
48+
Text(phrase)
49+
Divider()
50+
}
51+
}
52+
}
53+
54+
55+
@Preview
56+
@Composable
57+
fun DefaultPreview() {
58+
MyApplicationTheme {
59+
GreetingView(listOf("Hello, Android!"))
60+
}
3661
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.jetbrains.simplelogin.kotlinmultiplatformsandbox
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import kotlinx.coroutines.flow.MutableStateFlow
6+
import kotlinx.coroutines.flow.SharingStarted
7+
import kotlinx.coroutines.flow.StateFlow
8+
import kotlinx.coroutines.flow.stateIn
9+
import kotlinx.coroutines.flow.update
10+
import kotlinx.coroutines.launch
11+
12+
class MainViewModel : ViewModel() {
13+
private val _greetingList = MutableStateFlow<List<String>>(listOf())
14+
val greetingList: StateFlow<List<String>> get() = _greetingList
15+
16+
init {
17+
viewModelScope.launch {
18+
Greeting().greet().collect { phrase ->
19+
_greetingList.update { list -> list + phrase }
20+
}
21+
}
22+
}
23+
24+
}

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

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

33
import androidx.compose.foundation.isSystemInDarkTheme
44
import androidx.compose.foundation.shape.RoundedCornerShape
5-
import androidx.compose.material.MaterialTheme
6-
import androidx.compose.material.Shapes
7-
import androidx.compose.material.Typography
8-
import androidx.compose.material.darkColors
9-
import androidx.compose.material.lightColors
5+
import androidx.compose.material3.MaterialTheme
6+
import androidx.compose.material3.Shapes
7+
import androidx.compose.material3.Typography
8+
import androidx.compose.material3.darkColorScheme
9+
import androidx.compose.material3.lightColorScheme
1010
import androidx.compose.runtime.Composable
1111
import androidx.compose.ui.graphics.Color
1212
import androidx.compose.ui.text.TextStyle
@@ -21,20 +21,18 @@ fun MyApplicationTheme(
2121
content: @Composable () -> Unit
2222
) {
2323
val colors = if (darkTheme) {
24-
darkColors(
24+
darkColorScheme(
2525
primary = Color(0xFFBB86FC),
26-
primaryVariant = Color(0xFF3700B3),
2726
secondary = Color(0xFF03DAC5)
2827
)
2928
} else {
30-
lightColors(
29+
lightColorScheme(
3130
primary = Color(0xFF6200EE),
32-
primaryVariant = Color(0xFF3700B3),
3331
secondary = Color(0xFF03DAC5)
3432
)
3533
}
3634
val typography = Typography(
37-
body1 = TextStyle(
35+
bodyLarge = TextStyle(
3836
fontFamily = FontFamily.Default,
3937
fontWeight = FontWeight.Normal,
4038
fontSize = 16.sp
@@ -47,7 +45,7 @@ fun MyApplicationTheme(
4745
)
4846

4947
MaterialTheme(
50-
colors = colors,
48+
colorScheme = colors,
5149
typography = typography,
5250
shapes = shapes,
5351
content = content

step4/build.gradle.kts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
plugins {
22
//trick: for the same plugin versions in all sub-modules
3-
id("com.android.application").version("8.1.2").apply(false)
4-
id("com.android.library").version("8.1.2").apply(false)
3+
id("com.android.application").version("8.1.4").apply(false)
4+
id("com.android.library").version("8.1.4").apply(false)
55
kotlin("android").version("1.9.20-Beta2").apply(false)
66
kotlin("multiplatform").version("1.9.20-Beta2").apply(false)
7-
}
7+
kotlin("plugin.serialization").version("1.9.20-Beta2").apply(false)
8+
id("com.google.devtools.ksp").version("1.9.10-1.0.13").apply(false)
9+
id("com.rickclephas.kmp.nativecoroutines").version("1.0.0-ALPHA-18").apply(false)
10+
}
11+
12+
tasks.register("clean", Delete::class) {
13+
delete(rootProject.buildDir)
14+
}

0 commit comments

Comments
 (0)