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

API for getting intent extras within an Activity implementation #265

@gabrielhuff

Description

@gabrielhuff

I think it would be nice to have something for accessing argument intent extras within an Activity implementation.

I'm proposing two alternatives here (I'd personally go for the second one):

Alternative 1 - Minimalist, unsafe at compile time

class MyActivity : Activity {
  
  private val id: Long? by extra("id")

  private val timestamp: Long by extra("timestamp")

  private val name: String by extra("name")
}

// Pseudo implementation, just to show the equivalency to lazy
inline fun <reified T> Activity.extra(key: String): ReadOnlyProperty<Activity, T> {
  return lazy { intent.get(key) as T }
}

Alternative 2 - Robust, safe at compile time

class MyActivity : Activity {
  
  private val id by Extra.Nullable.long("id")

  private val timestamp by Extra.long("timestamp")

  private val name by Extra.string("name")
}

object Extra {

    fun long(key: String): ReadOnlyProperty<Activity, Long> = TODO()

    fun string(key: String): ReadOnlyProperty<Activity, Int> = TODO()

    // ...
    
    object Nullable {

        fun long(key: String): ReadOnlyProperty<Activity, Long> = TODO()

        // ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions