data New app data with the bookmarked article UI state 1. Current UI state 6. New UI state The data layer persists the data change and updates the application data Data layer View model UI Layer UI elements The ViewModel notifies the state change to the data layer User bookmarks article (UI event)
1 class NewsViewModel(...): ViewModel() { 2 var state by mutableStateOf<List<Article>?>(null) 3 private set 4 5 fun load() { 6 viewModelScope.launch { 6 val articles = repository.getLatestArticles() 7 withMutableSnapshot { state = articles } 8 } 9 } 10 }
of Compose’s state model State is versioned Reads happen within a snapshot Writes are isolated Changes are applied atomically Observers react after apply
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes
class ArticlesRepository( 3 private val dataSource: ArticlesDataSource 4 ) { 5 var articles: List<Article>? by mutableStateOf(null) 6 private set 7 8 suspend fun load() { 9 val result = dataSource.getLatestArticles() 10 Snapshot.withMutableSnapshot { articles = result } 11 } 12 }
class ArticlesRepository( 3 private val dataSource: ArticlesDataSource 4 ) { 5 var articles: List<Article>? by mutableStateOf(null) 6 private set 7 8 suspend fun load() { 9 val result = dataSource.getLatestArticles() 10 Snapshot.withMutableSnapshot { articles = result } 11 } 12 }
class ArticlesRepository( 3 private val dataSource: ArticlesDataSource 4 ) { 5 var articles: List<Article>? by mutableStateOf(null) 6 private set 7 8 suspend fun load() { 9 val result = dataSource.getLatestArticles() 10 Snapshot.withMutableSnapshot { articles = result } 11 } 12 }
searchQueryFlow combine + map articles favorites searchQuery normal Kotlin logic Same inputs. Same output. Different mechanism. stateIn visibleArticles visibleArticles