整理 Android 官方 Architecture: UI Layer
Compose Phases
@Composable Content changed Size/Position changed
State -------------> Composition -----------------> Layout -----------------------> Drawing ---> UI
v v
SlotTable Measure children
v v
RestartGroup Decide own size
MoveableGroup v
ReplaceableGroup Place children
ReusableGroup
UI
UI Layer==UI lifecycle independent+UI lifecycle dependentUI lifecycle independent==Bussiness logic+Screen UI StateUI lifecycle dependent==UI Logic+UI Elements State+UI Elements
UI State
Screen UI state (App state / shared state in Flutter)
It is what you need to display on the screen.
This state is usually connected with other layers of the hierarchy because it contains app data.
Example:
- A NewsUiState class can contain the news articles and other information needed to render the UI.
UI element state (Ephemeral state / UI state / local state in Flutter)
It refers to properties intrinsic to UI elements that influence how they are rendered.
A UI element may be shown or hidden and may have a certain font, font size, or font color.
In Android Views, the View manages this state itself as it is inherently stateful,
exposing methods to modify or query its state.
Logic
Business logic
It refers to what to do with state changes.
It is the implementation of product requirements for app data.
Business logic is usually placed in the domain or data layers,
but never in the UI layer.
Example:
- Bookmarking an article in the case study app.
- Making a payment or storing user preferences.
UI (behavior) logic
It is how to display state changes on the screen.
It refers to how to display state changes.
The UI handles this logic.
Example:
- Obtaining the right text to show on the screen using Android Resources.
- Navigating to a particular screen when the user clicks a button.
- Navigation logic or how to show messages to the user.
- Displaying a user message on the screen using a toast or a snackbar.
State Holder
State Holder == UI State + necessary logic
The ViewModel type is the recommended implementation for the management of screen-level UI state
with access to the data layer.
Furthermore, it survives configuration changes automatically.
ViewModel classes define the logic to be applied to events in the app and produce updated state as a
result.
Business logic state holder
Business logic state holders process user events and transform data from the data or domain layers
to screen UI state.
UI logic state holder
UI logic is logic that operates on data that the UI itself provides.
This may be on UI elements' state, or on UI data sources like the permissions API or Resources.
Events
UI events
Actions that should be handled in the UI layer.
User events
Events that the user produces when interacting with the app.
ViewModel events
UI actions that originate from the ViewModel.
Navigation events
If the event is triggered in the UI because the user tapped on a button,
the UI takes care of that by calling the navigation controller or exposing the event to the caller
composable as appropriate.