Format watch history item dates.

This commit is contained in:
Isira Seneviratne 2023-03-01 19:49:27 +05:30
parent 58374ddba1
commit 57477ff9fd
8 changed files with 32 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package com.github.libretube.db
import androidx.room.AutoMigration import androidx.room.AutoMigration
import androidx.room.Database import androidx.room.Database
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.github.libretube.db.dao.CustomInstanceDao import com.github.libretube.db.dao.CustomInstanceDao
import com.github.libretube.db.dao.DownloadDao import com.github.libretube.db.dao.DownloadDao
import com.github.libretube.db.dao.LocalPlaylistsDao import com.github.libretube.db.dao.LocalPlaylistsDao
@ -42,6 +43,7 @@ import com.github.libretube.db.obj.WatchPosition
AutoMigration(from = 9, to = 10) AutoMigration(from = 9, to = 10)
] ]
) )
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
/** /**
* Watch History * Watch History

View File

@ -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()
}

View File

@ -17,7 +17,7 @@ object DatabaseHelper {
val watchHistoryItem = WatchHistoryItem( val watchHistoryItem = WatchHistoryItem(
videoId, videoId,
streams.title, streams.title,
streams.uploadDate.toString(), streams.uploadDate,
streams.uploader, streams.uploader,
streams.uploaderUrl.toID(), streams.uploaderUrl.toID(),
streams.uploaderAvatar, streams.uploaderAvatar,

View File

@ -3,6 +3,7 @@ package com.github.libretube.db.obj
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
@ -10,7 +11,7 @@ import kotlinx.serialization.Serializable
data class WatchHistoryItem( data class WatchHistoryItem(
@PrimaryKey val videoId: String = "", @PrimaryKey val videoId: String = "",
@ColumnInfo val title: String? = null, @ColumnInfo val title: String? = null,
@ColumnInfo val uploadDate: String? = null, @ColumnInfo val uploadDate: LocalDate? = null,
@ColumnInfo val uploader: String? = null, @ColumnInfo val uploader: String? = null,
@ColumnInfo val uploaderUrl: String? = null, @ColumnInfo val uploaderUrl: String? = null,
@ColumnInfo var uploaderAvatar: String? = null, @ColumnInfo var uploaderAvatar: String? = null,

View File

@ -14,6 +14,7 @@ import com.github.libretube.ui.extensions.setFormattedDuration
import com.github.libretube.ui.extensions.setWatchProgressLength import com.github.libretube.ui.extensions.setWatchProgressLength
import com.github.libretube.ui.sheets.VideoOptionsBottomSheet import com.github.libretube.ui.sheets.VideoOptionsBottomSheet
import com.github.libretube.ui.viewholders.WatchHistoryViewHolder import com.github.libretube.ui.viewholders.WatchHistoryViewHolder
import com.github.libretube.util.TextUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -55,7 +56,7 @@ class WatchHistoryAdapter(
holder.binding.apply { holder.binding.apply {
videoTitle.text = video.title videoTitle.text = video.title
channelName.text = video.uploader channelName.text = video.uploader
videoInfo.text = video.uploadDate videoInfo.text = video.uploadDate?.let { TextUtils.localizeDate(it) }
thumbnailDuration.setFormattedDuration(video.duration!!, null) thumbnailDuration.setFormattedDuration(video.duration!!, null)
ImageHelper.loadImage(video.thumbnailUrl, thumbnail) ImageHelper.loadImage(video.thumbnailUrl, thumbnail)
ImageHelper.loadImage(video.uploaderAvatar, channelImage) ImageHelper.loadImage(video.uploaderAvatar, channelImage)

View File

@ -26,7 +26,6 @@ import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.os.ConfigurationCompat
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.core.text.parseAsHtml import androidx.core.text.parseAsHtml
@ -818,8 +817,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
private fun localizeDate(streams: Streams): String { private fun localizeDate(streams: Streams): String {
return if (!streams.livestream) { return if (!streams.livestream) {
val locale = ConfigurationCompat.getLocales(resources.configuration)[0]!! TextUtils.SEPARATOR + TextUtils.localizeDate(streams.uploadDate)
TextUtils.SEPARATOR + TextUtils.localizeDate(streams.uploadDate, locale)
} else { } else {
"" ""
} }

View File

@ -90,7 +90,7 @@ class WatchHistoryFragment : Fragment() {
uploaderName = it.uploader, uploaderName = it.uploader,
uploaderUrl = it.uploaderUrl, uploaderUrl = it.uploaderUrl,
uploaderAvatar = it.uploaderAvatar, uploaderAvatar = it.uploaderAvatar,
uploadedDate = it.uploadDate, uploadedDate = it.uploadDate?.toString(),
duration = it.duration duration = it.duration
) )
}.toTypedArray() }.toTypedArray()

View File

@ -9,7 +9,6 @@ import java.time.ZoneId
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle import java.time.format.FormatStyle
import java.time.temporal.ChronoUnit import java.time.temporal.ChronoUnit
import java.util.*
import kotlin.time.Duration import kotlin.time.Duration
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.toJavaLocalDate import kotlinx.datetime.toJavaLocalDate
@ -27,14 +26,17 @@ object TextUtils {
const val RESERVED_CHARS = "?:\"*|/\\<>\u0000" const val RESERVED_CHARS = "?:\"*|/\\<>\u0000"
/** /**
* Localize the date from a time string * Date time formatter which uses the [FormatStyle.MEDIUM] format style.
* @param date The date to parse
* @param locale The locale to use, otherwise uses system default
* return Localized date string
*/ */
fun localizeDate(date: LocalDate, locale: Locale): String { private val MEDIUM_DATE_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale)
return date.toJavaLocalDate().format(formatter) /**
* 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)
} }
/** /**