-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewBindingActivity.kt
130 lines (108 loc) · 4.23 KB
/
ViewBindingActivity.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
/*
* class ProfileActivity : AppCompatActivity() {
private val binding by viewBinding(ProfileLayoutBinding::inflate)
// use binding freely... no need to clear it out!
}
* */
abstract class ViewBindingActivity<VB : ViewBinding> : AppCompatActivity() {
val activityLauncher: BetterActivityResult<Intent, ActivityResult> =
BetterActivityResult.registerActivityForResult(this)
private var _binding: ViewBinding? = null
abstract val bindingInflater: (LayoutInflater) -> VB
@Suppress("UNCHECKED_CAST")
protected val mBinding: VB
get() = _binding as VB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = bindingInflater.invoke(layoutInflater)
setContentView(requireNotNull(_binding).root)
preset()
}
abstract fun preset()
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
class BetterActivityResult<Input, Result> private constructor(
caller: ActivityResultCaller,
contract: ActivityResultContract<Input, Result>,
private var onActivityResult: OnActivityResult<Result>?
) {
/**
* Callback interface
*/
interface OnActivityResult<O> {
/**
* Called after receiving a result from the target activity
*/
fun onActivityResult(result: O)
}
private val launcher: ActivityResultLauncher<Input>
fun setOnActivityResult(onActivityResult: OnActivityResult<Result>?) {
this.onActivityResult = onActivityResult
}
/**
* Launch activity, same as [ActivityResultLauncher.launch] except that it allows a callback
* executed after receiving a result from the target activity.
*/
/**
* Same as [.launch] with last parameter set to `null`.
*/
@JvmOverloads
fun launch(input: Input, onActivityResult: OnActivityResult<Result>? = this.onActivityResult) {
if (onActivityResult != null) {
this.onActivityResult = onActivityResult
}
launcher.launch(input)
}
private fun callOnActivityResult(result: Result) {
if (onActivityResult != null) onActivityResult!!.onActivityResult(result)
}
companion object {
/**
* Register activity result using a [ActivityResultContract] and an in-place activity result callback like
* the default approach. You can still customise callback using [.launch].
*/
fun <Input, Result> registerForActivityResult(
caller: ActivityResultCaller,
contract: ActivityResultContract<Input, Result>,
onActivityResult: OnActivityResult<Result>?
): BetterActivityResult<Input, Result> {
return BetterActivityResult(caller, contract, onActivityResult)
}
/**
* Same as [.registerForActivityResult] except
* the last argument is set to `null`.
*/
fun <Input, Result> registerForActivityResult(
caller: ActivityResultCaller,
contract: ActivityResultContract<Input, Result>
): BetterActivityResult<Input, Result> {
return registerForActivityResult(caller, contract, null)
}
/**
* Specialised method for launching new activities.
*/
fun registerActivityForResult(
caller: ActivityResultCaller
): BetterActivityResult<Intent, ActivityResult> {
return registerForActivityResult(caller, StartActivityForResult())
}
}
init {
launcher = caller.registerForActivityResult(
contract
) { result: Result -> callOnActivityResult(result) }
}
}