Skip to content

Commit 14f3905

Browse files
committed
Update ktor-all-platforms-app sample to sync service creation
1 parent 9244646 commit 14f3905

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

samples/ktor-all-platforms-app/composeApp/src/commonMain/kotlin/App.kt

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ val client by lazy {
3333

3434
@Composable
3535
fun App() {
36-
var serviceOrNull: UserService? by remember { mutableStateOf(null) }
37-
38-
LaunchedEffect(Unit) {
39-
serviceOrNull = client.rpc {
36+
val service: UserService = remember {
37+
client.rpc {
4038
url {
4139
host = DEV_SERVER_HOST
4240
port = 8080
@@ -48,53 +46,49 @@ fun App() {
4846
json()
4947
}
5048
}
51-
}.withService()
49+
}.withService<UserService>()
5250
}
5351

54-
val service = serviceOrNull // for smart casting
55-
56-
if (service != null) {
57-
var greeting by remember { mutableStateOf<String?>(null) }
58-
val news = remember { mutableStateListOf<String>() }
52+
var greeting by remember { mutableStateOf<String?>(null) }
53+
val news = remember { mutableStateListOf<String>() }
5954

60-
LaunchedEffect(service) {
61-
greeting = service.hello(
62-
"User from ${getPlatform().name} platform",
63-
UserData("Berlin", "Smith")
64-
)
65-
}
55+
LaunchedEffect(service) {
56+
greeting = service.hello(
57+
"User from ${getPlatform().name} platform",
58+
UserData("Berlin", "Smith")
59+
)
60+
}
6661

67-
LaunchedEffect(service) {
68-
service.subscribeToNews().collect { article ->
69-
news.add(article)
70-
}
62+
LaunchedEffect(service) {
63+
service.subscribeToNews().collect { article ->
64+
news.add(article)
7165
}
66+
}
7267

73-
MaterialTheme {
74-
var showIcon by remember { mutableStateOf(false) }
68+
MaterialTheme {
69+
var showIcon by remember { mutableStateOf(false) }
7570

76-
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
77-
greeting?.let {
78-
Text(it)
79-
} ?: run {
80-
Text("Establishing server connection...")
81-
}
71+
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
72+
greeting?.let {
73+
Text(it)
74+
} ?: run {
75+
Text("Establishing server connection...")
76+
}
8277

83-
news.forEach {
84-
Text("Article: $it")
85-
}
78+
news.forEach {
79+
Text("Article: $it")
80+
}
8681

87-
Button(onClick = { showIcon = !showIcon }) {
88-
Text("Click me!")
89-
}
82+
Button(onClick = { showIcon = !showIcon }) {
83+
Text("Click me!")
84+
}
9085

91-
AnimatedVisibility(showIcon) {
92-
Column(
93-
Modifier.fillMaxWidth(),
94-
horizontalAlignment = Alignment.CenterHorizontally
95-
) {
96-
Image(painterResource(Res.drawable.compose_multiplatform), null)
97-
}
86+
AnimatedVisibility(showIcon) {
87+
Column(
88+
Modifier.fillMaxWidth(),
89+
horizontalAlignment = Alignment.CenterHorizontally
90+
) {
91+
Image(painterResource(Res.drawable.compose_multiplatform), null)
9892
}
9993
}
10094
}

samples/ktor-all-platforms-app/gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
kotlin = "2.2.0"
33

4-
agp = "8.11.0-alpha07"
4+
agp = "8.11.0"
55
android-compileSdk = "36"
66
android-minSdk = "24"
77
android-targetSdk = "36"

0 commit comments

Comments
 (0)