missing api features

This commit is contained in:
2026-02-11 16:08:09 +01:00
parent e254554532
commit ef9b54b4be
3 changed files with 144 additions and 16 deletions

View File

@@ -7,7 +7,6 @@ import kotlinx.serialization.Serializable
@Serializable
data class AuthResponse(
val token: String

View File

@@ -10,6 +10,9 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.time.Duration
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
class DashboardViewModel(private val api: ApiService) : ViewModel() {
@@ -39,21 +42,38 @@ class DashboardViewModel(private val api: ApiService) : ViewModel() {
private fun startLocalTimer() {
viewModelScope.launch {
while (isActive) {
state.value?.entry?.startTime?.let { isoTime ->
val currentEntry = state.value?.entry
if (state.value?.isTracking == true && currentEntry != null) {
try {
// Assuming ISO format like "2023-10-27T10:00:00Z"
val start = Instant.parse(isoTime)
val now = Instant.now()
val seconds = Duration.between(start, now).seconds
if (seconds >= 0) {
val h = seconds / 3600
val m = (seconds % 3600) / 60
val s = seconds % 60
elapsedTime.value = String.format("%02d:%02d:%02d", h, m, s)
val startTimeStr = currentEntry.startTime
// Robust Parsing: Try Instant first, fallback to LocalDateTime assuming System Default Zone
val startInstant = try {
Instant.parse(startTimeStr)
} catch (e: Exception) {
// CHANGE: Use systemDefault() instead of UTC.
// If server sends "15:51" and it is 15:51 on your clock, this matches the timeline correctly.
LocalDateTime.parse(startTimeStr)
.atZone(java.time.ZoneId.systemDefault())
.toInstant()
}
val now = Instant.now()
var diffSeconds = Duration.between(startInstant, now).seconds
// Prevent negative time display due to small clock skews
if (diffSeconds < 0) diffSeconds = 0
val h = diffSeconds / 3600
val m = (diffSeconds % 3600) / 60
val s = diffSeconds % 60
elapsedTime.value = String.format("%02d:%02d:%02d", h, m, s)
} catch (e: Exception) {
e.printStackTrace()
elapsedTime.value = "--:--"
}
} else {
elapsedTime.value = "00:00:00"
}
delay(1000)
}
@@ -62,10 +82,10 @@ class DashboardViewModel(private val api: ApiService) : ViewModel() {
fun startActivity(activityId: String) {
viewModelScope.launch {
// Optimistic UI Update possible here
try {
api.startActivity(activityId)
// Polling will update the full state shortly
// Refresh immediately instead of waiting for the next poll
state.value = api.getStatus()
} catch (e: Exception) {
e.printStackTrace()
}
@@ -76,7 +96,7 @@ class DashboardViewModel(private val api: ApiService) : ViewModel() {
viewModelScope.launch {
try {
api.stopActivity()
// Explicitly refresh state immediately for better UX
// Refresh immediately
state.value = api.getStatus()
} catch (e: Exception) {
e.printStackTrace()
@@ -84,4 +104,3 @@ class DashboardViewModel(private val api: ApiService) : ViewModel() {
}
}
}