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
private inline fun <reified T> String.asModel(): T = Gson().fromJson(this, T::class.java) | |
// val result: String = ... | |
// val model = result.asModel<AnyModel>() |
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 Mapper<D, M> { | |
fun toModel(data: D): M | |
fun toData(model: M): D | |
} | |
object ModelMapper : Mapper<Data, Model> { | |
override fun toModel(data: Data): Model = with(data) { | |
Model(...) | |
} |
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 : Activity> Activity.launchActivity(extras: Bundle? = null) { | |
val intent = Intent(this, T::class.java).apply { | |
extras?.let { putExtras(it) } | |
} | |
startActivity(intent) | |
} | |
inline fun <reified T : Any> Activity.extra(key: String, default: T) = lazy { | |
val value = intent?.extras?.get(key) | |
if (value is T) value else default |