Skip to content

Commit

Permalink
fix: History date header duplication (#1817)
Browse files Browse the repository at this point in the history
  • Loading branch information
quickdesh authored Nov 9, 2024
1 parent 8645d6f commit 7976495
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fun relativeDateText(
)
}

// For use in chapter/episode release time
@Composable
fun relativeDateTimeText(
dateEpochMillis: Long,
Expand Down Expand Up @@ -58,6 +59,7 @@ fun relativeDateText(
?: stringResource(MR.strings.not_applicable)
}

// For use in chapter/episode release time
@Composable
fun relativeDateTimeText(
localDateTime: LocalDateTime?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.tooling.preview.PreviewParameter
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.components.relativeDateText
import eu.kanade.presentation.history.anime.components.AnimeHistoryItem
import eu.kanade.presentation.theme.TachiyomiPreviewTheme
import eu.kanade.presentation.util.animateItemFastScroll
Expand All @@ -21,7 +21,7 @@ import tachiyomi.presentation.core.components.ListGroupHeader
import tachiyomi.presentation.core.components.material.Scaffold
import tachiyomi.presentation.core.screens.EmptyScreen
import tachiyomi.presentation.core.screens.LoadingScreen
import java.time.LocalDateTime
import java.time.LocalDate

@Composable
fun AnimeHistoryScreen(
Expand Down Expand Up @@ -86,7 +86,7 @@ private fun AnimeHistoryScreenContent(
is AnimeHistoryUiModel.Header -> {
ListGroupHeader(
modifier = Modifier.animateItemFastScroll(),
text = relativeDateTimeText(item.date),
text = relativeDateText(item.date),
)
}
is AnimeHistoryUiModel.Item -> {
Expand All @@ -105,7 +105,7 @@ private fun AnimeHistoryScreenContent(
}

sealed interface AnimeHistoryUiModel {
data class Header(val date: LocalDateTime) : AnimeHistoryUiModel
data class Header(val date: LocalDate) : AnimeHistoryUiModel
data class Item(val item: AnimeHistoryWithRelations) : AnimeHistoryUiModel
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import eu.kanade.tachiyomi.ui.history.anime.AnimeHistoryScreenModel
import tachiyomi.domain.entries.anime.model.AnimeCover
import tachiyomi.domain.history.anime.model.AnimeHistoryWithRelations
import java.time.Instant
import java.time.LocalDateTime
import java.time.LocalDate
import java.time.temporal.ChronoUnit
import java.util.Date
import kotlin.random.Random
Expand Down Expand Up @@ -72,10 +72,10 @@ class AnimeHistoryScreenModelStateProvider : PreviewParameterProvider<AnimeHisto
private object HistoryUiModelExamples {
val headerToday = header()
val headerTomorrow =
AnimeHistoryUiModel.Header(LocalDateTime.now().plusDays(1))
AnimeHistoryUiModel.Header(LocalDate.now().plusDays(1))

fun header(instantBuilder: (Instant) -> Instant = { it }) =
AnimeHistoryUiModel.Header(LocalDateTime.from(instantBuilder(Instant.now())))
AnimeHistoryUiModel.Header(LocalDate.from(instantBuilder(Instant.now())))

fun items() = sequence {
var count = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.tooling.preview.PreviewParameter
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.components.relativeDateText
import eu.kanade.presentation.history.manga.components.MangaHistoryItem
import eu.kanade.presentation.theme.TachiyomiPreviewTheme
import eu.kanade.presentation.util.animateItemFastScroll
Expand All @@ -21,7 +21,7 @@ import tachiyomi.presentation.core.components.ListGroupHeader
import tachiyomi.presentation.core.components.material.Scaffold
import tachiyomi.presentation.core.screens.EmptyScreen
import tachiyomi.presentation.core.screens.LoadingScreen
import java.time.LocalDateTime
import java.time.LocalDate

@Composable
fun MangaHistoryScreen(
Expand Down Expand Up @@ -86,7 +86,7 @@ private fun MangaHistoryScreenContent(
is MangaHistoryUiModel.Header -> {
ListGroupHeader(
modifier = Modifier.animateItemFastScroll(),
text = relativeDateTimeText(item.date),
text = relativeDateText(item.date),
)
}
is MangaHistoryUiModel.Item -> {
Expand All @@ -105,7 +105,7 @@ private fun MangaHistoryScreenContent(
}

sealed interface MangaHistoryUiModel {
data class Header(val date: LocalDateTime) : MangaHistoryUiModel
data class Header(val date: LocalDate) : MangaHistoryUiModel
data class Item(val item: MangaHistoryWithRelations) : MangaHistoryUiModel
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import eu.kanade.tachiyomi.ui.history.manga.MangaHistoryScreenModel
import tachiyomi.domain.entries.manga.model.MangaCover
import tachiyomi.domain.history.manga.model.MangaHistoryWithRelations
import java.time.Instant
import java.time.LocalDateTime
import java.time.LocalDate
import java.time.temporal.ChronoUnit
import java.util.Date
import kotlin.random.Random
Expand Down Expand Up @@ -72,10 +72,10 @@ class MangaHistoryScreenModelStateProvider : PreviewParameterProvider<MangaHisto
private object HistoryUiModelExamples {
val headerToday = header()
val headerTomorrow =
MangaHistoryUiModel.Header(LocalDateTime.now().plusDays(1))
MangaHistoryUiModel.Header(LocalDate.now().plusDays(1))

fun header(instantBuilder: (Instant) -> Instant = { it }) =
MangaHistoryUiModel.Header(LocalDateTime.from(instantBuilder(Instant.now())))
MangaHistoryUiModel.Header(LocalDate.from(instantBuilder(Instant.now())))

fun items() = sequence {
var count = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import eu.kanade.core.util.insertSeparators
import eu.kanade.presentation.history.anime.AnimeHistoryUiModel
import eu.kanade.tachiyomi.util.lang.toLocalDateTime
import eu.kanade.tachiyomi.util.lang.toLocalDate
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -69,8 +69,8 @@ class AnimeHistoryScreenModel(
private fun List<AnimeHistoryWithRelations>.toAnimeHistoryUiModels(): List<AnimeHistoryUiModel> {
return map { AnimeHistoryUiModel.Item(it) }
.insertSeparators { before, after ->
val beforeDate = before?.item?.seenAt?.time?.toLocalDateTime()
val afterDate = after?.item?.seenAt?.time?.toLocalDateTime()
val beforeDate = before?.item?.seenAt?.time?.toLocalDate()
val afterDate = after?.item?.seenAt?.time?.toLocalDate()
when {
beforeDate != afterDate && afterDate != null -> AnimeHistoryUiModel.Header(afterDate)
// Return null to avoid adding a separator between two items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import eu.kanade.core.util.insertSeparators
import eu.kanade.presentation.history.manga.MangaHistoryUiModel
import eu.kanade.tachiyomi.util.lang.toLocalDateTime
import eu.kanade.tachiyomi.util.lang.toLocalDate
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -69,8 +69,8 @@ class MangaHistoryScreenModel(
private fun List<MangaHistoryWithRelations>.toHistoryUiModels(): List<MangaHistoryUiModel> {
return map { MangaHistoryUiModel.Item(it) }
.insertSeparators { before, after ->
val beforeDate = before?.item?.readAt?.time?.toLocalDateTime()
val afterDate = after?.item?.readAt?.time?.toLocalDateTime()
val beforeDate = before?.item?.readAt?.time?.toLocalDate()
val afterDate = after?.item?.readAt?.time?.toLocalDate()
when {
beforeDate != afterDate && afterDate != null -> MangaHistoryUiModel.Header(afterDate)
// Return null to avoid adding a separator between two items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import tachiyomi.presentation.core.components.material.DISABLED_ALPHA
import tachiyomi.presentation.core.components.material.padding
import tachiyomi.presentation.core.i18n.stringResource
import java.time.Instant
import java.time.LocalDateTime
import java.time.LocalDate
import java.time.ZoneId
import java.time.format.DateTimeFormatter

Expand Down Expand Up @@ -94,7 +94,7 @@ fun EpisodeListDialog(
val date = episode.date_upload
.takeIf { it > 0L }
?.let {
LocalDateTime.ofInstant(
LocalDate.ofInstant(
Instant.ofEpochMilli(it),
ZoneId.systemDefault(),
).toRelativeString(
Expand Down
18 changes: 5 additions & 13 deletions app/src/main/java/eu/kanade/tachiyomi/util/lang/DateExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.kanade.tachiyomi.util.lang

import android.content.Context
import eu.kanade.tachiyomi.R
import tachiyomi.core.common.i18n.pluralStringResource
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.i18n.MR
Expand Down Expand Up @@ -40,18 +39,10 @@ fun Long.toLocalDate(): LocalDate {
return LocalDate.ofInstant(Instant.ofEpochMilli(this), ZoneId.systemDefault())
}

fun Long.toLocalDateTime(): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(this), ZoneId.systemDefault())
}

fun Instant.toLocalDate(zoneId: ZoneId = ZoneId.systemDefault()): LocalDate {
return LocalDate.ofInstant(this, zoneId)
}

fun Instant.toLocalDateTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalDateTime {
return LocalDateTime.ofInstant(this, zoneId)
}

fun LocalDate.toRelativeString(
context: Context,
relative: Boolean = true,
Expand All @@ -70,15 +61,16 @@ fun LocalDate.toRelativeString(
difference.toInt().absoluteValue,
)
difference < 1 -> context.stringResource(MR.strings.relative_time_today)
difference < 7 -> context.resources.getQuantityString(
R.plurals.relative_time,
difference < 7 -> context.pluralStringResource(
MR.plurals.relative_time,
difference.toInt(),
difference.toInt(),
)
else -> dateFormat.format(this)
}
}

// For use in chapter/episode release time
fun LocalDateTime.toRelativeString(
context: Context,
relative: Boolean = true,
Expand Down Expand Up @@ -128,8 +120,8 @@ fun LocalDateTime.toRelativeString(
)
}
}
timeDifference < 7 -> context.resources.getQuantityString(
R.plurals.relative_time,
timeDifference < 7 -> context.pluralStringResource(
MR.plurals.relative_time,
dateDifference.toInt(),
dateDifference.toInt(),
)
Expand Down

0 comments on commit 7976495

Please sign in to comment.