1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47: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; object Converters {
import java.time.LocalDateTime; @JvmStatic
import java.time.ZoneId;
import java.time.ZoneOffset;
import awais.instagrabber.models.enums.FavoriteType;
public class Converters {
@TypeConverter @TypeConverter
public static FavoriteType fromFavoriteTypeString(String value) { fun fromFavoriteTypeString(value: String?): FavoriteType? =
try { if (value == null) null
return FavoriteType.valueOf(value); else try {
} catch (Exception e) { FavoriteType.valueOf(value)
return null; } catch (e: Exception) {
} null
} }
@JvmStatic
@TypeConverter @TypeConverter
public static String favoriteTypeToString(FavoriteType favoriteType) { fun favoriteTypeToString(favoriteType: FavoriteType?): String? = favoriteType?.toString()
return favoriteType == null ? null : favoriteType.toString();
}
@JvmStatic
@TypeConverter @TypeConverter
public static LocalDateTime fromTimestampToLocalDateTime(Long value) { fun fromTimestampToLocalDateTime(value: Long?): LocalDateTime? =
if (value == null) return null; if (value == null) null else LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.systemDefault())
return LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.systemDefault());
}
@JvmStatic
@TypeConverter @TypeConverter
public static Long localDateTimeToTimestamp(LocalDateTime localDateTime) { fun localDateTimeToTimestamp(localDateTime: LocalDateTime?): Long? = localDateTime?.atZone(ZoneId.systemDefault())?.toInstant()?.toEpochMilli()
if (localDateTime == null) return null;
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
} }