mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor(time): Centralize time handling with kotlinx-datetime (#4545)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
da04448dee
commit
5ca2ab4695
86 changed files with 993 additions and 663 deletions
|
|
@ -26,8 +26,9 @@ import org.meshtastic.core.database.entity.asExternalModel
|
|||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.model.BootloaderOtaQuirk
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import org.meshtastic.core.model.util.TimeConstants
|
||||
import org.meshtastic.core.model.util.nowMillis
|
||||
import org.meshtastic.core.network.DeviceHardwareRemoteDataSource
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
|
@ -210,7 +211,7 @@ constructor(
|
|||
* automatically healed from newer JSON snapshots even if their timestamp is recent.
|
||||
*/
|
||||
private fun DeviceHardwareEntity.isStale(): Boolean =
|
||||
isIncomplete() || (System.currentTimeMillis() - this.lastUpdated) > CACHE_EXPIRATION_TIME_MS
|
||||
isIncomplete() || (nowMillis - this.lastUpdated) > CACHE_EXPIRATION_TIME_MS
|
||||
|
||||
private fun loadQuirks(): List<BootloaderOtaQuirk> {
|
||||
val quirks = bootloaderOtaQuirksJsonDataSource.loadBootloaderOtaQuirksFromJsonAsset()
|
||||
|
|
@ -252,6 +253,6 @@ constructor(
|
|||
}
|
||||
|
||||
companion object {
|
||||
private val CACHE_EXPIRATION_TIME_MS = TimeUnit.DAYS.toMillis(1)
|
||||
private val CACHE_EXPIRATION_TIME_MS = TimeConstants.ONE_DAY.inWholeMilliseconds
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2025 Meshtastic LLC
|
||||
* Copyright (c) 2025-2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -14,7 +14,6 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.meshtastic.core.data.repository
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
|
|
@ -26,8 +25,9 @@ import org.meshtastic.core.database.entity.FirmwareRelease
|
|||
import org.meshtastic.core.database.entity.FirmwareReleaseEntity
|
||||
import org.meshtastic.core.database.entity.FirmwareReleaseType
|
||||
import org.meshtastic.core.database.entity.asExternalModel
|
||||
import org.meshtastic.core.model.util.TimeConstants
|
||||
import org.meshtastic.core.model.util.nowMillis
|
||||
import org.meshtastic.core.network.FirmwareReleaseRemoteDataSource
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
|
@ -124,10 +124,9 @@ constructor(
|
|||
}
|
||||
|
||||
/** Extension function to check if the cached entity is stale. */
|
||||
private fun FirmwareReleaseEntity.isStale(): Boolean =
|
||||
(System.currentTimeMillis() - this.lastUpdated) > CACHE_EXPIRATION_TIME_MS
|
||||
private fun FirmwareReleaseEntity.isStale(): Boolean = (nowMillis - this.lastUpdated) > CACHE_EXPIRATION_TIME_MS
|
||||
|
||||
companion object {
|
||||
private val CACHE_EXPIRATION_TIME_MS = TimeUnit.HOURS.toMillis(1)
|
||||
private val CACHE_EXPIRATION_TIME_MS = TimeConstants.ONE_HOUR.inWholeMilliseconds
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ import kotlinx.coroutines.withContext
|
|||
import org.meshtastic.core.database.DatabaseManager
|
||||
import org.meshtastic.core.database.entity.MeshLog
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.model.util.TimeConstants
|
||||
import org.meshtastic.core.model.util.nowMillis
|
||||
import org.meshtastic.core.prefs.meshlog.MeshLogPrefs
|
||||
import org.meshtastic.proto.MeshPacket
|
||||
import org.meshtastic.proto.MyNodeInfo
|
||||
|
|
@ -126,9 +128,9 @@ constructor(
|
|||
|
||||
val cutoffTimestamp =
|
||||
if (retentionDays == MeshLogPrefs.ONE_HOUR_RETENTION_DAYS) {
|
||||
System.currentTimeMillis() - (60 * 60 * 1000L)
|
||||
nowMillis - TimeConstants.ONE_HOUR.inWholeMilliseconds
|
||||
} else {
|
||||
System.currentTimeMillis() - (retentionDays * 24 * 60 * 60 * 1000L)
|
||||
nowMillis - (retentionDays * TimeConstants.ONE_DAY.inWholeMilliseconds)
|
||||
}
|
||||
dbManager.currentDb.value.meshLogDao().deleteOlderThan(cutoffTimestamp)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import org.meshtastic.core.data.datasource.DeviceHardwareJsonDataSource
|
|||
import org.meshtastic.core.data.datasource.DeviceHardwareLocalDataSource
|
||||
import org.meshtastic.core.database.entity.DeviceHardwareEntity
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.model.util.nowMillis
|
||||
import org.meshtastic.core.network.DeviceHardwareRemoteDataSource
|
||||
|
||||
class DeviceHardwareRepositoryTest {
|
||||
|
|
@ -120,6 +121,6 @@ class DeviceHardwareRepositoryTest {
|
|||
requiresDfu = false,
|
||||
supportLevel = 0,
|
||||
tags = emptyList(),
|
||||
lastUpdated = System.currentTimeMillis(),
|
||||
lastUpdated = nowMillis,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import org.meshtastic.core.database.MeshtasticDatabase
|
|||
import org.meshtastic.core.database.dao.MeshLogDao
|
||||
import org.meshtastic.core.database.entity.MeshLog
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.model.util.nowMillis
|
||||
import org.meshtastic.core.prefs.meshlog.MeshLogPrefs
|
||||
import org.meshtastic.proto.Data
|
||||
import org.meshtastic.proto.EnvironmentMetrics
|
||||
|
|
@ -67,7 +68,7 @@ class MeshLogRepositoryTest {
|
|||
MeshLog(
|
||||
uuid = UUID.randomUUID().toString(),
|
||||
message_type = "telemetry",
|
||||
received_date = System.currentTimeMillis(),
|
||||
received_date = nowMillis,
|
||||
raw_message = "",
|
||||
fromRadio = FromRadio(packet = meshPacket),
|
||||
)
|
||||
|
|
@ -94,7 +95,7 @@ class MeshLogRepositoryTest {
|
|||
MeshLog(
|
||||
uuid = UUID.randomUUID().toString(),
|
||||
message_type = "telemetry",
|
||||
received_date = System.currentTimeMillis(),
|
||||
received_date = nowMillis,
|
||||
raw_message = "",
|
||||
fromRadio = FromRadio(packet = meshPacket),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue