Convert Converters to a Kotlin object.

This commit is contained in:
Isira Seneviratne 2021-05-16 11:14:15 +05:30
parent e8b8391868
commit 3b1c4b043d
1 changed files with 24 additions and 36 deletions

View File

@ -1,64 +1,52 @@
package org.schabi.newpipe.database; package org.schabi.newpipe.database
import androidx.room.TypeConverter; import androidx.room.TypeConverter
import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.local.subscription.FeedGroupIcon
import org.schabi.newpipe.local.subscription.FeedGroupIcon; import java.time.Instant
import java.time.OffsetDateTime
import java.time.Instant; import java.time.ZoneOffset
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public final class Converters {
private Converters() { }
object Converters {
/** /**
* Convert a long value to a {@link OffsetDateTime}. * Convert a long value to a [OffsetDateTime].
* *
* @param value the long value * @param value the long value
* @return the {@code OffsetDateTime} * @return the `OffsetDateTime`
*/ */
@TypeConverter @TypeConverter
public static OffsetDateTime offsetDateTimeFromTimestamp(final Long value) { fun offsetDateTimeFromTimestamp(value: Long?): OffsetDateTime? {
return value == null ? null : OffsetDateTime.ofInstant(Instant.ofEpochMilli(value), return value?.let { OffsetDateTime.ofInstant(Instant.ofEpochMilli(it), ZoneOffset.UTC) }
ZoneOffset.UTC);
} }
/** /**
* Convert a {@link OffsetDateTime} to a long value. * Convert a [OffsetDateTime] to a long value.
* *
* @param offsetDateTime the {@code OffsetDateTime} * @param offsetDateTime the `OffsetDateTime`
* @return the long value * @return the long value
*/ */
@TypeConverter @TypeConverter
public static Long offsetDateTimeToTimestamp(final OffsetDateTime offsetDateTime) { fun offsetDateTimeToTimestamp(offsetDateTime: OffsetDateTime?): Long? {
return offsetDateTime == null ? null : offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC) return offsetDateTime?.withOffsetSameInstant(ZoneOffset.UTC)?.toInstant()?.toEpochMilli()
.toInstant().toEpochMilli();
} }
@TypeConverter @TypeConverter
public static StreamType streamTypeOf(final String value) { fun streamTypeOf(value: String): StreamType {
return StreamType.valueOf(value); return StreamType.valueOf(value)
} }
@TypeConverter @TypeConverter
public static String stringOf(final StreamType streamType) { fun stringOf(streamType: StreamType): String {
return streamType.name(); return streamType.name
} }
@TypeConverter @TypeConverter
public static Integer integerOf(final FeedGroupIcon feedGroupIcon) { fun integerOf(feedGroupIcon: FeedGroupIcon): Int {
return feedGroupIcon.getId(); return feedGroupIcon.id
} }
@TypeConverter @TypeConverter
public static FeedGroupIcon feedGroupIconOf(final Integer id) { fun feedGroupIconOf(id: Int): FeedGroupIcon {
for (final FeedGroupIcon icon : FeedGroupIcon.values()) { return FeedGroupIcon.values().first { it.id == id }
if (icon.getId() == id) {
return icon;
}
}
throw new IllegalArgumentException("There's no feed group icon with the id \"" + id + "\"");
} }
} }