Last active
April 18, 2023 11:25
-
-
Save jamiesanson/d1a3ed0910cd605e928572ce245bafc4 to your computer and use it in GitHub Desktop.
Kotlin Property Delegate for Fragment view lifecycle binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Fragment.viewLifecycle(bindUntilEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY): ReadWriteProperty<Fragment, T> = | |
object: ReadWriteProperty<Fragment, T>, LifecycleObserver { | |
// A backing property to hold our value | |
private var binding: T? = null | |
private var viewLifecycleOwner: LifecycleOwner? = null | |
init { | |
// Observe the View Lifecycle of the Fragment | |
this@viewLifecycle | |
.viewLifecycleOwnerLiveData | |
.observe(this@viewLifecycle, Observer { newLifecycleOwner -> | |
viewLifecycleOwner | |
?.lifecycle | |
?.removeObserver(this) | |
viewLifecycleOwner = newLifecycleOwner.also { | |
it.lifecycle.addObserver(this) | |
} | |
}) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_ANY) | |
fun onDestroy(event: Lifecycle.Event) { | |
if (event == bindUntilEvent) { | |
// Clear out backing property just before onDestroyView | |
binding = null | |
} | |
} | |
override fun getValue( | |
thisRef: Fragment, | |
property: KProperty<*> | |
): T { | |
// Return the backing property if it's set | |
return this.binding!! | |
} | |
override fun setValue( | |
thisRef: Fragment, | |
property: KProperty<*>, | |
value: T | |
) { | |
// Set the backing property | |
this.binding = value | |
} | |
} | |
} | |
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) { | |
this.property = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apologies, I know this is incorrect and I've been meaning to update the blog post + gist. This is where I've settled - slightly more refined and reusable: https://gist.github.com/jamiesanson/478997780eb6ca93361df311058dc5c2