From 57477ff9fde39d298fc7755211f2ee19b6da2659 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 1 Mar 2023 19:49:27 +0530 Subject: [PATCH] Format watch history item dates. --- .../com/github/libretube/db/AppDatabase.kt | 2 ++ .../java/com/github/libretube/db/Converters.kt | 13 +++++++++++++ .../com/github/libretube/db/DatabaseHelper.kt | 2 +- .../libretube/db/obj/WatchHistoryItem.kt | 3 ++- .../ui/adapters/WatchHistoryAdapter.kt | 3 ++- .../libretube/ui/fragments/PlayerFragment.kt | 4 +--- .../ui/fragments/WatchHistoryFragment.kt | 2 +- .../com/github/libretube/util/TextUtils.kt | 18 ++++++++++-------- 8 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/db/Converters.kt diff --git a/app/src/main/java/com/github/libretube/db/AppDatabase.kt b/app/src/main/java/com/github/libretube/db/AppDatabase.kt index 91326a615..371dd9957 100644 --- a/app/src/main/java/com/github/libretube/db/AppDatabase.kt +++ b/app/src/main/java/com/github/libretube/db/AppDatabase.kt @@ -3,6 +3,7 @@ package com.github.libretube.db import androidx.room.AutoMigration import androidx.room.Database import androidx.room.RoomDatabase +import androidx.room.TypeConverters import com.github.libretube.db.dao.CustomInstanceDao import com.github.libretube.db.dao.DownloadDao import com.github.libretube.db.dao.LocalPlaylistsDao @@ -42,6 +43,7 @@ import com.github.libretube.db.obj.WatchPosition AutoMigration(from = 9, to = 10) ] ) +@TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { /** * Watch History diff --git a/app/src/main/java/com/github/libretube/db/Converters.kt b/app/src/main/java/com/github/libretube/db/Converters.kt new file mode 100644 index 000000000..be094b0b4 --- /dev/null +++ b/app/src/main/java/com/github/libretube/db/Converters.kt @@ -0,0 +1,13 @@ +package com.github.libretube.db + +import androidx.room.TypeConverter +import kotlinx.datetime.LocalDate +import kotlinx.datetime.toLocalDate + +object Converters { + @TypeConverter + fun localDateToString(localDate: LocalDate?) = localDate?.toString() + + @TypeConverter + fun stringToLocalDate(string: String?) = string?.toLocalDate() +} diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index 9ff1669d5..711b73f13 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -17,7 +17,7 @@ object DatabaseHelper { val watchHistoryItem = WatchHistoryItem( videoId, streams.title, - streams.uploadDate.toString(), + streams.uploadDate, streams.uploader, streams.uploaderUrl.toID(), streams.uploaderAvatar, diff --git a/app/src/main/java/com/github/libretube/db/obj/WatchHistoryItem.kt b/app/src/main/java/com/github/libretube/db/obj/WatchHistoryItem.kt index 01a8bdd74..54503d0b6 100644 --- a/app/src/main/java/com/github/libretube/db/obj/WatchHistoryItem.kt +++ b/app/src/main/java/com/github/libretube/db/obj/WatchHistoryItem.kt @@ -3,6 +3,7 @@ package com.github.libretube.db.obj import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey +import kotlinx.datetime.LocalDate import kotlinx.serialization.Serializable @Serializable @@ -10,7 +11,7 @@ import kotlinx.serialization.Serializable data class WatchHistoryItem( @PrimaryKey val videoId: String = "", @ColumnInfo val title: String? = null, - @ColumnInfo val uploadDate: String? = null, + @ColumnInfo val uploadDate: LocalDate? = null, @ColumnInfo val uploader: String? = null, @ColumnInfo val uploaderUrl: String? = null, @ColumnInfo var uploaderAvatar: String? = null, diff --git a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt index d18c10edb..a99fab3db 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt @@ -14,6 +14,7 @@ import com.github.libretube.ui.extensions.setFormattedDuration import com.github.libretube.ui.extensions.setWatchProgressLength import com.github.libretube.ui.sheets.VideoOptionsBottomSheet import com.github.libretube.ui.viewholders.WatchHistoryViewHolder +import com.github.libretube.util.TextUtils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking @@ -55,7 +56,7 @@ class WatchHistoryAdapter( holder.binding.apply { videoTitle.text = video.title channelName.text = video.uploader - videoInfo.text = video.uploadDate + videoInfo.text = video.uploadDate?.let { TextUtils.localizeDate(it) } thumbnailDuration.setFormattedDuration(video.duration!!, null) ImageHelper.loadImage(video.thumbnailUrl, thumbnail) ImageHelper.loadImage(video.uploaderAvatar, channelImage) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 06ebeabb3..abd554f02 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -26,7 +26,6 @@ import android.widget.Toast import androidx.annotation.RequiresApi import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.net.toUri -import androidx.core.os.ConfigurationCompat import androidx.core.os.bundleOf import androidx.core.os.postDelayed import androidx.core.text.parseAsHtml @@ -818,8 +817,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { private fun localizeDate(streams: Streams): String { return if (!streams.livestream) { - val locale = ConfigurationCompat.getLocales(resources.configuration)[0]!! - TextUtils.SEPARATOR + TextUtils.localizeDate(streams.uploadDate, locale) + TextUtils.SEPARATOR + TextUtils.localizeDate(streams.uploadDate) } else { "" } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt index fa3c715a0..feebea6ba 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt @@ -90,7 +90,7 @@ class WatchHistoryFragment : Fragment() { uploaderName = it.uploader, uploaderUrl = it.uploaderUrl, uploaderAvatar = it.uploaderAvatar, - uploadedDate = it.uploadDate, + uploadedDate = it.uploadDate?.toString(), duration = it.duration ) }.toTypedArray() diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index a1a5e5c78..adcbe9b1c 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -9,7 +9,6 @@ import java.time.ZoneId import java.time.format.DateTimeFormatter import java.time.format.FormatStyle import java.time.temporal.ChronoUnit -import java.util.* import kotlin.time.Duration import kotlinx.datetime.LocalDate import kotlinx.datetime.toJavaLocalDate @@ -27,14 +26,17 @@ object TextUtils { const val RESERVED_CHARS = "?:\"*|/\\<>\u0000" /** - * Localize the date from a time string - * @param date The date to parse - * @param locale The locale to use, otherwise uses system default - * return Localized date string + * Date time formatter which uses the [FormatStyle.MEDIUM] format style. */ - fun localizeDate(date: LocalDate, locale: Locale): String { - val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale) - return date.toJavaLocalDate().format(formatter) + private val MEDIUM_DATE_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) + + /** + * Localize the date from a date string, using the medium format. + * @param date The date to parse + * @return localized date string + */ + fun localizeDate(date: LocalDate): String { + return date.toJavaLocalDate().format(MEDIUM_DATE_FORMATTER) } /**