NewPipe/app/src/main/java/org/schabi/newpipe/database/Converters.kt

53 lines
1.4 KiB
Kotlin
Raw Normal View History

2021-05-16 07:44:15 +02:00
package org.schabi.newpipe.database
2021-05-16 07:44:15 +02:00
import androidx.room.TypeConverter
import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.local.subscription.FeedGroupIcon
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
class Converters {
/**
2021-05-16 07:44:15 +02:00
* Convert a long value to a [OffsetDateTime].
*
* @param value the long value
2021-05-16 07:44:15 +02:00
* @return the `OffsetDateTime`
*/
@TypeConverter
2021-05-16 07:44:15 +02:00
fun offsetDateTimeFromTimestamp(value: Long?): OffsetDateTime? {
return value?.let { OffsetDateTime.ofInstant(Instant.ofEpochMilli(it), ZoneOffset.UTC) }
}
/**
2021-05-16 07:44:15 +02:00
* Convert a [OffsetDateTime] to a long value.
*
2021-05-16 07:44:15 +02:00
* @param offsetDateTime the `OffsetDateTime`
* @return the long value
*/
@TypeConverter
2021-05-16 07:44:15 +02:00
fun offsetDateTimeToTimestamp(offsetDateTime: OffsetDateTime?): Long? {
return offsetDateTime?.withOffsetSameInstant(ZoneOffset.UTC)?.toInstant()?.toEpochMilli()
}
@TypeConverter
2021-05-16 07:44:15 +02:00
fun streamTypeOf(value: String): StreamType {
return StreamType.valueOf(value)
}
@TypeConverter
2021-05-16 07:44:15 +02:00
fun stringOf(streamType: StreamType): String {
return streamType.name
}
@TypeConverter
2021-05-16 07:44:15 +02:00
fun integerOf(feedGroupIcon: FeedGroupIcon): Int {
return feedGroupIcon.id
}
@TypeConverter
2021-05-16 07:44:15 +02:00
fun feedGroupIconOf(id: Int): FeedGroupIcon {
2023-11-01 02:25:11 +01:00
return FeedGroupIcon.entries.first { it.id == id }
}
}