mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-07-08 19:50:03 +02:00
Add a less memory intensive timeAgo calculation
This commit is contained in:
@ -508,10 +508,8 @@ private fun TimeAgo(channelLastTime: Long?) {
|
|||||||
if (channelLastTime == null) return
|
if (channelLastTime == null) return
|
||||||
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val timeAgo by remember(channelLastTime) {
|
val timeAgo = remember(channelLastTime) {
|
||||||
derivedStateOf {
|
timeAgo(channelLastTime, context)
|
||||||
timeAgo(channelLastTime, context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Text(
|
Text(
|
||||||
text = timeAgo,
|
text = timeAgo,
|
||||||
|
@ -3,28 +3,49 @@ package com.vitorpamplona.amethyst.ui.note
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import com.vitorpamplona.amethyst.R
|
import com.vitorpamplona.amethyst.R
|
||||||
|
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
fun timeAgo(mills: Long?, context: Context): String {
|
var locale = Locale.getDefault()
|
||||||
if (mills == null) return " "
|
var yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale)
|
||||||
if (mills == 0L) return " • ${context.getString(R.string.never)}"
|
var monthFormatter = SimpleDateFormat(" • MMM dd", locale)
|
||||||
|
|
||||||
var humanReadable = DateUtils.getRelativeTimeSpanString(
|
fun timeAgo(time: Long?, context: Context): String {
|
||||||
mills * 1000,
|
if (time == null) return " "
|
||||||
System.currentTimeMillis(),
|
if (time == 0L) return " • ${context.getString(R.string.never)}"
|
||||||
DateUtils.MINUTE_IN_MILLIS,
|
|
||||||
DateUtils.FORMAT_ABBREV_ALL
|
val timeDifference = TimeUtils.now() - time
|
||||||
).toString()
|
|
||||||
if (humanReadable.startsWith("In") || humanReadable.startsWith("0")) {
|
return if (timeDifference > TimeUtils.oneYear) {
|
||||||
humanReadable = context.getString(R.string.now)
|
// Dec 12, 2022
|
||||||
|
|
||||||
|
if (locale != Locale.getDefault()) {
|
||||||
|
locale = Locale.getDefault()
|
||||||
|
yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale)
|
||||||
|
monthFormatter = SimpleDateFormat(" • MMM dd", locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
yearFormatter.format(time * 1000)
|
||||||
|
} else if (timeDifference > TimeUtils.oneMonth) {
|
||||||
|
// Dec 12
|
||||||
|
if (locale != Locale.getDefault()) {
|
||||||
|
locale = Locale.getDefault()
|
||||||
|
yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale)
|
||||||
|
monthFormatter = SimpleDateFormat(" • MMM dd", locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
monthFormatter.format(time * 1000)
|
||||||
|
} else if (timeDifference > TimeUtils.oneDay) {
|
||||||
|
// 2 days
|
||||||
|
" • " + (timeDifference / TimeUtils.oneDay).toString() + context.getString(R.string.d)
|
||||||
|
} else if (timeDifference > TimeUtils.oneHour) {
|
||||||
|
" • " + (timeDifference / TimeUtils.oneHour).toString() + context.getString(R.string.h)
|
||||||
|
} else if (timeDifference > TimeUtils.oneMinute) {
|
||||||
|
" • " + (timeDifference / TimeUtils.oneMinute).toString() + context.getString(R.string.m)
|
||||||
|
} else {
|
||||||
|
" • " + context.getString(R.string.now)
|
||||||
}
|
}
|
||||||
|
|
||||||
return " • " + humanReadable
|
|
||||||
.replace(" hr. ago", context.getString(R.string.h))
|
|
||||||
.replace(" min. ago", context.getString(R.string.m))
|
|
||||||
.replace(" days ago", context.getString(R.string.d))
|
|
||||||
.replace(" hr ago", context.getString(R.string.h))
|
|
||||||
.replace(" min ago", context.getString(R.string.m))
|
|
||||||
.replace("Yesterday", "1" + context.getString(R.string.d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun timeAgoShort(mills: Long?, stringForNow: String): String {
|
fun timeAgoShort(mills: Long?, stringForNow: String): String {
|
||||||
@ -42,19 +63,3 @@ fun timeAgoShort(mills: Long?, stringForNow: String): String {
|
|||||||
|
|
||||||
return humanReadable
|
return humanReadable
|
||||||
}
|
}
|
||||||
|
|
||||||
fun timeAgoLong(mills: Long?, context: Context): String {
|
|
||||||
if (mills == null) return " "
|
|
||||||
|
|
||||||
var humanReadable = DateUtils.getRelativeTimeSpanString(
|
|
||||||
mills * 1000,
|
|
||||||
System.currentTimeMillis(),
|
|
||||||
DateUtils.MINUTE_IN_MILLIS,
|
|
||||||
DateUtils.FORMAT_SHOW_TIME
|
|
||||||
).toString()
|
|
||||||
if (humanReadable.startsWith("In") || humanReadable.startsWith("0")) {
|
|
||||||
humanReadable = context.getString(R.string.now)
|
|
||||||
}
|
|
||||||
|
|
||||||
return humanReadable
|
|
||||||
}
|
|
||||||
|
@ -9,6 +9,8 @@ object TimeUtils {
|
|||||||
const val eightHours = 8 * oneHour
|
const val eightHours = 8 * oneHour
|
||||||
const val oneDay = 24 * oneHour
|
const val oneDay = 24 * oneHour
|
||||||
const val oneWeek = 7 * oneDay
|
const val oneWeek = 7 * oneDay
|
||||||
|
const val oneMonth = 30 * oneDay
|
||||||
|
const val oneYear = 365 * oneDay
|
||||||
|
|
||||||
fun now() = System.currentTimeMillis() / 1000
|
fun now() = System.currentTimeMillis() / 1000
|
||||||
fun oneMinuteAgo() = now() - oneMinute
|
fun oneMinuteAgo() = now() - oneMinute
|
||||||
@ -19,4 +21,4 @@ object TimeUtils {
|
|||||||
fun eightHoursAgo() = now() - eightHours
|
fun eightHoursAgo() = now() - eightHours
|
||||||
fun oneWeekAgo() = now() - oneWeek
|
fun oneWeekAgo() = now() - oneWeek
|
||||||
fun randomWithinAWeek() = System.currentTimeMillis() / 1000 - CryptoUtils.randomInt(oneWeek)
|
fun randomWithinAWeek() = System.currentTimeMillis() / 1000 - CryptoUtils.randomInt(oneWeek)
|
||||||
}
|
}
|
Reference in New Issue
Block a user