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: AModifierto 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 aSimpleStatesenum value to determine the current state.
Functionality:
Initial State: The initial state is set to
SimpleStates.CONTENT.State Updates: The
contentfunction is called to determine the current state.State Rendering: Based on the current state, the appropriate view is displayed:
SimpleStates.LOADING: Displays theloadingViewif provided, or a default loading view.SimpleStates.CONTENT: Calls thecontentfunction again to determine the content to be displayed.SimpleStates.EXITED: Displays theexitViewif 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:
Initial State (Form Page): The initial state is
SimpleStates.CONTENT, displaying the form.Loading State (Posting to Server): When the user submits the form, the state is set to
SimpleStates.LOADING, displaying a loading indicator.Final State (Result from Server): Once the server response is received, the state is set to either
SimpleStates.CONTENT(if the submission was successful) orSimpleStates.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