android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

Property delegates for Activity extras

Open gabrielhuff opened this issue 8 years ago • 11 comments

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" }
}

gabrielhuff avatar Mar 17 '18 15:03 gabrielhuff

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
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.

googlebot avatar Mar 17 '18 15:03 googlebot

I signed it!

gabrielhuff avatar Mar 17 '18 15:03 gabrielhuff

CLAs look good, thanks!

googlebot avatar Mar 17 '18 15:03 googlebot

You're missing the default value parameter

dovahkiin98 avatar Mar 18 '18 15:03 dovahkiin98

@dovahkiin98 What do you mean? Where?

gabrielhuff avatar Mar 18 '18 16:03 gabrielhuff

@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 avatar Mar 18 '18 16:03 dovahkiin98

@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.

gabrielhuff avatar Mar 18 '18 16:03 gabrielhuff

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.

JakeWharton avatar Mar 19 '18 14:03 JakeWharton

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?

gabrielhuff avatar Mar 19 '18 14:03 gabrielhuff

A lateinit property is an order of magnitude less expensive.

I have no idea what "same extra on different scopes" means.

JakeWharton avatar Mar 19 '18 14:03 JakeWharton

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.

gabrielhuff avatar Mar 19 '18 14:03 gabrielhuff