mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-17 06:27:29 +01:00
Use relative time in ChapterHolder (#5719)
* Use relative time in ChapterHolder Similar to how J2K does it * Use custom implementation for relative time * Changes based on review comments
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package eu.kanade.tachiyomi.util.lang
|
||||
|
||||
import android.content.Context
|
||||
import eu.kanade.tachiyomi.R
|
||||
import java.text.DateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
@@ -94,3 +96,24 @@ fun Long.toLocalCalendar(): Calendar? {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private const val MILLISECONDS_IN_DAY = 86_400_000.0
|
||||
|
||||
fun Date.toRelativeString(
|
||||
context: Context,
|
||||
range: Int = 7,
|
||||
dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT)
|
||||
): String {
|
||||
val now = Date()
|
||||
val difference = now.time - this.time
|
||||
val days = difference / MILLISECONDS_IN_DAY
|
||||
return when {
|
||||
difference < 0 -> context.getString(R.string.recently)
|
||||
difference < MILLISECONDS_IN_DAY.times(range) -> context.resources.getQuantityString(
|
||||
R.plurals.relative_time,
|
||||
days.toInt(),
|
||||
days
|
||||
)
|
||||
else -> dateFormat.format(this)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user