diff --git a/app/src/main/java/com/github/libretube/api/obj/Streams.kt b/app/src/main/java/com/github/libretube/api/obj/Streams.kt index aa4ef6fce..e704090a5 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Streams.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Streams.kt @@ -2,18 +2,19 @@ package com.github.libretube.api.obj import com.github.libretube.db.obj.DownloadItem import com.github.libretube.enums.FileType -import com.github.libretube.extensions.toLocalDateSafe import com.github.libretube.extensions.toMillis import com.github.libretube.helpers.ProxyHelper +import com.github.libretube.json.SafeLocalDateSerializer import com.github.libretube.parcelable.DownloadData -import kotlin.io.path.Path +import kotlinx.datetime.LocalDate import kotlinx.serialization.Serializable +import kotlin.io.path.Path @Serializable data class Streams( val title: String, val description: String, - val uploadDate: String, + @Serializable(SafeLocalDateSerializer::class) val uploadDate: LocalDate, val uploader: String, val uploaderUrl: String, val uploaderAvatar: String? = null, @@ -84,8 +85,8 @@ data class Streams( uploaderName = uploader, uploaderUrl = uploaderUrl, uploaderAvatar = uploaderAvatar, - uploadedDate = uploadDate, - uploaded = uploadDate.toLocalDateSafe().toMillis(), + uploadedDate = uploadDate.toString(), + uploaded = uploadDate.toMillis(), duration = duration, views = views, uploaderVerified = uploaderVerified, diff --git a/app/src/main/java/com/github/libretube/db/Converters.kt b/app/src/main/java/com/github/libretube/db/Converters.kt index c3e56a690..71bfb289a 100644 --- a/app/src/main/java/com/github/libretube/db/Converters.kt +++ b/app/src/main/java/com/github/libretube/db/Converters.kt @@ -2,18 +2,18 @@ package com.github.libretube.db import androidx.room.TypeConverter import com.github.libretube.api.JsonHelper -import com.github.libretube.extensions.toLocalDateSafe +import kotlinx.datetime.LocalDate +import kotlinx.datetime.toLocalDate +import kotlinx.serialization.encodeToString import java.nio.file.Path import kotlin.io.path.Path -import kotlinx.datetime.LocalDate -import kotlinx.serialization.encodeToString object Converters { @TypeConverter fun localDateToString(localDate: LocalDate?) = localDate?.toString() @TypeConverter - fun stringToLocalDate(string: String?) = string?.toLocalDateSafe() + fun stringToLocalDate(string: String?) = string?.toLocalDate() @TypeConverter fun pathToString(path: Path?) = path?.toString() diff --git a/app/src/main/java/com/github/libretube/extensions/LocalDate.kt b/app/src/main/java/com/github/libretube/extensions/LocalDate.kt index 338cec6c7..370d9a17a 100644 --- a/app/src/main/java/com/github/libretube/extensions/LocalDate.kt +++ b/app/src/main/java/com/github/libretube/extensions/LocalDate.kt @@ -3,8 +3,5 @@ package com.github.libretube.extensions import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone import kotlinx.datetime.atStartOfDayIn -import kotlinx.datetime.toLocalDate fun LocalDate.toMillis() = this.atStartOfDayIn(TimeZone.UTC).toEpochMilliseconds() - -fun String.toLocalDateSafe() = this.substring(0, 10).toLocalDate() diff --git a/app/src/main/java/com/github/libretube/json/SafeLocalDateSerializer.kt b/app/src/main/java/com/github/libretube/json/SafeLocalDateSerializer.kt new file mode 100644 index 000000000..f38e30964 --- /dev/null +++ b/app/src/main/java/com/github/libretube/json/SafeLocalDateSerializer.kt @@ -0,0 +1,32 @@ +package com.github.libretube.json + +import android.util.Log +import com.github.libretube.extensions.TAG +import kotlinx.datetime.LocalDate +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toInstant +import kotlinx.datetime.toLocalDate +import kotlinx.datetime.toLocalDateTime +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveKind.STRING +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +object SafeLocalDateSerializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("LocalDate", STRING) + + override fun deserialize(decoder: Decoder): LocalDate { + val string = decoder.decodeString() + return try { + string.toLocalDate() + } catch (e: IllegalArgumentException) { + Log.e(TAG(), "Error parsing date '$string'", e) + string.toInstant().toLocalDateTime(TimeZone.currentSystemDefault()).date + } + } + + override fun serialize(encoder: Encoder, value: LocalDate) { + encoder.encodeString(value.toString()) + } +} diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 8e28534b4..98689be01 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -28,7 +28,6 @@ import com.github.libretube.enums.FileType import com.github.libretube.extensions.formatAsFileSize import com.github.libretube.extensions.getContentLength import com.github.libretube.extensions.parcelableExtra -import com.github.libretube.extensions.toLocalDateSafe import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.helpers.DownloadHelper import com.github.libretube.helpers.DownloadHelper.getNotificationId @@ -116,7 +115,7 @@ class DownloadService : LifecycleService() { streams.title, streams.description, streams.uploader, - streams.uploadDate.toLocalDateSafe(), + streams.uploadDate, thumbnailTargetPath ) Database.downloadDao().insertDownload(download)