mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
fix: Properly parse date strings
This commit is contained in:
parent
0b8de2764b
commit
1ccb857f2a
@ -2,18 +2,19 @@ package com.github.libretube.api.obj
|
|||||||
|
|
||||||
import com.github.libretube.db.obj.DownloadItem
|
import com.github.libretube.db.obj.DownloadItem
|
||||||
import com.github.libretube.enums.FileType
|
import com.github.libretube.enums.FileType
|
||||||
import com.github.libretube.extensions.toLocalDateSafe
|
|
||||||
import com.github.libretube.extensions.toMillis
|
import com.github.libretube.extensions.toMillis
|
||||||
import com.github.libretube.helpers.ProxyHelper
|
import com.github.libretube.helpers.ProxyHelper
|
||||||
|
import com.github.libretube.json.SafeLocalDateSerializer
|
||||||
import com.github.libretube.parcelable.DownloadData
|
import com.github.libretube.parcelable.DownloadData
|
||||||
import kotlin.io.path.Path
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Streams(
|
data class Streams(
|
||||||
val title: String,
|
val title: String,
|
||||||
val description: String,
|
val description: String,
|
||||||
val uploadDate: String,
|
@Serializable(SafeLocalDateSerializer::class) val uploadDate: LocalDate,
|
||||||
val uploader: String,
|
val uploader: String,
|
||||||
val uploaderUrl: String,
|
val uploaderUrl: String,
|
||||||
val uploaderAvatar: String? = null,
|
val uploaderAvatar: String? = null,
|
||||||
@ -84,8 +85,8 @@ data class Streams(
|
|||||||
uploaderName = uploader,
|
uploaderName = uploader,
|
||||||
uploaderUrl = uploaderUrl,
|
uploaderUrl = uploaderUrl,
|
||||||
uploaderAvatar = uploaderAvatar,
|
uploaderAvatar = uploaderAvatar,
|
||||||
uploadedDate = uploadDate,
|
uploadedDate = uploadDate.toString(),
|
||||||
uploaded = uploadDate.toLocalDateSafe().toMillis(),
|
uploaded = uploadDate.toMillis(),
|
||||||
duration = duration,
|
duration = duration,
|
||||||
views = views,
|
views = views,
|
||||||
uploaderVerified = uploaderVerified,
|
uploaderVerified = uploaderVerified,
|
||||||
|
@ -2,18 +2,18 @@ package com.github.libretube.db
|
|||||||
|
|
||||||
import androidx.room.TypeConverter
|
import androidx.room.TypeConverter
|
||||||
import com.github.libretube.api.JsonHelper
|
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 java.nio.file.Path
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
import kotlinx.datetime.LocalDate
|
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
|
|
||||||
object Converters {
|
object Converters {
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
fun localDateToString(localDate: LocalDate?) = localDate?.toString()
|
fun localDateToString(localDate: LocalDate?) = localDate?.toString()
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
fun stringToLocalDate(string: String?) = string?.toLocalDateSafe()
|
fun stringToLocalDate(string: String?) = string?.toLocalDate()
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
fun pathToString(path: Path?) = path?.toString()
|
fun pathToString(path: Path?) = path?.toString()
|
||||||
|
@ -3,8 +3,5 @@ package com.github.libretube.extensions
|
|||||||
import kotlinx.datetime.LocalDate
|
import kotlinx.datetime.LocalDate
|
||||||
import kotlinx.datetime.TimeZone
|
import kotlinx.datetime.TimeZone
|
||||||
import kotlinx.datetime.atStartOfDayIn
|
import kotlinx.datetime.atStartOfDayIn
|
||||||
import kotlinx.datetime.toLocalDate
|
|
||||||
|
|
||||||
fun LocalDate.toMillis() = this.atStartOfDayIn(TimeZone.UTC).toEpochMilliseconds()
|
fun LocalDate.toMillis() = this.atStartOfDayIn(TimeZone.UTC).toEpochMilliseconds()
|
||||||
|
|
||||||
fun String.toLocalDateSafe() = this.substring(0, 10).toLocalDate()
|
|
||||||
|
@ -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<LocalDate> {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
@ -28,7 +28,6 @@ import com.github.libretube.enums.FileType
|
|||||||
import com.github.libretube.extensions.formatAsFileSize
|
import com.github.libretube.extensions.formatAsFileSize
|
||||||
import com.github.libretube.extensions.getContentLength
|
import com.github.libretube.extensions.getContentLength
|
||||||
import com.github.libretube.extensions.parcelableExtra
|
import com.github.libretube.extensions.parcelableExtra
|
||||||
import com.github.libretube.extensions.toLocalDateSafe
|
|
||||||
import com.github.libretube.extensions.toastFromMainThread
|
import com.github.libretube.extensions.toastFromMainThread
|
||||||
import com.github.libretube.helpers.DownloadHelper
|
import com.github.libretube.helpers.DownloadHelper
|
||||||
import com.github.libretube.helpers.DownloadHelper.getNotificationId
|
import com.github.libretube.helpers.DownloadHelper.getNotificationId
|
||||||
@ -116,7 +115,7 @@ class DownloadService : LifecycleService() {
|
|||||||
streams.title,
|
streams.title,
|
||||||
streams.description,
|
streams.description,
|
||||||
streams.uploader,
|
streams.uploader,
|
||||||
streams.uploadDate.toLocalDateSafe(),
|
streams.uploadDate,
|
||||||
thumbnailTargetPath
|
thumbnailTargetPath
|
||||||
)
|
)
|
||||||
Database.downloadDao().insertDownload(download)
|
Database.downloadDao().insertDownload(download)
|
||||||
|
Loading…
Reference in New Issue
Block a user