diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt index 9f1f0797f2..0b7bdc46bb 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt @@ -508,10 +508,8 @@ private fun TimeAgo(channelLastTime: Long?) { if (channelLastTime == null) return val context = LocalContext.current - val timeAgo by remember(channelLastTime) { - derivedStateOf { - timeAgo(channelLastTime, context) - } + val timeAgo = remember(channelLastTime) { + timeAgo(channelLastTime, context) } Text( text = timeAgo, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/TimeAgoFormatter.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/TimeAgoFormatter.kt index 9b3333e46d..12fa0a245f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/TimeAgoFormatter.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/TimeAgoFormatter.kt @@ -3,28 +3,49 @@ package com.vitorpamplona.amethyst.ui.note import android.content.Context import android.text.format.DateUtils 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 { - if (mills == null) return " " - if (mills == 0L) return " • ${context.getString(R.string.never)}" +var locale = Locale.getDefault() +var yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale) +var monthFormatter = SimpleDateFormat(" • MMM dd", locale) - var humanReadable = DateUtils.getRelativeTimeSpanString( - mills * 1000, - System.currentTimeMillis(), - DateUtils.MINUTE_IN_MILLIS, - DateUtils.FORMAT_ABBREV_ALL - ).toString() - if (humanReadable.startsWith("In") || humanReadable.startsWith("0")) { - humanReadable = context.getString(R.string.now) +fun timeAgo(time: Long?, context: Context): String { + if (time == null) return " " + if (time == 0L) return " • ${context.getString(R.string.never)}" + + val timeDifference = TimeUtils.now() - time + + return if (timeDifference > TimeUtils.oneYear) { + // 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 { @@ -42,19 +63,3 @@ fun timeAgoShort(mills: Long?, stringForNow: String): String { 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 -} diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/utils/TimeUtils.kt b/quartz/src/main/java/com/vitorpamplona/quartz/utils/TimeUtils.kt index f98c67f76c..9f1694463b 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/utils/TimeUtils.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/utils/TimeUtils.kt @@ -9,6 +9,8 @@ object TimeUtils { const val eightHours = 8 * oneHour const val oneDay = 24 * oneHour const val oneWeek = 7 * oneDay + const val oneMonth = 30 * oneDay + const val oneYear = 365 * oneDay fun now() = System.currentTimeMillis() / 1000 fun oneMinuteAgo() = now() - oneMinute @@ -19,4 +21,4 @@ object TimeUtils { fun eightHoursAgo() = now() - eightHours fun oneWeekAgo() = now() - oneWeek fun randomWithinAWeek() = System.currentTimeMillis() / 1000 - CryptoUtils.randomInt(oneWeek) -} +} \ No newline at end of file