8000 Intent builder · Issue #166 · android/android-ktx · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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.
Intent builder #166
Open
Open
@gildor

Description

@gildor

Kotlin stdlib uses lambdas to build different types of complex objects:
buildString
buildSequence
List builder

I propose to add intent builder to ktx. That will work like:

buildIntent {
   action = "some.Action"
   data = "http://example.com".toUri()
   putExtra("name", value)
}

Possible implementation:

inline fun buildIntent(initializer: Intent.() -> Unit) = Intent().apply { initializer() }

Also we can move often used properties to arguments of the function, such as action:

inline fun buildIntent(action: String) = Intent(action).apply { initializer() }

but not sure that this is make a lot of sense.

But another feature would more useful. Intent builder with reified generic instead class argument:

inline fun <reified T> buildIntent(
        context: Context, 
        initializer: Intent.() -> Unit
) = Intent(context, T::class.java).apply { initializer() }

This version also can provide empty lambda as default argument for initilizer, to make builder usage optional, reified acitivity creation already useful feature

Possible naming:

Intent (same as List builder, that looks like constructor call but actually function)
buildIntent (same as stringBuilder)
intent is short but doesn't follow current Kotlin naming and clashes with variable names

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0