|
| 1 | +package com.softartdev.kronos.sample |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.ColumnScope |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.calculateStartPadding |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.material.Button |
| 10 | +import androidx.compose.material.Checkbox |
| 11 | +import androidx.compose.material.CircularProgressIndicator |
| 12 | +import androidx.compose.material.MaterialTheme.typography |
| 13 | +import androidx.compose.material.Scaffold |
| 14 | +import androidx.compose.material.Switch |
| 15 | +import androidx.compose.material.Text |
| 16 | +import androidx.compose.material.TopAppBar |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.runtime.LaunchedEffect |
| 19 | +import androidx.compose.runtime.MutableState |
| 20 | +import androidx.compose.runtime.derivedStateOf |
| 21 | +import androidx.compose.runtime.getValue |
| 22 | +import androidx.compose.runtime.mutableStateOf |
| 23 | +import androidx.compose.runtime.remember |
| 24 | +import androidx.compose.runtime.rememberCoroutineScope |
| 25 | +import androidx.compose.runtime.setValue |
| 26 | +import androidx.compose.ui.Alignment |
| 27 | +import androidx.compose.ui.Modifier |
| 28 | +import androidx.compose.ui.platform.LocalLayoutDirection |
| 29 | +import androidx.compose.ui.unit.dp |
| 30 | +import com.softartdev.kronos.Network |
| 31 | +import io.github.aakira.napier.Napier |
| 32 | +import kotlinx.coroutines.delay |
| 33 | +import kotlinx.coroutines.launch |
| 34 | +import kotlinx.datetime.Clock |
| 35 | +import kotlinx.datetime.Instant |
| 36 | +import kotlinx.datetime.TimeZone |
| 37 | +import kotlinx.datetime.toLocalDateTime |
| 38 | +import kotlin.time.Duration |
| 39 | +import kotlin.time.Duration.Companion.seconds |
| 40 | + |
| 41 | +@Composable |
| 42 | +fun TickScreen() = Scaffold(topBar = { |
| 43 | + TopAppBar(title = { Text(text = Greeting().greet()) }) |
| 44 | +}, content = { paddingValues -> |
| 45 | + Column( |
| 46 | + modifier = Modifier.padding( |
| 47 | + vertical = paddingValues.calculateTopPadding() |
| 48 | + .coerceAtLeast(minimumValue = 8.dp), |
| 49 | + horizontal = paddingValues.calculateStartPadding(LocalLayoutDirection.current) |
| 50 | + .coerceAtLeast(minimumValue = 8.dp), |
| 51 | + ), |
| 52 | + horizontalAlignment = Alignment.Start, |
| 53 | + verticalArrangement = Arrangement.spacedBy(16.dp) |
| 54 | + ) { |
| 55 | + var networkTime: Instant? by remember { mutableStateOf(value = clockNetworkNowOrNull()) } |
| 56 | + Text(text = "Network time:", style = typography.subtitle2) |
| 57 | + Text(text = networkTime.toString(), style = typography.body1) |
| 58 | + var systemTime: Instant by remember { mutableStateOf(Clock.System.now()) } |
| 59 | + Text(text = "System time:", style = typography.subtitle2) |
| 60 | + Text( |
| 61 | + text = systemTime.toLocalDateTime(TimeZone.currentSystemDefault()).toString(), |
| 62 | + style = typography.body1 |
| 63 | + ) |
| 64 | + var diff: Duration? by remember { |
| 65 | + mutableStateOf(value = networkTime?.let { it - systemTime }) |
| 66 | + } |
| 67 | + Text(text = "Diff:", style = typography.subtitle2) |
| 68 | + Text(text = diff.toString(), style = typography.body1) |
| 69 | + var ticking by remember { mutableStateOf(false) } |
| 70 | + val synced by remember { derivedStateOf { networkTime != null } } |
| 71 | + Row( |
| 72 | + verticalAlignment = Alignment.CenterVertically, |
| 73 | + horizontalArrangement = Arrangement.spacedBy(8.dp) |
| 74 | + ) { |
| 75 | + Text(text = "Ticking: ", style = typography.subtitle1) |
| 76 | + Switch(checked = ticking, onCheckedChange = { ticking = it }) |
| 77 | + Text(text = "Synced: ", style = typography.subtitle1) |
| 78 | + Checkbox(checked = synced, onCheckedChange = null, enabled = false) |
| 79 | + } |
| 80 | + fun tick() { |
| 81 | + systemTime = Clock.System.now() |
| 82 | + networkTime = clockNetworkNowOrNull() |
| 83 | + diff = networkTime?.let { it - systemTime } |
| 84 | + Napier.d(tag = "⏳", message = "Ticking Diff: $diff") |
| 85 | + } |
| 86 | + if (ticking) { |
| 87 | + LaunchedEffect(key1 = "tick") { |
| 88 | + while (ticking) { |
| 89 | + tick() |
| 90 | + delay(1.seconds) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + val loadingState: MutableState<Boolean> = remember { mutableStateOf(false) } |
| 95 | + val coroutineScope = rememberCoroutineScope() |
| 96 | + if (loadingState.value) { |
| 97 | + CircularProgressIndicator() |
| 98 | + } else { |
| 99 | + Button(onClick = { |
| 100 | + coroutineScope.launch { |
| 101 | + loadingState.value = true |
| 102 | + clickAwaitSync() |
| 103 | + tick() |
| 104 | + loadingState.value = false |
| 105 | + } |
| 106 | + }) { |
| 107 | + Text(text = "Sync network time") |
| 108 | + } |
| 109 | + } |
| 110 | + Button(onClick = { openUrl("https://github.com/softartdev/Kronos-Multiplatform") }) { |
| 111 | + Text("Open github") |
| 112 | + } |
| 113 | + } |
| 114 | +}) |
| 115 | + |
| 116 | +private fun clockNetworkNowOrNull(): Instant? = try { |
| 117 | + Clock.Network.now() |
| 118 | +} catch (e: Exception) { |
| 119 | + Napier.e(tag = "❌", throwable = e, message = "Network time error") |
| 120 | + null |
| 121 | +} |
0 commit comments