I have a tipping flow that consists from three screens "Select tip amount from…
- User
I have a tipping flow that consists from three screens "Select tip amount from predefined values", "Enter custom tip amount" and "Pay tip". I need to store and share the state between these three screens. For example, the user types custom tip amount, we store it somewhere, then the user comes back to the "Select tip amount from predefined values" we show this just typed custom tip, then the user goes to the "Pay tip" screen we show it again. We don't need to persist it in the database or shared prefs. It is a runtime information that it initialized when the user enters into the flow and removed after they leave it. When the user enters to the flow, we also need to go to the server to get some information that are used on these screens(e.g. the amount of min tip or predefined amounts of tip) which also should be stored only in the scope of the flow. In what layer in the scope of Clean Architecture in Android development, do you recommend to store this tip amount to share it between different screens?
- Assistant
In the scope of Clean Architecture in Android development, I recommend storing this shared state in a ViewModel layer. The ViewModel can have flow or feature-scoped lifecycles and serve as data storage during the lifetime of the flow without persisting in a database or shared prefs.
The ViewModel acts as a mediator between the View (your screens or fragments) and the underlying logic (Interactors or Use Cases). Using LiveData or another observable pattern in your ViewModel, you can easily communicate changes in state between the fragments and ensure data is retained during configuration changes (e.g., screen rotation).
For fetching initial data from the server, you can keep a separate repository within the Domain layer responsible for communicating with the data source, such as your server or local cache. The Use Case will interact with this repository and provide the result to your ViewModel.