This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChatViewModel( | |
loadChatMessagesUseCase: LoadChatMessagesUseCase, | |
private val getChatMessagesUseCase: GetChatMessagesUseCase, | |
private val sendTextMessageUseCase: SendTextMessageUseCase, | |
private val user: User | |
) : BaseViewModel() { | |
data class State( | |
val messages: List<MessageUiModel> | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun createMoshi(): Moshi { | |
return Moshi.Builder() | |
.add(createEnumJsonAdapter<Role>()) | |
.add(createEnumJsonAdapter<Category>()) | |
.add(createEnumJsonAdapter<AnotherEnum>()) | |
// ... | |
.build() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> createEnumJsonAdapter(): JsonAdapter<T> where T : Enum<T>, T : IEnumValue { | |
return object : JsonAdapter<T>() { | |
@FromJson | |
override fun fromJson(reader: JsonReader): T? { | |
return if (reader.peek() != JsonReader.Token.NULL) { | |
enumValues<T>().firstOrNull { it.value == reader.nextInt() } | |
} else { | |
reader.nextNull() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericEnumJsonAdapter<T> : JsonAdapter<T>() { | |
@FromJson | |
override fun fromJson(reader: JsonReader): T? { | |
// Cannot access enumValues<T>() | |
} | |
@ToJson | |
override fun toJson(writer: JsonWriter, value: T?) { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class Role(override val value: Int) : IEnumValue { | |
Admin(1), | |
Moderator(2) | |
} | |
object GenericEnumFactory { | |
inline fun <reified T> fromValueOrNull(rawValue: Int?): T? where T : Enum<T>, T : IEnumValue { | |
return enumValues<T>().firstOrNull { it.value == rawValue } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IEnumValue { | |
val value: Int | |
} | |
enum class Role(override val value: Int) : IEnumValue { | |
Admin(1), | |
Moderator(2); | |
companion object { | |
fun fromValueOrNull(value: Int): Role? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoleEnumJsonAdapter : JsonAdapter<Role>() { | |
@FromJson | |
override fun fromJson(reader: JsonReader): Role? { | |
return if (reader.peek() != JsonReader.Token.NULL) { | |
Role.fromValueOrNull(reader.nextInt()) | |
} else { | |
reader.nextNull() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class Role(val value: Int) { | |
Admin(1), | |
Moderator(2); | |
companion object { | |
fun fromValueOrNull(value: Int): Role? { | |
return values().firstOrNull { it.value == value } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "John", | |
"role": 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "John", | |
"role": "Admin" | |
} |
NewerOlder