forked from penguin86/luna-tracker
add previous/next event link to details dialog
The links will point to the previous/next event of the same type
This commit is contained in:
@@ -7,6 +7,62 @@ import java.util.Date
|
||||
|
||||
class DateUtils {
|
||||
companion object {
|
||||
fun formatTimeDuration(context: Context, secondsDiff: Long): String {
|
||||
var seconds = secondsDiff
|
||||
|
||||
val years = (seconds / (365 * 24 * 60 * 60F)).toLong()
|
||||
seconds -= years * (365 * 24 * 60 * 60)
|
||||
val days = (seconds / (24 * 60 * 60F)).toLong()
|
||||
seconds -= days * (24 * 60 * 60)
|
||||
val hours = (seconds / (60 * 60F)).toLong()
|
||||
seconds -= hours * (60 * 60)
|
||||
val minutes = (seconds / 60F).toLong()
|
||||
seconds -= minutes * 60
|
||||
|
||||
fun format(value1: Long, value2: Long, resIdSingular1: Int, resIdPlural1: Int, resIdSingular2: Int, resIdPlural2: Int): String {
|
||||
val builder = StringBuilder()
|
||||
if (value1 == 0L) {
|
||||
// omit
|
||||
} else if (value1 == 1L) {
|
||||
builder.append(value1)
|
||||
builder.append(" ")
|
||||
builder.append(context.getString(resIdSingular1))
|
||||
} else {
|
||||
builder.append(value1)
|
||||
builder.append(" ")
|
||||
builder.append(context.getString(resIdPlural1))
|
||||
}
|
||||
|
||||
if (value1 > 0L && value2 > 0L) {
|
||||
builder.append(", ")
|
||||
}
|
||||
|
||||
if (value2 == 0L) {
|
||||
// omit
|
||||
} else if (value2 == 1L) {
|
||||
builder.append(value2)
|
||||
builder.append(" ")
|
||||
builder.append(context.getString(resIdSingular2))
|
||||
} else {
|
||||
builder.append(value2)
|
||||
builder.append(" ")
|
||||
builder.append(context.getString(resIdPlural2))
|
||||
}
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
if (years > 0) {
|
||||
return format(years, days, R.string.year_ago, R.string.years_ago, R.string.day_ago, R.string.days_ago)
|
||||
} else if (days > 0) {
|
||||
return format(days, hours, R.string.day_ago, R.string.days_ago, R.string.hour_ago, R.string.hours_ago)
|
||||
} else if (hours > 0) {
|
||||
return format(hours, minutes, R.string.hour_ago, R.string.hours_ago, R.string.minute_ago, R.string.minutes_ago)
|
||||
} else if (minutes > 0) {
|
||||
return format(minutes, seconds, R.string.minute_ago, R.string.minute_ago, R.string.second_ago, R.string.seconds_ago)
|
||||
} else {
|
||||
return context.getString(R.string.now)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago)
|
||||
|
Reference in New Issue
Block a user