2018-01-15 21:30:52 +01:00
|
|
|
package org.schabi.newpipe.database;
|
2017-08-12 06:50:25 +02:00
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.room.TypeConverter;
|
2017-08-12 06:50:25 +02:00
|
|
|
|
2018-01-15 21:30:52 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamType;
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.local.subscription.FeedGroupIcon;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2020-10-18 08:16:55 +02:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.OffsetDateTime;
|
|
|
|
import java.time.ZoneOffset;
|
2017-08-12 06:50:25 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public final class Converters {
|
|
|
|
private Converters() { }
|
2017-08-12 06:50:25 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-18 08:16:55 +02:00
|
|
|
* Convert a long value to a {@link OffsetDateTime}.
|
2020-03-31 19:20:15 +02:00
|
|
|
*
|
2017-08-12 06:50:25 +02:00
|
|
|
* @param value the long value
|
2020-10-18 08:16:55 +02:00
|
|
|
* @return the {@code OffsetDateTime}
|
2017-08-12 06:50:25 +02:00
|
|
|
*/
|
|
|
|
@TypeConverter
|
2020-10-18 08:16:55 +02:00
|
|
|
public static OffsetDateTime offsetDateTimeFromTimestamp(final Long value) {
|
|
|
|
return value == null ? null : OffsetDateTime.ofInstant(Instant.ofEpochMilli(value),
|
|
|
|
ZoneOffset.UTC);
|
2017-08-12 06:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-18 08:16:55 +02:00
|
|
|
* Convert a {@link OffsetDateTime} to a long value.
|
2020-03-31 19:20:15 +02:00
|
|
|
*
|
2020-10-18 08:16:55 +02:00
|
|
|
* @param offsetDateTime the {@code OffsetDateTime}
|
2017-08-12 06:50:25 +02:00
|
|
|
* @return the long value
|
|
|
|
*/
|
|
|
|
@TypeConverter
|
2020-10-18 08:16:55 +02:00
|
|
|
public static Long offsetDateTimeToTimestamp(final OffsetDateTime offsetDateTime) {
|
|
|
|
return offsetDateTime == null ? null : offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC)
|
|
|
|
.toInstant().toEpochMilli();
|
2017-08-12 06:50:25 +02:00
|
|
|
}
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
@TypeConverter
|
2020-03-31 19:20:15 +02:00
|
|
|
public static StreamType streamTypeOf(final String value) {
|
2018-01-15 21:30:52 +01:00
|
|
|
return StreamType.valueOf(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
2020-03-31 19:20:15 +02:00
|
|
|
public static String stringOf(final StreamType streamType) {
|
2018-01-15 21:30:52 +01:00
|
|
|
return streamType.name();
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
@TypeConverter
|
2020-03-31 19:20:15 +02:00
|
|
|
public static Integer integerOf(final FeedGroupIcon feedGroupIcon) {
|
2019-04-28 22:43:54 +02:00
|
|
|
return feedGroupIcon.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
2020-03-31 19:20:15 +02:00
|
|
|
public static FeedGroupIcon feedGroupIconOf(final Integer id) {
|
2020-08-16 10:24:58 +02:00
|
|
|
for (final FeedGroupIcon icon : FeedGroupIcon.values()) {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (icon.getId() == id) {
|
|
|
|
return icon;
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("There's no feed group icon with the id \"" + id + "\"");
|
|
|
|
}
|
2017-08-12 06:50:25 +02:00
|
|
|
}
|