mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: Add "now" string and update formatAgo function (#4183)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
c5c7c2b454
commit
0591e9186b
5 changed files with 37 additions and 36 deletions
|
|
@ -17,16 +17,38 @@
|
|||
package org.meshtastic.core.ui.util
|
||||
|
||||
import android.text.format.DateUtils
|
||||
import com.meshtastic.core.strings.getString
|
||||
import org.meshtastic.core.strings.Res
|
||||
import org.meshtastic.core.strings.now
|
||||
import java.lang.System.currentTimeMillis
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
fun formatAgo(lastSeenUnix: Int, currentTimeMillis: Long = System.currentTimeMillis()): String {
|
||||
val timeInMillis = lastSeenUnix * 1000L
|
||||
/**
|
||||
* Formats a given Unix timestamp (in seconds) into a relative "time ago" string.
|
||||
*
|
||||
* For durations less than a minute, it returns "now". For longer durations, it uses Android's
|
||||
* `DateUtils.getRelativeTimeSpanString` to generate a concise, localized, and abbreviated representation (e.g., "5m
|
||||
* ago", "2h ago").
|
||||
*
|
||||
* @param lastSeenUnixSeconds The Unix timestamp in seconds to be formatted.
|
||||
* @return A [String] representing the relative time that has passed.
|
||||
*/
|
||||
fun formatAgo(lastSeenUnixSeconds: Int): String {
|
||||
val lastSeenDuration = lastSeenUnixSeconds.seconds
|
||||
val currentDuration = currentTimeMillis().milliseconds
|
||||
val diff = (currentDuration - lastSeenDuration).absoluteValue
|
||||
|
||||
return DateUtils.getRelativeTimeSpanString(
|
||||
timeInMillis,
|
||||
currentTimeMillis,
|
||||
DateUtils.SECOND_IN_MILLIS,
|
||||
DateUtils.FORMAT_ABBREV_RELATIVE,
|
||||
)
|
||||
.toString()
|
||||
return if (diff < 1.minutes) {
|
||||
getString(Res.string.now)
|
||||
} else {
|
||||
DateUtils.getRelativeTimeSpanString(
|
||||
lastSeenDuration.inWholeMilliseconds,
|
||||
currentDuration.inWholeMilliseconds,
|
||||
DateUtils.MINUTE_IN_MILLIS,
|
||||
DateUtils.FORMAT_ABBREV_RELATIVE,
|
||||
)
|
||||
.toString()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue