simplify ControllerViewBindingDelegate

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2023-01-16 20:23:22 +01:00
parent b45794b0ca
commit b95399750d
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 7 additions and 10 deletions

View File

@ -378,17 +378,11 @@ class AccountVerificationController(args: Bundle? = null) :
if (eventStatus.eventType == EventStatus.EventType.PUSH_REGISTRATION) {
if (internalAccountId == eventStatus.userId && !eventStatus.isAllGood && activity != null) {
activity!!.runOnUiThread {
// try {
binding?.progressText?.text =
"""
${binding?.progressText?.text}
${resources!!.getString(R.string.nc_push_disabled)}
""".trimIndent()
// } catch (npe: NullPointerException) {
// // view binding can be null
// // since this is called asynchronously and UI might have been destroyed in the meantime
// Log.i(TAG, "UI destroyed - view binding already gone")
// }
}
}
fetchAndStoreCapabilities()

View File

@ -25,12 +25,12 @@ import com.bluelinelabs.conductor.Controller
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
fun <T : ViewBinding> Controller.viewBinding(bindingFactory: (View) -> T?) =
fun <T : ViewBinding> Controller.viewBinding(bindingFactory: (View) -> T) =
ControllerViewBindingDelegate(this, bindingFactory)
class ControllerViewBindingDelegate<T : ViewBinding>(
controller: Controller,
private val viewBinder: (View) -> T?
private val viewBinder: (View) -> T
) : ReadOnlyProperty<Controller, T?>, LifecycleObserver {
private var binding: T? = null
@ -43,7 +43,10 @@ class ControllerViewBindingDelegate<T : ViewBinding>(
})
}
override fun getValue(thisRef: Controller, property: KProperty <*>): T? {
return binding ?: thisRef.view?.let { viewBinder(it).also { binding = it } }
override fun getValue(thisRef: Controller, property: KProperty<*>): T? {
if (binding == null) {
binding = thisRef.view?.let { viewBinder(it) }
}
return binding
}
}