Skip to content

Creating Custom Use Cases

Andrés Miguel Rubio edited this page Jan 23, 2025 · 3 revisions

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 Android library module. This module will encapsulate the functionality and can be easily maintained and tested.

  1. Open your project in Android Studio.
  2. Go to File -> New -> New Module.
  3. Select Android Library and click Next.
  4. Name your module according to the pattern customcase-usecase (e.g., stock-usecase).
  5. Click Finish.

Step 2: Add Dependencies

In your newly created module, add the commons module as a dependency to access the design system, DHIS2 SDK, and other common tools.

  1. Open build.gradle.kts file of your new module.

  2. Add the following line to the dependencies section:

    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 module.

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:

Example Module Structure

  • src/main/java: Contains the Java/Kotlin source files.
  • src/main/res: Contains the resources such as layouts, strings, etc.
  • src/main/AndroidManifest.xml: The manifest file for the module.

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.

  1. Open build.gradle.kts file of the app module.

  2. Add the following line to the dependencies section:

    dependencies {
        implementation(project(":usecase-module"))
    }
  3. 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.

Clone this wiki locally