From e7ee261680e0e2327d7e40e6d711dc6331746924 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 2 Aug 2023 12:08:28 +0300 Subject: [PATCH] Grouping history by progress --- .../history/domain/model/HistoryOrder.kt | 2 +- .../history/ui/HistoryListViewModel.kt | 29 +++++++++++++++---- app/src/main/res/values/strings.xml | 1 + 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/history/domain/model/HistoryOrder.kt b/app/src/main/kotlin/org/koitharu/kotatsu/history/domain/model/HistoryOrder.kt index 215c59c1f..dd1fcd5b9 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/history/domain/model/HistoryOrder.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/history/domain/model/HistoryOrder.kt @@ -12,5 +12,5 @@ enum class HistoryOrder( PROGRESS(R.string.progress), ALPHABETIC(R.string.by_name); - fun isGroupingSupported() = this == UPDATED || this == CREATED + fun isGroupingSupported() = this == UPDATED || this == CREATED || this == PROGRESS } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt index ff4acb3c4..a7c68d9d5 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/history/ui/HistoryListViewModel.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.plus import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.model.MangaHistory import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.ListMode import org.koitharu.kotatsu.core.prefs.observeAsFlow @@ -130,14 +131,17 @@ class HistoryListViewModel @Inject constructor( mode: ListMode, ): List { val result = ArrayList(if (grouped) (list.size * 1.4).toInt() else list.size + 1) - var prevDate: DateTimeAgo? = null + val order = sortOrder.value + var prevHeader: ListHeader? = null for ((manga, history) in list) { if (grouped) { - val date = timeAgo(history.updatedAt) - if (prevDate != date) { - result += ListHeader(date) + val header = history.header(order) + if (header != prevHeader) { + if (header != null) { + result += header + } + prevHeader = header } - prevDate = date } result += when (mode) { ListMode.LIST -> manga.toListModel(extraProvider) @@ -148,6 +152,21 @@ class HistoryListViewModel @Inject constructor( return result } + private fun MangaHistory.header(order: HistoryOrder): ListHeader? = when (order) { + HistoryOrder.UPDATED -> ListHeader(timeAgo(updatedAt)) + HistoryOrder.CREATED -> ListHeader(timeAgo(createdAt)) + HistoryOrder.PROGRESS -> ListHeader( + when (percent) { + 1f -> R.string.status_completed + in 0f..0.01f -> R.string.status_planned + in 0f..1f -> R.string.status_reading + else -> R.string.unknown + }, + ) + + HistoryOrder.ALPHABETIC -> null + } + private fun timeAgo(date: Date): DateTimeAgo { val diff = (System.currentTimeMillis() - date.time).coerceAtLeast(0L) val diffMinutes = TimeUnit.MILLISECONDS.toMinutes(diff).toInt() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4a792b729..dbe15c23f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -470,4 +470,5 @@ Show %s requires a captcha to be resolved to work properly Languages + Unknown