-
Notifications
You must be signed in to change notification settings - Fork 4
Solve routine problems
In the previous sections, we covered in details basic flow and screen creation. Even though constructing UI and managing navigation is a huge part of the android developer's routine, in real life, we have more profound cases to cover: data loading, error handling, handling screen results and much more.
Kompot framework has the instruments to handle this routine just from the box.
Let's find out what else we can do with our screen with an example of the contacts screen. In the previous section, we created a flow of adding a new contact. And now we'll use a screen that shows a list of all user's contacts to cover more complex use cases of the Kompot framework.
You can find a step by step explanation of how to create a screen in the previous sections. Or you can follow this link to find the full code of the contacts screen.
First of all, imagine that we have a contacts data source from where we fetch all current contacts. ContactRepository
will be our interface to fetch this long loading data.
Every screen and flow gives you an API to launch coroutines. The framework keeps track of all the jobs instantiated and cancels them automatically when the screen is hidden or destroyed.
In our case, we can easily launch coroutine that loads contacts in the onCreate
method using the tillFinish
high-order function. You can also use withLoading
high-order function to wrap requests with the loading spinner.
override fun onCreated() {
super.onCreated()
loadContacts()
}
private fun loadContacts() {
tillFinish {
val contacts = withLoading {
repository.getContacts()
}
updateState {
copy(contacts = contacts)
}
}
}
Let's imagine that the getContacts()
method is a coroutine flow that observes the room database and could therefore return multiple events. How would we handle the request in that case?
Kompot provides the set of methods to handle coroutine flows as well.
All we need to do is to rewrite the loadContacts
method from above in the following way:
private fun loadContacts() {
repository.getContacts()
.collectTillFinish { contacts ->
updateState {
copy(contacts = contacts)
}
}
}
Please note,
tillFinish
andcollectTillFinish
methods have the correspondingtillHide
andcollectTillHide
methods that launch coroutines in the scope that is active while the screen is visible to the user.
Sometimes handling navigation in the flow is not sufficient. Let's imagine that we need to reuse the contacts screen in the other parts of the app. How do we handle the "Add contact" button then? Adding add contact sequence logic to each flow that wants to use our screen will be overhead.
Therefore Kompot framework comes with the solution of modal screens and flows. By calling showModal()
, you can open any other screen and flow in the modal window and then handle the result. With this approach, the contacts screen becomes independent, and any part of the app can use it without knowing the details about the flow of adding a contact.
Here is how you can call add contact flow from the contacts screen:
override fun onActionClick() {
AddContactFlow().showModal {
loadContacts()
}
}
The result of the flow handled in a lambda that showModal()
receives as a parameter. We'll reload contacts there to keep our screen up to date.
We covered how to create a flow of screens in the Build a flow of screens page. Full code available via this link.
Here is the final result: