Lay out a clean MVVM architecture for an Android feature with clear layers, dependencies, and data flow.
## CONTEXT I want to structure an Android feature using MVVM so that responsibilities are clear and the code is testable. Right now logic is scattered between Activities, Fragments, and helper classes, and I cannot tell which layer should own what. I want a concrete blueprint for my feature. ## ROLE You are an Android architect who designs maintainable feature modules using MVVM and the recommended app architecture, balancing pragmatism with clean separation. ## RESPONSE GUIDELINES - Outline the layers involved and what each one owns. - Provide Kotlin skeletons for the ViewModel, repository, and UI state. - Show how data flows from data sources to the UI and how events flow back. - Explain where to place mapping, validation, and error handling. - Keep the design easy to extend and test. ## TASK CRITERIA ### Layer responsibilities - Keep UI components thin and free of business logic. - Place screen logic and state in the ViewModel. - Put data access behind a repository abstraction. - Separate domain models from data and UI models. - Define clear boundaries between layers. ### State exposure - Expose UI state as an observable stream from the ViewModel. - Use an immutable state class for the screen. - Represent loading and error states explicitly. - Avoid exposing mutable internals. - Emit one-time events through a dedicated channel. ### Dependency direction - Ensure UI depends on the ViewModel, not the reverse. - Keep the repository unaware of Android UI types. - Inject dependencies rather than constructing them inline. - Allow swapping data sources without UI changes. - Keep the domain layer framework-agnostic. ### Data flow - Describe how a user action results in a state update. - Show mapping from data models to UI models. - Handle combining multiple data sources cleanly. - Cache or refresh data where appropriate. - Keep flows lifecycle-aware in the UI. ### Testability and growth - Make the ViewModel testable without Android framework. - Provide fakes for repositories in tests. - Suggest a package structure for the feature. - Note where to add navigation and side effects. - Highlight trade-offs of the chosen structure. ## ASK THE USER FOR - The feature you are building and its main screens. - The data sources involved, such as network or local database. - Whether you use a dependency injection framework. - Your current UI toolkit, Compose or Views. - Any constraints from the existing codebase.
Or press ⌘C to copy
Copy and paste into your favorite AI tool
Explore more Coding prompts
Browse Coding