Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Property delegates for Activity extras #440

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Property delegates for Activity extras #440

wants to merge 5 commits into from

Conversation

gabrielhuff
Copy link

@gabrielhuff gabrielhuff commented Mar 17, 2018

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

@googlebot
Copy link

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

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

@gabrielhuff
Copy link
Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@dovahkiin98
Copy link

You're missing the default value parameter

@gabrielhuff
Copy link
Author

@dovahkiin98 What do you mean? Where?

@dovahkiin98
Copy link

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

@gabrielhuff
Copy link
Author

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

- Add example with default value
- Document default function
@JakeWharton
Copy link
Contributor

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.

@gabrielhuff
Copy link
Author

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?

@JakeWharton
Copy link
Contributor

A lateinit property is an order of magnitude less expensive.

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

@gabrielhuff
Copy link
Author

gabrielhuff commented Mar 19, 2018

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants