Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

start() method, resources and broadcastReceiver #83

Open yoavst opened 9 years ago

yoavst commented 9 years ago
inline public fun <reified T : Activity> Activity.start() {
    startActivity(intentFor<T>())
}

inline public fun <reified T: Service> Context.start() {
    this.startService(intentFor<T>())
}
val number = intRes(R.integer.number)
val dimenPixelSize = dimenRes(R.dimen.size)
val color = colorRes(R.color.red)
val intArray = intArrayRes(R.array.ids)
val stringArray = stringArrayRes(R.array.options)
val drawable = drawableRes(R.drawable.background)

// Delegation part (lazy because of Android...):
val text by stringResource(R.string.text)
val number by intResource(R.integer.number)
val dimenPixelSize by dimenResource(R.dimen.size)
val color by colorResource(R.color.red)
val intArray by intArrayResource(R.array.ids)
val stringArray by stringArrayResource(R.array.options)
val drawable by drawableResource(R.drawable.background)

Can include something similar in Anko?

public fun broadcastReceiver(init: (Context, Intent?) -> Unit): BroadcastReceiver {
    return object : BroadcastReceiver() {
        public override fun onReceive(context: Context, intent: Intent?) {
            init(context, intent)
        }
    }
}
yanex commented 9 years ago

:+1: !

yoavst commented 9 years ago

Also, I've noticed that AnkoLogger.loggerTag is recalculated every time. Maybe it is better to switch to lazy delegate.

yanex commented 9 years ago

By the way, why startActivity is bad? I think that the name start is not concrete. Below that, it can easily clash with something in user code.

yoavst commented 9 years ago

Because Intellij doesn't auto-import the extension method, since there is regular method with the same name.

udev commented 7 years ago

Any word on those resources?