Property delegates for Activity extras
This implements #265. Usage:
class MyActivity : Activity {
private val id by extra<Long?>("id")
private val timestamp by extra<Long>("timestamp")
private val name by extra<String>("name") { "Default Name" }
}
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
:memo: Please visit https://cla.developers.google.com/ to sign.
Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.
What to do if you already signed the CLA
Individual signers
- It's possible we don't have your GitHub username or you're using a different email address on your commit. Check your existing CLA data and verify that your email is set on your git commits.
Corporate signers
- Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to
go/cla#troubleshoot. - The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
- The email used to register you as an authorized contributor must also be attached to your GitHub account.
I signed it!
CLAs look good, thanks!
You're missing the default value parameter
@dovahkiin98 What do you mean? Where?
@gabrielhuff when you use intent.getStringExtra("extra", "") you provide a default value, it's not always 0 or "" or false, sometimes it's different. I would suggest this implementation :
inline fun <T> Activity.extra(key: String, defaultValue: T) = lazy {
when(T::class.java) {
Int::class.java -> intent.getIntExtra(key, defaultValue) as T
Float::class.java -> intent.getFloatExtra(key, defaultValue) as T
...
}
}
val intExtra: Int by extra("intExtra", 2)
@dovahkiin98 The current implementation allows you to define default values. The only difference is that we are getting them from a function instead of a raw value:
val intExtra by extra("intExtra") { 2 }
Note that you can omit the default value function. If so, the implementation will throw an IllegalArgumentException if no extra was passed.
Delegates are an expensive hammer to use for this. I don't think we've decided whether their use is appropriate for simple things like this.
IMHO delegates (in this case, specifically lazy instantiation) are perfect for this. Consider a scenario in which you need to access the same extra on different scopes. What would be the most appropriate way to do it?
A lateinit property is an order of magnitude less expensive.
I have no idea what "same extra on different scopes" means.
By different scopes I mean different methods / contexts. We don't want to read the same extra from the input intent more than once (not because of performance - just because the call might be kinda ugly):
override fun onCreate(state: Bundle?) {
...
val extra = intent.getParcelableExtra("my_key") as MyCustomExtra
}
fun onSomethingElse() {
val extra = intent.getParcelableExtra("my_key") as MyCustomExtra // Eww
}
But I get what you're saying. Performance-wise, lateinit would be ideal. However, the property would have to be a var and would require an additional line of code somewhere else for instantiation.