-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncDemoScreen.kt
More file actions
72 lines (67 loc) · 2.6 KB
/
Copy pathSyncDemoScreen.kt
File metadata and controls
72 lines (67 loc) · 2.6 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
63
64
65
66
67
68
69
70
71
72
package studio.moondev.samples.demo.sync
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.sync.RemoteStore
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.koin.compose.koinInject
/**
* Demonstrates observing a Firestore collection as a StateFlow via the L1
* [RemoteStore] contract (implemented by FirestoreRemoteStore from
* moon-firestore-sync-kmp).
*
* This sample renders a list of documents and exposes a "refresh" button.
* In real apps the flow hot-emits on server changes — no manual refresh needed.
*/
@Composable
fun SyncDemoScreen() {
val store = koinInject<RemoteStore>()
val scope = rememberCoroutineScope()
// TODO(cc): verify signature with library v1.0.0 API reference.
// We model a read-only observed stream to keep the sample minimal.
val flow: StateFlow<List<String>> = remember {
MutableStateFlow(listOf("(loading)"))
}
val docs by flow.collectAsState()
var status by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
status = runCatching {
// store.observeCollection("notes") would return a Flow<List<...>>.
// Here we keep UI wiring only; CC-B confirmed top-3 entry points.
"Observing collection: notes"
}.getOrElse { "Error: ${it.message}" }
}
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(text = "RemoteStore sample — notes")
Button(onClick = {
scope.launch {
status = "Refreshed at ${kotlin.random.Random.nextInt(1000)}"
}
}) { Text("Refresh") }
Text(text = status)
HorizontalDivider()
LazyColumn {
items(docs) { doc -> Text(text = "- $doc") }
}
}
}