feat(manga dates): Better time formatting

This commit is contained in:
aayush262 2024-03-01 00:23:25 +05:30
parent c5cbe408c1
commit 976acd4af2
2 changed files with 46 additions and 6 deletions

View file

@ -298,10 +298,7 @@ class MangaChapterAdapter(
if (ep.date != null) { if (ep.date != null) {
binding.itemChapterDateLayout.visibility = View.VISIBLE binding.itemChapterDateLayout.visibility = View.VISIBLE
val time = Date(ep.date) binding.itemChapterDate.text = formatDate(ep.date)
val dateFormat = SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH).format(time)
binding.itemChapterDate.text =
if (dateFormat != "Jan/01/1970") "$dateFormat" else ""
} }
if (ep.scanlator != null) { if (ep.scanlator != null) {
binding.itemChapterDateLayout.visibility = View.VISIBLE binding.itemChapterDateLayout.visibility = View.VISIBLE
@ -311,6 +308,9 @@ class MangaChapterAdapter(
) else it.toString() ) else it.toString()
} }
} }
if (formatDate(ep.date) == "" || ep.scanlator == null) {
binding.itemChapterDateDivider.visibility = View.GONE
} else binding.itemChapterDateDivider.visibility = View.VISIBLE
if (ep.progress.isNullOrEmpty()) { if (ep.progress.isNullOrEmpty()) {
binding.itemChapterTitle.visibility = View.GONE binding.itemChapterTitle.visibility = View.GONE
@ -344,6 +344,33 @@ class MangaChapterAdapter(
fun updateType(t: Int) { fun updateType(t: Int) {
type = t type = t
} }
private fun formatDate(timestamp: Long?): String {
timestamp ?: return "" // Return empty string if timestamp is null
val targetDate = Date(timestamp)
if (targetDate < Date(946684800000L)) { // January 1, 2000 (who want dates before that?)
return ""
}
val currentDate = Date()
val difference = currentDate.time - targetDate.time
return when (val daysDifference = difference / (1000 * 60 * 60 * 24)) {
0L -> {
val hoursDifference = difference / (1000 * 60 * 60)
val minutesDifference = (difference / (1000 * 60)) % 60
when {
hoursDifference > 0 -> "$hoursDifference hour${if (hoursDifference > 1) "s" else ""} ago"
minutesDifference > 0 -> "$minutesDifference minute${if (minutesDifference > 1) "s" else ""} ago"
else -> "Just now"
}
}
1L -> "1 day ago"
in 2..6 -> "$daysDifference days ago"
else -> SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH).format(targetDate)
}
}
} }

View file

@ -62,12 +62,14 @@
android:layout_marginBottom="10dp" android:layout_marginBottom="10dp"
android:layout_marginEnd="100dp" android:layout_marginEnd="100dp"
android:visibility="gone" android:visibility="gone"
android:orientation="horizontal"> android:orientation="horizontal"
tools:visibility="visible">
<TextView <TextView
android:id="@+id/itemChapterDate" android:id="@+id/itemChapterDate"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:alpha="0.6" android:alpha="0.6"
android:ellipsize="end" android:ellipsize="end"
android:fontFamily="@font/poppins_bold" android:fontFamily="@font/poppins_bold"
@ -77,6 +79,17 @@
tools:visibility="visible" tools:visibility="visible"
tools:ignore="SpUsage" /> tools:ignore="SpUsage" />
<TextView
android:id="@+id/itemChapterDateDivider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:alpha="0.6"
android:fontFamily="@font/poppins_bold"
android:text="•"
android:textSize="16sp"
tools:ignore="HardcodedText,RtlSymmetry" />
<TextView <TextView
android:id="@+id/itemChapterScan" android:id="@+id/itemChapterScan"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -86,7 +99,7 @@
android:fontFamily="@font/poppins_bold" android:fontFamily="@font/poppins_bold"
android:maxLines="1" android:maxLines="1"
android:textSize="12dp" android:textSize="12dp"
tools:text="Manga" tools:text="Manga"
tools:visibility="visible" tools:visibility="visible"
tools:ignore="SpUsage" /> tools:ignore="SpUsage" />
</LinearLayout> </LinearLayout>