-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate the library into the sample apps
- Loading branch information
Showing
3 changed files
with
37 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
samples/shared/src/commonMain/kotlin/root/StateCalculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |