Android Architecture
Android Architecture
Santiago Beroch
·
The project
We will be using the Rick and Morty API, from where we will extract
character information to show them in a RecyclerView, and by clicking
one of its items the app will display the detail of one character. Simple.
The app
ui: Our Fragments and ViewModels helping to display data to the user.
One last thing before continuing. The API returns this response when
asking for a list of characters
Hilt
This is not a DI tutorial so I strongly advise to get a little more familiar
with the concept before continuing. I’ll briefly explain it anyway.
Our next step is to now create our Module inside the di folder. If you
are not so familiar with di concepts, think of the module as the “bag”
from where we will get our dependencies from. (I’ll show many
dependencies from upcoming sections of this tutorial, so don’t be
scared if you don’t understand all of them now).
AppModule.kt
Note that Room has LiveData support, so you can get observable values
from the database. And Kotlin Coroutines makes it even easier by
letting us suspend our Room functions.
First we need to let our LiveData know that we are looking for
the Character, so that it should have a LOADING state.
Then, we would like to get that character from the local data
source, because it is faster than getting it from the internet. If it
finds it, we are changing the state to a SUCCESS
Further Steps
We made it to the end. I hope I was clear throughout the tutorial.
We are syncing all the time with the strategy I presented, and
your application might not need it. Keep your needs in mind
and develop a smart strategy.
Also, we are only performing get operations. What if we need to
upload something to a server? The strategy will also be
different in such case.
The API paginates its characters. Why don’t we? Take a look at
Paging Library by Jetpack.
That’s all. Remember you can find all the code I used in the GitHub
repository
sberoch/RickAndMorty-AndroidArchitectureSample
A Rick And Morty simple app to show one approach to using some of the best practices
in Android Development. …
github.com
And if any of this was useful to you, leave a Clap! I would be very
grateful and motivated for a next tutorial.
Source : https://itnext.io/android-architecture-hilt-mvvm-kotlin-coroutines-live-data-room-and-retrofit-
ft-8b746cab4a06