Skip to content

Commit

Permalink
Integrate the library into the sample apps
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Oct 17, 2023
1 parent ac491d4 commit ba0078c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion samples/shared/src/commonMain/kotlin/root/MainState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import kotlin.time.Duration

data class MainState(
val referenceEpoch: Duration,
val networkEpoch: Duration,
val synchronizedEpoch: Duration?,
)
26 changes: 25 additions & 1 deletion samples/shared/src/commonMain/kotlin/root/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
package root

import com.tidal.networktime.DNSLookupStrategy
import com.tidal.networktime.NTPServer
import com.tidal.networktime.SNTPClient
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.milliseconds

class MainViewModel {
private val stateCalculator = StateCalculator()
private val referenceClock = KotlinXDateTimeSystemClock()
private val sntpClient = SNTPClient(
NTPServer("time.google.com"),
NTPServer("time.apple.com", dnsLookupStrategy = DNSLookupStrategy.IP_V6),
referenceClock = referenceClock,
)
private val stateCalculator = StateCalculator(referenceClock, sntpClient)
private val _uiState = MutableStateFlow(stateCalculator())
val uiState = _uiState.asStateFlow()

init {
sntpClient.enableSynchronization()
GlobalScope.launch {
while (true) {
_uiState.update { stateCalculator() }
delay(1.milliseconds)
}
}
}
}
13 changes: 11 additions & 2 deletions samples/shared/src/commonMain/kotlin/root/StateCalculator.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package root

internal class StateCalculator {
operator fun invoke(): MainState? = null // TODO("Transform clock data into state")
import com.tidal.networktime.ReadableClock
import com.tidal.networktime.SNTPClient

internal class StateCalculator(
private val referenceClock: ReadableClock,
private val sntpClient: SNTPClient,
) {
operator fun invoke(): MainState = MainState(
referenceEpoch = referenceClock.epochTime,
synchronizedEpoch = sntpClient.epochTime,
)
}

0 comments on commit ba0078c

Please sign in to comment.