Skip to content

Commit

Permalink
feat(manga dates): Better time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush2622 committed Feb 29, 2024
1 parent c5cbe40 commit 976acd4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
35 changes: 31 additions & 4 deletions app/src/main/java/ani/dantotsu/media/manga/MangaChapterAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,7 @@ class MangaChapterAdapter(

if (ep.date != null) {
binding.itemChapterDateLayout.visibility = View.VISIBLE
val time = Date(ep.date)
val dateFormat = SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH).format(time)
binding.itemChapterDate.text =
if (dateFormat != "Jan/01/1970") "$dateFormat" else ""
binding.itemChapterDate.text = formatDate(ep.date)
}
if (ep.scanlator != null) {
binding.itemChapterDateLayout.visibility = View.VISIBLE
Expand All @@ -311,6 +308,9 @@ class MangaChapterAdapter(
) 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()) {
binding.itemChapterTitle.visibility = View.GONE
Expand Down Expand Up @@ -344,6 +344,33 @@ class MangaChapterAdapter(
fun updateType(t: Int) {
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)
}
}

}
17 changes: 15 additions & 2 deletions app/src/main/res/layout/item_chapter_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@
android:layout_marginBottom="10dp"
android:layout_marginEnd="100dp"
android:visibility="gone"
android:orientation="horizontal">
android:orientation="horizontal"
tools:visibility="visible">

<TextView
android:id="@+id/itemChapterDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:alpha="0.6"
android:ellipsize="end"
android:fontFamily="@font/poppins_bold"
Expand All @@ -77,6 +79,17 @@
tools:visibility="visible"
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
android:id="@+id/itemChapterScan"
android:layout_width="wrap_content"
Expand All @@ -86,7 +99,7 @@
android:fontFamily="@font/poppins_bold"
android:maxLines="1"
android:textSize="12dp"
tools:text="Manga"
tools:text="Manga"
tools:visibility="visible"
tools:ignore="SpUsage" />
</LinearLayout>
Expand Down

0 comments on commit 976acd4

Please sign in to comment.