mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20: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.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,
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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.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)
|
||||
|
Loading…
Reference in New Issue
Block a user