Handle a screen with 3 states

SimpleThreeStateView Composable

Purpose:

The SimpleThreeStateView composable is a reusable component that simplifies the management of three common states within your UI: loading, content, and exited. It provides a structured way to handle these states and present the appropriate content to the user.

Parameters:

  • modifier: A Modifier to apply to the view.

  • loadingView: An optional composable function that returns a loading view to be displayed while data is being fetched.

  • exitView: An optional composable function that returns an exit view to be displayed when the content is exited.

  • content: A composable function that returns a SimpleStates enum value to determine the current state.

Functionality:

  1. Initial State: The initial state is set to SimpleStates.CONTENT.

  2. State Updates: The content function is called to determine the current state.

  3. State Rendering: Based on the current state, the appropriate view is displayed:

    • SimpleStates.LOADING: Displays the loadingView if provided, or a default loading view.

    • SimpleStates.CONTENT: Calls the content function again to determine the content to be displayed.

    • SimpleStates.EXITED: Displays the exitView if provided, or a default loading view.

Scenario: Form Page, Loading, and Result

In the provided scenario, the SimpleThreeStateView can be used to manage the different states of a form submission process:

  1. Initial State (Form Page): The initial state is SimpleStates.CONTENT, displaying the form.

  2. Loading State (Posting to Server): When the user submits the form, the state is set to SimpleStates.LOADING, displaying a loading indicator.

  3. Final State (Result from Server): Once the server response is received, the state is set to either SimpleStates.CONTENT (if the submission was successful) or SimpleStates.EXITED (if an error occurred).

Example Usage:

Kotlin

SimpleThreeStateView(
    content = {
        if (isFormSubmitted) {
            SimpleStates.LOADING
        } else {
            FormView()
        }
    },
    loadingView = { CircularProgressIndicator() },
    exitView = { Text(text = "Submission failed") }
)

Use code with caution.

Key Benefits:

  • Simplified State Management: Handles loading, content, and exited states in a structured way.

  • Customizable Views: Allows you to provide custom loading, content, and exit views.

  • Reusable Component: Can be reused in multiple parts of your application.

  • Clear Structure: Encourages a consistent approach to managing view states.

By using SimpleThreeStateView, you can streamline your UI development and improve the user experience by providing clear feedback and handling different states effectively.

Last updated