Skip to content

Instantly share code, notes, and snippets.

@HarryTylenol
Last active December 8, 2019 05:49
Show Gist options
  • Save HarryTylenol/bc59cce222e9067a92c05e1a96aaf327 to your computer and use it in GitHub Desktop.
Save HarryTylenol/bc59cce222e9067a92c05e1a96aaf327 to your computer and use it in GitHub Desktop.
fun <T> intentExtra(defaultValue: T? = null) = object : ReadOnlyProperty<Activity, T?> {
override fun getValue(thisRef: Activity, property: KProperty<*>): T? {
return when (defaultValue) {
is String -> thisRef.intent.getStringExtra(property.name) as T
is Int -> thisRef.intent.getIntExtra(property.name, defaultValue) as T
is Long -> thisRef.intent.getLongExtra(property.name, defaultValue) as T
// And More...
else -> null
}
}
}
class FestivalActivity : BaseActivity<ActvFestivalBinding, FestivalViewModel>() {
object FestivalDeepLinkIntents : DeeplinkActivityIntents {
@JvmStatic
@CommonUrlDeepLink("festivals/{extraId}")
fun intentForDeepLinkMethod(context: Context) =
createTaskBuilder(context,
Intent(context, MainActivity::class.java),
Intent(context, FestivalActivity::class.java)
)
}
override val viewModel: FestivalViewModel by viewModel()
override val layoutResId: Int = R.layout.actv_festival
private val extraId by intentExtra<String>()
override fun onCreate(savedInstanceState: Bundle?) {
loadKoinModules(FestivalModuleRepository.modules)
super.onCreate(savedInstanceState)
setupToolbar(binding.toolbar, true)
binding.viewModel = viewModel
extraId?.let(viewModel::requestFestival) ?: finish()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment