Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kotlinx-datetime instead of Java APIs #50

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
}

compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
Expand All @@ -52,11 +53,11 @@ android {
}

dependencies {
api(libs.kotlinx.datetime)
api(libs.kotlinx.serialization.json)
coreLibraryDesugaring(libs.android.desugar.jdk.libs)

testRuntimeOnly(libs.robolectric)
testImplementation(libs.junit)
testImplementation(libs.androidx.test.ext.junit)
testImplementation(libs.kotlin.test)
}

publishing {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
package ch.srg.dataProvider.integrationlayer.data

import ch.srg.dataProvider.integrationlayer.data.serializer.ImageUrlSerializer
import java.io.Serializable
import kotlinx.serialization.Serializable

/**
* Image url
*
* @property rawUrl Internal image url, to retrieve the url use [ImageUrl.decorated].
*/
@Suppress("SerialVersionUIDInSerializableClass")
@kotlinx.serialization.Serializable(with = ImageUrlSerializer::class)
@Serializable(with = ImageUrlSerializer::class)
data class ImageUrl(
/**
* Only for internal use! Please use a Decorator!
*
* @return the undecorated url
*/
val rawUrl: String
) : Serializable {
) {

/**
* Decorated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -17,6 +13,6 @@ import java.util.Date
data class BroadCastInformation(
val hintText: String? = null,
val url: String? = null,
val startDate: Date? = null,
val endDate: Date? = null
val startDate: Instant? = null,
val endDate: Instant? = null
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -29,12 +25,12 @@ data class Chapter(
override val blockReason: BlockReason? = null,
override val youthProtectionColor: YouthProtectionColor? = null,
override val type: Type,
override val date: Date,
override val date: Instant,
override val duration: Long,
override val podcastSdUrl: String? = null,
override val podcastHdUrl: String? = null,
override val validFrom: Date? = null,
override val validTo: Date? = null,
override val validFrom: Instant? = null,
override val validTo: Instant? = null,
override val assignedBy: Referrer? = null,
override val playableAbroad: Boolean = false,
override val relatedContentList: List<RelatedContent>? = null,
Expand All @@ -53,14 +49,14 @@ data class Chapter(
val segmentList: List<Segment>? = null,
val resourceList: List<Resource>? = null,
val spriteSheet: SpriteSheet? = null,
val preTrailerStart: Date? = null,
val postTrailerStart: Date? = null,
val preTrailerStart: Instant? = null,
val postTrailerStart: Instant? = null,
/**
* The reference date corresponding to the beginning of the stream, if any. You can use this date to map a time
* position relative to the stream (e.g., a segment mark in or mark out) to a date.
*/
@SerialName("dvrReferenceDate")
val resourceReferenceDate: Date? = null,
val resourceReferenceDate: Instant? = null,
val timeIntervalList: List<TimeInterval>? = null,
override val imageFocalPoint: FocalPoint? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -22,7 +18,7 @@ data class Episode(
override val description: String? = null,
override val imageTitle: String? = null,
override val imageCopyright: String? = null,
val publishedDate: Date? = null,
val publishedDate: Instant? = null,
val fullLengthUrn: String? = null,
val seasonNumber: Int? = null,
val number: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -22,7 +18,7 @@ data class Media(
override val urn: String,
override val title: String,
override val type: Type,
override val date: Date,
override val date: Instant,
override val duration: Long,
override val imageUrl: ImageUrl,
override val imageFocalPoint: FocalPoint? = null,
Expand All @@ -34,8 +30,8 @@ data class Media(
override val youthProtectionColor: YouthProtectionColor? = null,
override val podcastSdUrl: String? = null,
override val podcastHdUrl: String? = null,
override val validFrom: Date? = null,
override val validTo: Date? = null,
override val validFrom: Instant? = null,
override val validTo: Instant? = null,
override val assignedBy: Referrer? = null,
override val playableAbroad: Boolean,
override val relatedContentList: List<RelatedContent>? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand Down Expand Up @@ -42,7 +38,7 @@ data class ShowBucket(val urn: String, val title: String, override val count: In
data class DurationInMinutesBucket(val duration: Long, override val count: Int) : Bucket

@Serializable
data class DateBucket(val date: Date, override val count: Int) : Bucket
data class DateBucket(val date: Instant, override val count: Int) : Bucket

@Serializable
data class MediaAggregations(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -17,8 +13,8 @@ import java.util.Date
@Serializable
data class Program(
override val title: String,
val startTime: Date,
val endTime: Date,
val startTime: Instant,
val endTime: Instant,
override val lead: String? = null,
override val description: String? = null,
val imageUrl: ImageUrl? = null,
Expand Down Expand Up @@ -56,8 +52,8 @@ data class Program(
val channelUrn: String? = null
) : SRGMetadata {

fun isDateInProgramTime(date: Date): Boolean {
return date.after(startTime) && date.before(endTime)
fun isDateInProgramTime(instant: Instant): Boolean {
return instant > startTime && instant < endTime
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.srg.dataProvider.integrationlayer.data.remote

import java.util.Date
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -10,11 +11,11 @@ import java.util.Date
interface SRGMediaMetadata : SRGIdentifierMetadata, SRGImageMetadata, SRGMetadata {
val mediaType: MediaType
val type: Type
val date: Date
val date: Instant
val duration: Long
val blockReason: BlockReason?
val validFrom: Date?
val validTo: Date?
val validFrom: Instant?
val validTo: Instant?
val assignedBy: Referrer?
val playableAbroad: Boolean
val youthProtectionColor: YouthProtectionColor?
Expand All @@ -34,26 +35,28 @@ interface SRGMediaMetadata : SRGIdentifierMetadata, SRGImageMetadata, SRGMetadat
/**
* isBlocked if it has a blockReason or blocked by TimeAvailability at a given time
*/
fun isBlocked(at: Date = Date()): Boolean {
fun isBlocked(at: Instant = Clock.System.now()): Boolean {
return blockReason != null || getTimeAvailability(at) != TimeAvailability.AVAILABLE
}

fun getTimeAvailability(at: Date = Date()): TimeAvailability {
fun getTimeAvailability(at: Instant = Clock.System.now()): TimeAvailability {
val validTo = validTo
val validFrom = validFrom
return when {
blockReason == BlockReason.STARTDATE -> TimeAvailability.NOT_YET_AVAILABLE
blockReason == BlockReason.ENDDATE -> TimeAvailability.NOT_AVAILABLE_ANYMORE
validTo != null && at.after(validTo) -> TimeAvailability.NOT_AVAILABLE_ANYMORE
validFrom != null && at.before(validFrom) -> TimeAvailability.NOT_YET_AVAILABLE
validTo != null && at > validTo -> TimeAvailability.NOT_AVAILABLE_ANYMORE
validFrom != null && at < validFrom -> TimeAvailability.NOT_YET_AVAILABLE
else -> TimeAvailability.AVAILABLE
}
}

fun isBlockedValidFromTime(currentTime: Long = System.currentTimeMillis()): Boolean {
return validFrom != null && validFrom!!.time > currentTime
return validFrom != null && validFrom!!.toEpochMilliseconds() > currentTime
}

fun isBlockValidToTime(currentTime: Long = System.currentTimeMillis()): Boolean {
return validTo != null && currentTime > validTo!!.time
return validTo != null && currentTime > validTo!!.toEpochMilliseconds()
}

fun isTimeBlocked(currentTime: Long): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
@file:UseSerializers(DateSerializer::class)

package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.serializer.DateSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import java.util.Date

/**
* Copyright (c) SRG SSR. All rights reserved.
Expand All @@ -22,8 +18,8 @@ data class Section(
val representation: Representation,
val isPublished: Boolean,
@SerialName("start")
val startDate: Date? = null,
val startDate: Instant? = null,
@SerialName("end")
val endDate: Date? = null,
val endDate: Instant? = null,
val hasPersonalizedContent: Boolean? = null,
) : ILObject
Loading
Loading