-
Notifications
You must be signed in to change notification settings - Fork 15
/
ActivityCompanion.kt
48 lines (35 loc) · 1.56 KB
/
ActivityCompanion.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@file:Suppress("NOTHING_TO_INLINE")
package me.eugeniomarletti.extras
import android.app.Activity
import android.content.Context
import android.content.Intent
import kotlin.reflect.KClass
abstract class SimpleActivityCompanion(kclass: KClass<out Activity>) {
@PublishedApi internal val javaClass = kclass.java
inline fun intent(context: Context) = Intent(context, javaClass)
inline fun start(context: Context) {
context.startActivity(intent(context))
}
}
abstract class ActivityCompanion<out IntentOptions>(
@PublishedApi internal val intentOptions: IntentOptions,
kclass: KClass<out Activity>
) : SimpleActivityCompanion(kclass) {
inline fun intent(context: Context, configure: IntentOptions.(Intent) -> Unit): Intent =
intent(context).apply { configure(intentOptions, this) }
inline fun start(context: Context, configure: IntentOptions.(Intent) -> Unit) {
context.startActivity(intent(context, configure))
}
inline fun <T> Intent.options(block: IntentOptions.(Intent) -> T): T =
block(intentOptions, this)
}
abstract class SelfActivityCompanion<out IntentOptions>(
kclass: KClass<out Activity>
) : SimpleActivityCompanion(kclass) {
@Suppress("UNCHECKED_CAST")
inline fun intent(context: Context, configure: IntentOptions.(Intent) -> Unit): Intent =
intent(context).apply { configure(this@SelfActivityCompanion as IntentOptions, this) }
inline fun start(context: Context, configure: IntentOptions.(Intent) -> Unit) {
context.startActivity(intent(context, configure))
}
}