Modular Programming in Android with MVVM (Kotlin) — Part 2
This story describes the structure of modules created as part of sample application and the approach that is followed for this.
Link to Part 1:
Modular Programming in Android with MVVM (Kotlin) — Part 1
This story describes basics of modularization of mobile application during development with sample application.
The below are the considerations followed for this sample modular project:
- Kotlin — Programming language
- Retrofit — API calls
- RoomDB — Local DB
- Kotlin Coroutines
- All JetPack Components
Modular Approach :
In this sample project, I followed the below approach for creating modules:
One App Module — Which is the starting point of the application
Library Modules — Remaining all the modules are library modules
Detail Explanation of Modules:
AppModule: The “AppModule” is named as “MVVMLauncher” which is the starting point of the application. When the app is launched , we will be shown list of items through which we can navigate to other modules accordingly. Please find below screenshot for the same.
All other modules to be included under dependencies as those modules will be launched from this app module when clicked on any of the list item.
Clicking on each of the item will navigate to corresponding screens of the respective library modules.
Library Modules: All the modules created other than launcher module are library modules which includes some common library modules that can be accessed across all other modules.
Common Library Modules:
There is library module with name “Common” that can common features that can be used across all other modules as dependency.
This modules contains below features:
(1) Dependency Injection (DI) folder that contains Retrofit module that can be accessed globally.
(2)A common BottomSheet dialog that accepts “layout” dynamically and can be used across all other modules to display customized bottom sheet when needed.
(3)Utils class that contains common code for ToolBar that can be used by Activities/Fragments to display ToolBar.
Please find Screen shot for the same:
2.AppNavigator Module: This is another common library module that contains kotlin extension helper class that provides common code to be used across application to navigate from one screen to another screen like Fragment to Fragment navigation , Activity to Activity navigation and so on. It is single point of contact for screen navigation and can be used as dependency across all required modules.
Please find attached screen shot for the same:
3. RoomRepository Module: This is common module that can be used across other modules as dependency to access local DB. All tables required for DB can be created in this module can directly access those info from any module as required without providing individual DB implementation in each module.
Please find below screen shot for the same:
Note: If any of the other module wants to use any of the above functionality provided by these common module , dependency can be created as below in build.gradle file:
For example , there is a login module with name “MVVMLogin” that needs common functionalities , the dependency can be created as below under “dependencies” section:
Similarly there is a module that needs room DB ,common RoomDB module dependency can be added as below:
Detail description of all other modules will be in next Part: