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:
Andreas
2021-08-15 23:07:48 +02:00
committed by GitHub
parent 91fbccdbaa
commit 57a5862840
7 changed files with 56 additions and 1 deletions

View File

@@ -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)
}
}