Skip to content

Commit

Permalink
feat: Show hours and minutes in relative time strings
Browse files Browse the repository at this point in the history
Closes #1702
  • Loading branch information
jmir1 committed Jul 16, 2024
1 parent ee13863 commit 1f3be7b
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 43 deletions.
32 changes: 32 additions & 0 deletions app/src/main/java/eu/kanade/presentation/components/DateText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId

@Composable
Expand All @@ -26,6 +27,19 @@ fun relativeDateText(
)
}

@Composable
fun relativeDateTimeText(
dateEpochMillis: Long,
): String {
return relativeDateTimeText(
localDateTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(dateEpochMillis),
ZoneId.systemDefault(),
)
.takeIf { dateEpochMillis > 0L },
)
}

@Composable
fun relativeDateText(
localDate: LocalDate?,
Expand All @@ -43,3 +57,21 @@ fun relativeDateText(
)
?: stringResource(MR.strings.not_applicable)
}

@Composable
fun relativeDateTimeText(
localDateTime: LocalDateTime?,
): String {
val context = LocalContext.current

val preferences = remember { Injekt.get<UiPreferences>() }
val relativeTime = remember { preferences.relativeTime().get() }
val dateFormat = remember { UiPreferences.dateFormat(preferences.dateFormat().get()) }

return localDateTime?.toRelativeString(
context = context,
relative = relativeTime,
dateFormat = dateFormat,
)
?: stringResource(MR.strings.not_applicable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import androidx.compose.ui.util.fastMap
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.domain.entries.anime.model.episodesFiltered
import eu.kanade.presentation.components.relativeDateText
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.entries.DownloadAction
import eu.kanade.presentation.entries.EntryScreenItem
import eu.kanade.presentation.entries.anime.components.AnimeActionRow
Expand Down Expand Up @@ -902,7 +902,7 @@ private fun LazyListScope.sharedEpisodeItems(
} else {
episodeItem.episode.name
},
date = relativeDateText(episodeItem.episode.dateUpload),
date = relativeDateTimeText(episodeItem.episode.dateUpload),
watchProgress = episodeItem.episode.lastSecondSeen
.takeIf { !episodeItem.episode.seen && it > 0L }
?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import androidx.compose.ui.util.fastAny
import androidx.compose.ui.util.fastMap
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.presentation.components.relativeDateText
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.entries.DownloadAction
import eu.kanade.presentation.entries.EntryScreenItem
import eu.kanade.presentation.entries.components.EntryBottomActionMenu
Expand Down Expand Up @@ -803,7 +803,7 @@ private fun LazyListScope.sharedChapterItems(
} else {
item.chapter.name
},
date = relativeDateText(item.chapter.dateUpload),
date = relativeDateTimeText(item.chapter.dateUpload),
readProgress = item.chapter.lastPageRead
.takeIf { !item.chapter.read && it > 0L }
?.let {
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.relativeDateText
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.history.anime.components.AnimeHistoryItem
import eu.kanade.presentation.theme.TachiyomiPreviewTheme
import eu.kanade.tachiyomi.ui.history.anime.AnimeHistoryScreenModel
Expand All @@ -20,7 +20,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.LocalDate
import java.time.LocalDateTime

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

sealed interface AnimeHistoryUiModel {
data class Header(val date: LocalDate) : AnimeHistoryUiModel
data class Header(val date: LocalDateTime) : 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.LocalDate
import java.time.LocalDateTime
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(LocalDate.now().plusDays(1))
AnimeHistoryUiModel.Header(LocalDateTime.now().plusDays(1))

fun header(instantBuilder: (Instant) -> Instant = { it }) =
AnimeHistoryUiModel.Header(LocalDate.from(instantBuilder(Instant.now())))
AnimeHistoryUiModel.Header(LocalDateTime.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.relativeDateText
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.history.manga.components.MangaHistoryItem
import eu.kanade.presentation.theme.TachiyomiPreviewTheme
import eu.kanade.tachiyomi.ui.history.manga.MangaHistoryScreenModel
Expand All @@ -20,7 +20,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.LocalDate
import java.time.LocalDateTime

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

sealed interface MangaHistoryUiModel {
data class Header(val date: LocalDate) : MangaHistoryUiModel
data class Header(val date: LocalDateTime) : 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.LocalDate
import java.time.LocalDateTime
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(LocalDate.now().plusDays(1))
MangaHistoryUiModel.Header(LocalDateTime.now().plusDays(1))

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

fun items() = sequence {
var count = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import tachiyomi.presentation.core.screens.EmptyScreen
import tachiyomi.presentation.core.screens.LoadingScreen
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.time.Duration.Companion.seconds

@Composable
Expand Down Expand Up @@ -153,6 +153,6 @@ private fun AnimeUpdatesBottomBar(
}

sealed interface AnimeUpdatesUiModel {
data class Header(val date: LocalDate) : AnimeUpdatesUiModel
data class Header(val date: LocalDateTime) : AnimeUpdatesUiModel
data class Item(val item: AnimeUpdatesItem) : AnimeUpdatesUiModel
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import eu.kanade.presentation.components.relativeDateText
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.entries.anime.components.EpisodeDownloadAction
import eu.kanade.presentation.entries.anime.components.EpisodeDownloadIndicator
import eu.kanade.presentation.entries.components.DotSeparatorText
Expand Down Expand Up @@ -96,7 +96,7 @@ internal fun LazyListScope.animeUpdatesUiItems(
is AnimeUpdatesUiModel.Header -> {
ListGroupHeader(
modifier = Modifier.animateItem(),
text = relativeDateText(item.date),
text = relativeDateTimeText(item.date),
)
}
is AnimeUpdatesUiModel.Item -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import tachiyomi.presentation.core.components.material.PullRefresh
import tachiyomi.presentation.core.components.material.Scaffold
import tachiyomi.presentation.core.screens.EmptyScreen
import tachiyomi.presentation.core.screens.LoadingScreen
import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.time.Duration.Companion.seconds

@Composable
Expand Down Expand Up @@ -141,6 +141,6 @@ private fun MangaUpdatesBottomBar(
}

sealed interface MangaUpdatesUiModel {
data class Header(val date: LocalDate) : MangaUpdatesUiModel
data class Header(val date: LocalDateTime) : MangaUpdatesUiModel
data class Item(val item: MangaUpdatesItem) : MangaUpdatesUiModel
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import eu.kanade.presentation.components.relativeDateText
import eu.kanade.presentation.components.relativeDateTimeText
import eu.kanade.presentation.entries.components.DotSeparatorText
import eu.kanade.presentation.entries.components.ItemCover
import eu.kanade.presentation.entries.manga.components.ChapterDownloadAction
Expand Down Expand Up @@ -92,7 +92,7 @@ internal fun LazyListScope.mangaUpdatesUiItems(
is MangaUpdatesUiModel.Header -> {
ListGroupHeader(
modifier = Modifier.animateItem(),
text = relativeDateText(item.date),
text = relativeDateTimeText(item.date),
)
}
is MangaUpdatesUiModel.Item -> {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/eu/kanade/tachiyomi/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi
import android.annotation.SuppressLint
import android.app.Application
import android.app.PendingIntent
import android.app.job.JobInfo
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -47,8 +46,6 @@ import eu.kanade.tachiyomi.util.system.DeviceUtil
import eu.kanade.tachiyomi.util.system.WebViewUtil
import eu.kanade.tachiyomi.util.system.animatorDurationScale
import eu.kanade.tachiyomi.util.system.cancelNotification
import eu.kanade.tachiyomi.util.system.isPreviewBuildType
import eu.kanade.tachiyomi.util.system.isReleaseBuildType
import eu.kanade.tachiyomi.util.system.notify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
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.toLocalDate
import eu.kanade.tachiyomi.util.lang.toLocalDateTime
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?.toLocalDate()
val afterDate = after?.item?.seenAt?.time?.toLocalDate()
val beforeDate = before?.item?.seenAt?.time?.toLocalDateTime()
val afterDate = after?.item?.seenAt?.time?.toLocalDateTime()
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.toLocalDate
import eu.kanade.tachiyomi.util.lang.toLocalDateTime
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?.toLocalDate()
val afterDate = after?.item?.readAt?.time?.toLocalDate()
val beforeDate = before?.item?.readAt?.time?.toLocalDateTime()
val afterDate = after?.item?.readAt?.time?.toLocalDateTime()
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.ReadItemAlpha
import tachiyomi.presentation.core.components.material.padding
import tachiyomi.presentation.core.i18n.stringResource
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
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 {
LocalDate.ofInstant(
LocalDateTime.ofInstant(
Instant.ofEpochMilli(it),
ZoneId.systemDefault(),
).toRelativeString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import eu.kanade.tachiyomi.data.download.anime.AnimeDownloadCache
import eu.kanade.tachiyomi.data.download.anime.AnimeDownloadManager
import eu.kanade.tachiyomi.data.download.anime.model.AnimeDownload
import eu.kanade.tachiyomi.data.library.anime.AnimeLibraryUpdateJob
import eu.kanade.tachiyomi.util.lang.toLocalDate
import eu.kanade.tachiyomi.util.lang.toLocalDateTime
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.mutate
import kotlinx.collections.immutable.persistentListOf
Expand Down Expand Up @@ -392,8 +392,8 @@ class AnimeUpdatesScreenModel(
return items
.map { AnimeUpdatesUiModel.Item(it) }
.insertSeparators { before, after ->
val beforeDate = before?.item?.update?.dateFetch?.toLocalDate()
val afterDate = after?.item?.update?.dateFetch?.toLocalDate()
val beforeDate = before?.item?.update?.dateFetch?.toLocalDateTime()
val afterDate = after?.item?.update?.dateFetch?.toLocalDateTime()
when {
beforeDate != afterDate && afterDate != null -> AnimeUpdatesUiModel.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 @@ -16,7 +16,7 @@ import eu.kanade.tachiyomi.data.download.manga.MangaDownloadCache
import eu.kanade.tachiyomi.data.download.manga.MangaDownloadManager
import eu.kanade.tachiyomi.data.download.manga.model.MangaDownload
import eu.kanade.tachiyomi.data.library.manga.MangaLibraryUpdateJob
import eu.kanade.tachiyomi.util.lang.toLocalDate
import eu.kanade.tachiyomi.util.lang.toLocalDateTime
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.mutate
import kotlinx.collections.immutable.persistentListOf
Expand Down Expand Up @@ -373,8 +373,8 @@ class MangaUpdatesScreenModel(
return items
.map { MangaUpdatesUiModel.Item(it) }
.insertSeparators { before, after ->
val beforeDate = before?.item?.update?.dateFetch?.toLocalDate()
val afterDate = after?.item?.update?.dateFetch?.toLocalDate()
val beforeDate = before?.item?.update?.dateFetch?.toLocalDateTime()
val afterDate = after?.item?.update?.dateFetch?.toLocalDateTime()
when {
beforeDate != afterDate && afterDate != null -> MangaUpdatesUiModel.Header(afterDate)
// Return null to avoid adding a separator between two items.
Expand Down
Loading

0 comments on commit 1f3be7b

Please sign in to comment.