1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-12 17:57:29 +00:00

Convert Converters to kotlin

This commit is contained in:
Ammar Githam 2021-06-08 21:29:47 +09:00
parent b7d74964fa
commit 34e2ba429c

View File

@ -1,38 +1,33 @@
package awais.instagrabber.db;
package awais.instagrabber.db
import androidx.room.TypeConverter;
import androidx.room.TypeConverter
import awais.instagrabber.models.enums.FavoriteType
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import awais.instagrabber.models.enums.FavoriteType;
public class Converters {
object Converters {
@JvmStatic
@TypeConverter
public static FavoriteType fromFavoriteTypeString(String value) {
try {
return FavoriteType.valueOf(value);
} catch (Exception e) {
return null;
fun fromFavoriteTypeString(value: String?): FavoriteType? =
if (value == null) null
else try {
FavoriteType.valueOf(value)
} catch (e: Exception) {
null
}
}
@JvmStatic
@TypeConverter
public static String favoriteTypeToString(FavoriteType favoriteType) {
return favoriteType == null ? null : favoriteType.toString();
}
fun favoriteTypeToString(favoriteType: FavoriteType?): String? = favoriteType?.toString()
@JvmStatic
@TypeConverter
public static LocalDateTime fromTimestampToLocalDateTime(Long value) {
if (value == null) return null;
return LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.systemDefault());
}
fun fromTimestampToLocalDateTime(value: Long?): LocalDateTime? =
if (value == null) null else LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.systemDefault())
@JvmStatic
@TypeConverter
public static Long localDateTimeToTimestamp(LocalDateTime localDateTime) {
if (localDateTime == null) return null;
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}
fun localDateTimeToTimestamp(localDateTime: LocalDateTime?): Long? = localDateTime?.atZone(ZoneId.systemDefault())?.toInstant()?.toEpochMilli()
}