-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerCallScreen.kt
More file actions
62 lines (57 loc) · 2.21 KB
/
Copy pathServerCallScreen.kt
File metadata and controls
62 lines (57 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package studio.moondev.samples.demo.server
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.moondev.server.MoonServerClient
import io.moondev.server.MoonServerException
import kotlinx.coroutines.launch
import org.koin.compose.koinInject
/**
* Demonstrates calling a backend endpoint via [MoonServerClient].
*
* Loading / error states are modeled explicitly so it is obvious how to
* surface transient errors to the UI. [MoonServerException] is the only
* checked failure thrown by the client.
*/
@Composable
fun ServerCallScreen() {
val client = koinInject<MoonServerClient>()
val scope = rememberCoroutineScope()
var loading by remember { mutableStateOf(false) }
var result by remember { mutableStateOf("") }
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(text = "MoonServerClient sample")
Button(onClick = {
scope.launch {
loading = true
result = runCatching {
// TODO(cc): verify signature with library v1.0.0 API reference.
// val res: String = client.get("/health")
"OK (replace with client.get(\"/health\"))"
}.getOrElse { err ->
when (err) {
is MoonServerException -> "Server error: ${err.message}"
else -> "Unexpected error: ${err.message}"
}
}
loading = false
}
}) { Text("GET /health") }
if (loading) CircularProgressIndicator()
Text(text = result)
}
}