Skip to content

Commit

Permalink
Use exact datetime for details screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Semper-Viventem committed Jan 25, 2025
1 parent 8304418 commit 0053961
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/f/cking/software/domain/model/DeviceData.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package f.cking.software.domain.model

import android.content.Context
import f.cking.software.dateTimeStringFormatLocalized
import f.cking.software.getTimePeriodStr
import java.time.format.FormatStyle

data class DeviceData(
val address: String,
Expand All @@ -25,10 +27,18 @@ data class DeviceData(
return (System.currentTimeMillis() - firstDetectTimeMs).getTimePeriodStr(context)
}

fun firstDetectionExactTime(context: Context, formatStyle: FormatStyle = FormatStyle.SHORT): String {
return firstDetectTimeMs.dateTimeStringFormatLocalized(formatStyle)
}

fun lastDetectionPeriod(context: Context): String {
return (System.currentTimeMillis() - lastDetectTimeMs).getTimePeriodStr(context)
}

fun lastDetectionExactTime(context: Context, formatStyle: FormatStyle = FormatStyle.SHORT): String {
return lastDetectTimeMs.dateTimeStringFormatLocalized(formatStyle)
}

fun hasBeenSeenTimeAgo(): Long {
return System.currentTimeMillis() - lastDetectTimeMs
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import org.osmdroid.views.overlay.simplefastpoint.SimpleFastPointOverlay
import org.osmdroid.views.overlay.simplefastpoint.SimpleFastPointOverlayOptions
import org.osmdroid.views.overlay.simplefastpoint.SimplePointTheme
import timber.log.Timber
import java.time.format.FormatStyle

@OptIn(ExperimentalMaterial3Api::class)
object DeviceDetailsScreen {
Expand Down Expand Up @@ -284,18 +285,14 @@ object DeviceDetailsScreen {
Spacer(Modifier.width(4.dp))
Text(text = deviceData.detectCount.toString())
}
Spacer(modifier = Modifier.height(8.dp))

Text(text = stringResource(R.string.device_details_first_detection), fontWeight = FontWeight.Bold)
Text(
text = stringResource(R.string.time_ago, deviceData.firstDetectionPeriod(LocalContext.current))
)
Spacer(modifier = Modifier.height(8.dp))
Text(text = stringResource(R.string.device_details_first_detection), fontWeight = FontWeight.Bold)
Text(text = deviceData.firstDetectionExactTime(LocalContext.current, formatStyle = FormatStyle.MEDIUM))

Spacer(modifier = Modifier.height(8.dp))
Text(text = stringResource(R.string.device_details_last_detection), fontWeight = FontWeight.Bold)
Text(
text = stringResource(R.string.time_ago, deviceData.lastDetectionPeriod(LocalContext.current))
)
Text(text = deviceData.lastDetectionExactTime(LocalContext.current, formatStyle = FormatStyle.MEDIUM))
Spacer(modifier = Modifier.height(16.dp))
}
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/f/cking/software/utils/ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.regex.PatternSyntaxException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
Expand Down Expand Up @@ -66,6 +67,11 @@ fun Long.dateTimeStringFormat(format: String, timeZone: ZoneId = ZoneId.systemDe
.format(DateTimeFormatter.ofPattern(format))
}

fun Long.dateTimeStringFormatLocalized(formatStyle: FormatStyle = FormatStyle.SHORT, timeZone: ZoneId = ZoneId.systemDefault()): String {
return LocalDateTime.of(toLocalDate(timeZone), toLocalTime(timeZone))
.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle))
}

fun LocalTime.dateTimeFormat(format: String): String {
return format(DateTimeFormatter.ofPattern(format))
}
Expand Down

0 comments on commit 0053961

Please sign in to comment.