MVVM architecture with firebase firestore android series | Java part (2/2)
Fact: Samsung had the opportunity to buy Android in late 2004 but passed the opportunity because they did not see any potential in it
Hello again, If you have not read my first post please read it first from below link, if you want to jump to example directly then keep reading
As I promised, I will not bored with you theory.lets get our hand dirty. But let me give you some idea about what we are trying to do here.
We will fetch list of blog store in firestore and display in our application using MVVM architecture pattern.
Source code can be access from here.
Here is our content:
- Create project and add internet permission
- Add dependency(If using image please add Glide as well)
- Connect you app with firebase and enable firestore rule as public.
- Model class (Blog.java)
- View Model (ListViewModel.java)
- Repository class (BlogRepository.java)
- View class (MainActivity.java,activity_main.xml)
- Adapter to inflate list (BlogAdapter.java,single_blog_item.xml)
- Output
- TL;DR
Lets start from first, while reading if you have not understand any code , please leave a comment I will update this post.
1.Create new project and in AndroidManifest.xml add internet permission
2.Add dependency in build.gradle file, add glide if you are using image in your app.
3.Okay now go to tool on top menu, choose firebase and connect your app with firebase.
Our database structure:
Once app is connect with firebase,Make sure you have google-services.json in app folder. make firestore rule public accessible
4.Create Model class (Blog.java)
5.Create new View Model (ListViewModel.java)
- call repository to get all the blog list from firebase. since don’t have complex operation keep data in view model till view observe.
6.Create new Repository class (BlogRepository.java)
- get Blog from firebase and send to viewmodel
7.Now in View class (MainActivity.java,activity_main.xml)
- In activity_main.xml
- In MainActivity.java
get blog with help of viewmodel, now once we call viewmodel, It will maintain your app state even after screen rotation.
8.Create Adapter and single xml to inflate list (BlogAdapter.java,single_blog_item.xml)
- In single_blog_item.xml
- In BlogAdapter.java
Now run the app and you should able to get the list of blog from firebase.
Update: If you are getting error like Default FirebaseApp is not initialized then make sure you add google-services dependency as below
In project level build.gradle
And now in app level build.gradle if you are using newer version add
if you are using older version than add this line at bottom
Important Point to Note : Here is the advantage of using MVVM
- Your code look cleaner
- If you rotate screen it will not call request to firebase again since viewmodel maintain you app state
- You can remove view and replace with new one easily.
9. Output
10.TL;DR
- In this post we have learned how to implment firebase with mvvm architecture in our application
- We took and example to get list of blog from firestore and display in UI.
That all, Thanks guys, I hope you like this blog. please make sure to give clap 👏 and leave some engagement.