Android Componentization with Ogya
Ogya is a set of tools for quick android development. It gives you a consistent way to display dialogs and load lists with only one recycler adapter. The adapter can handle multiple view types.
If this is your first encounter with Ogya. Then please read this
With Ogya we have the power to use one listAdapter for all. However the binding logic was coupled with each instance of the listableAdapter. This led to duplication of code , especially if you needed the same list item in multiple places. Componentization helps us build reusable components that behave exactly the same way where ever we invoke it. By the way you can also use it on normal views too. Lets get started!
In previous versions of Ogya, the listableBindingListener looked something like this
This is quite ok. But Imagine you had a project where you had to display persons at multiple places. You would need to set the properties over and over again in all places. But with Componentization. Its done at one place and the change ripples across your entire project.
Componentization Axioms
- A component should have its dependencies injected into it
- A components state is determined by the listable object that’s passed to it.
Origins
All components are derived from the abstract class BaseComponent.
It’s a generic abstract class that expects a ViewDataBinding Type and a Listable (any class that extends Listable)Type. The ViewDataBinding files are generated classes created by any layout file which has a root parent of <layout>
.
Function: Render
The render method is used to display the component.
Function: ListableType
The listableType function returns the listableType of the component.
Lets Dive In
The Kotlin language provides as with beautiful structures. One such structure is object.
An object is a thread-safe singleton class. Our derived components are all objects. They have no constructors and they do not keep state. The only state they know of is the state of the listable.
Then …
This makes it easy to reuse code. As long as dependencies are provided for render, the components can be recreated with its expected behavior.
BaseComponents are found in Ogya : 0.54
You can find Ogya on GitHub by following this link