PYDroid 20.4.0 just release with support for the new architecture framework that I've been building out - but I'm already hard at work on a fancy new version 20.5.0 which will include support for hooks to make it easier to extend and implement your code into the UiView based arch framework.
Here's a sneak peak of what's to come!
Before, when implementing your own extension of the UiView class, your inflate and teardown logic would look like this:
class MyUiView : UiView<*,*>() {
override fun onInflate(view: View, savedInstanceState: Bundle?) {
foo()
bar()
}
override fun onTeardown() {
fii()
baz()
}
}
Generally speaking, this was not too bad in the case where
MyUiView
was a final class, but what about when your class was open
and would be extended further by another UiView? Then we ran into issues. If you wanted to run a third function on inflate and teardown such as fizz()
and buzz()
, you would need to either mark onInflate
and onTeardown
as final, and build something like your own onPostInflate(view: View, savedInstanceState: Bundle?)
function to make sure that any classes which extend MyUiView
don't forget to call through its onInflate
, or you would override the inflate function in child classes and pray that you remember to call through to super
. Both of these were problematic, as they were either error prone, or just plain annoying.
Enter hooks.
Now instead of overriding
onInflate
and onTeardown
, you can do something like the following:
class MyUiView : UiView<*,*>() {
init {
doOnInflate {
foo()
bar()
}
doOnInflate {
fizz()
}
doOnTeardown {
fii()
baz()
}
doOnTeardown {
buzz()
}
}
}
Well that looks interesting, but what does it all do? The
doOnInflate(onInflate: (savedInstanceState: Bundle?) -> Unit)
hook replaces onInflate
and the doOnTeardown(onTeardown: () -> Unit))
hook replaces onTeardown
. The hooks execute their lambda functions at the same points in the lifecycle as the old brittle methods did, and they are guaranteed to run in set orders. This way you can compose together various bits of functionality without having to worry about the execution order or how to extend from a base class.The
doOnInflate
hook will run its events in FIFO order, meaning the first function called in a doOnInflate
hook will run before all the others. Conversely, the doOnTeardown
hook will run in LIFO order, meaning the events run in reverse - the last event entered will be the first to run on teardown. A similar style hook for onSaveState(outState: Bundle)
exists called doOnSaveState(onSaveState: (outState: Bundle) -> Unit)
which does not make any guarantees on the execution order of its events.
Very similar hooks exist for
UiViewModel
as well to replace the brittle onInit()
and onTeardown
methods.
These hooks will eventually completely replace their old method counterparts, but at least in 20.5.0 the old methods will only be soft deprecated. They will still continue to work as normal, making 20.5.0 completely backwards compatible with 20.4.0!
Hop over to GitHub to see the source!
========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?
Send me an email at: pyam.soft@gmail.com
Or find me online at: https://pyamsoft.blogspot.com
Follow my Facebook Page
Check out my code on GitHub
=========================