Creating Custom Use Cases
To create custom use cases in our DHIS2 Android Capture App, follow the steps below:
Step 1: Create a New Module
The best way to implement a new use case is by creating a new Multiplatform library module. This module will encapsulate the functionality and can be easily maintained and tested.
- Open your project in Android Studio.
- Go to
File->New->New Module. - Select
Kotlin Multiplatform shared moduleand clickNext. - Name your module according to the pattern
customcase-usecase(e.g.,stock-usecase). - Click
Finish.
Step 2: Add Dependencies
In your newly created module, add the commonskmm module as a dependency to access the design
system, and commons for the DHIS2 SDK, and other common tools.
-
Open
build.gradle.ktsfile of your new module. -
Add the following line to the dependencies sections:
commonMain {
dependencies {
implementation(project(":commonskmm"))
}
}
androidMain.dependencies {
implementation(project(":commons"))
}
Configuration Adjustments
Depending on your Gradle version, you might need to make some configuration adjustments in the
build.gradle file. Additionally, if you are using the version catalog, you will need to adjust the
configuration accordingly.
This allows you to use resources and tools provided in the commons modules.
Step 3: Implement the Use Case
Now, you can start implementing your use case within the new module. Structure your code following
the best practices and utilize the resources from the commons module.
Recommended Architecture
We recommend using the MVVM (Model-View-ViewModel) architecture with Kotlin language and Jetpack Compose. This approach ensures a clean separation of concerns and enhances code maintainability. For more details, refer to the official Android Architecture Guidelines.
Example Module Structure
Here is an example of the module structure:
src/androidMain: Contains Java/Kotlin source files that needs the android framework as well as the AndroidManifest and android resourcessrc/commonMain: Contains Kotlin source files that can be shared between platforms.src/desktopMain: Contains source files that needs the desktop framework
It is recommended to include the kotlin multiplatform resource library to share resources.
Step 4: Using the Custom Use Case
To use your custom use case, add your new module as a dependency in the app module and call the
entry point (which can be an Activity or a Composable) from where you would like to start using it.
-
Open
build.gradle.ktsfile of theappmodule. -
Add the following line to the dependencies section:
dependencies {
implementation(project(":usecase-module"))
} -
Call the entry point in your code:
// Example: Calling an Activity
startActivity(Intent(this, YourUseCaseActivity::class.java))
// Example: Calling a Composable
setContent {
YourUseCaseComposable()
}
Benefits
This modular approach helps to avoid conflicts when updating your project with our new developments. By isolating custom use cases in separate modules, you can independently maintain and update your custom functionalities without interfering with the main project components.