Share
Embed
Transcript
129 slides
-
DNA.inc Android Makers Paris 2026 Compose Without Flow Unidirectional
State with Snapshots and Courtines Remco Mokveld Staff Engineer - Android -
DNA.inc Compose Without Flow Unidirectional Data Flow 0. Current app
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) -
DNA.inc Compose Without Flow Two paradigms of observability.
What to use where? -
DNA.inc Two Observability Paradigms One-shot data source with State Flow
1 class NewsViewModel(...) : ViewModel() { 2 private val _state = MutableStateFlow<List<Article>?>(null) 3 val state = _state.asStateFlow() 4 5 fun load() { 6 viewModelScope.launch { 7 _state.value = repository.getLatestArticles() 8 } 9 } 10 } -
DNA.inc Two Observability Paradigms One-shot data source with State Flow
1 class NewsViewModel(...) : ViewModel() { 2 private val _state = MutableStateFlow<List<Article>?>(null) 3 val state = _state.asStateFlow() 4 5 fun load() { 6 viewModelScope.launch { 7 _state.value = repository.getLatestArticles() 8 } 9 } 10 } -
DNA.inc Two Observability Paradigms One-shot data source with State Flow
1 class NewsViewModel(...) : ViewModel() { 2 private val _state = MutableStateFlow<List<Article>?>(null) 3 val state = _state.asStateFlow() 4 5 fun load() { 6 viewModelScope.launch { 7 _state.value = repository.getLatestArticles() 8 } 9 } 10 } -
DNA.inc Two Observability Paradigms One-shot data source with State Flow
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 val articles by viewModel.state.collectAsStateWithLifecycle() 5 LazyColumn { 6 items(articles) { article -> ArticleListItem(article) } 7 } 8 } -
DNA.inc Two Observability Paradigms One-shot data source with State Flow
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 val articles by viewModel.state.collectAsStateWithLifecycle() 5 LazyColumn { 6 items(articles) { article -> ArticleListItem(article) } 7 } 8 } -
DNA.inc Two Observability Paradigms One-shot data source with State Flow
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 val articles by viewModel.state.collectAsStateWithLifecycle() 5 LazyColumn { 6 items(articles) { article -> ArticleListItem(article) } 7 } 8 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
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 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
1 class NewsViewModel(...): ViewModel() { 2 var state by mutableStateOf<List<Article>?>(null) 3 private set 4 5 fun load() { 6 viewModelScope.launch { 7 withMutableSnapshot { 8 state = repository.getLatestArticles() 9 } 10 } 11 } 12 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
1 class NewsViewModel(...): ViewModel() { 2 var state by mutableStateOf<List<Article>?>(null) 3 private set 4 5 fun load() { 6 viewModelScope.launch { 7 withMutableSnapshot { 8 state = repository.getLatestArticles() 9 } 10 } 11 } 12 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 LazyColumn { 5 items(viewModel.state.orEmpty()) { article -> ..} 6 } 7 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 LazyColumn { 5 items(viewModel.state.orEmpty()) { article -> ..} 6 } 7 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 LazyColumn { 5 items(viewModel.state.orEmpty()) { article -> ..} 6 } 7 } -
DNA.inc Two Observability Paradigms One-shot data source with Compose State
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 LaunchedEffect(viewModel) { viewModel.load() } 4 LazyColumn { 5 items(viewModel.state.orEmpty()) { article -> ..} 6 } 7 } -
DNA.inc Two Observability Paradigms Streaming data source with State Flow
1 class NewsViewModel(...): ViewModel() { 2 val state = repository.getLatestArticles() 3 .stateIn(viewModelScope, WhileSubscribed(5.seconds), null) 4 } -
DNA.inc Two Observability Paradigms Streaming data source with State Flow
1 class NewsViewModel(...): ViewModel() { 2 val state = repository.getLatestArticles() 3 .stateIn(viewModelScope, WhileSubscribed(5.seconds), null) 4 } -
DNA.inc Two Observability Paradigms Streaming data source with State Flow
1 class NewsViewModel(...): ViewModel() { 2 val state = repository.getLatestArticles() 3 .stateIn(viewModelScope, WhileSubscribed(5.seconds), null) 4 } -
DNA.inc Two Observability Paradigms Streaming data source with State Flow
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 val articles by viewModel.state.collectAsStateWithLifecycle() 4 LazyColumn { 5 items(articles.orEmpty()) { article -> } 6 } 7 } -
DNA.inc Two Observability Paradigms Streaming data source with State Flow
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 3 val articles by viewModel.state.collectAsStateWithLifecycle() 4 LazyColumn { 5 items(articles.orEmpty()) { article -> } 6 } 7 } -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
It’s complicated... -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> ... } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> ... } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> ... } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> ... } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> ... } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> ... } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 @Composable 2 fun NewsList(viewModel: NewsViewModel) { 4 val state by viewModel.state.collectAsState() 5 LazyColumn { 6 items(state.articles) { article -> ArticleListItem(article) } 7 } 8 } -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task = tasksRepository.getTaskStream(taskId) 3 4 val uiState: StateFlow<TaskDetailUiState> = combine( 5 snapshotFlow { _isTaskDeleted }, 6 _task 7 ) { isTaskDeleted, task -> TaskDetailUiState(task, isTaskDeleted) } 8 .stateIn(...) -
DNA.inc Two Observability Paradigms Streaming data source with Compose State
1 private var _isTaskDeleted by mutableStateOf(false) 2 private val _task get() = tasksRepository.getTask(taskId) 3 4 val uiState: TaskDetailUiState get() = TaskDetailUiState(_task, _isTaskDeleted) -
DNA.inc Flow’s Responsibilities Flow models a stream of values
over time Compose State models a current value -
DNA.inc Flow’s Responsibilities Flow vs Compose State Flow State +
Execution Collection drives work Compose State State only No execution model -
DNA.inc Flow’s Responsibilities Who drives the work? Flow Start when
collected Stop when cancelled Compose State Read current value Re-read when invalidated -
DNA.inc Flow’s Responsibilities What did Flow actually give us? State
propagation Lifecycle-aware activation -
DNA.inc Flow’s Responsibilities Removing Flow splits responsibilities propagate state changes
→ ? lifecycle-aware activation → ? -
DNA.inc Flow’s Responsibilities Removing Flow splits responsibilities propagate state changes
→ Snapshot System lifecycle-aware activation → ? -
DNA.inc Compose State Propagation How does compose propagate change? No
streams No collection No operators -
DNA.inc Compose State Propagation How does compose propagate change? No
streams No collection No operators Compose uses the snapshot system -
DNA.inc Compose State Propagation The Snapshot System (high-level) Foundation
of Compose’s state model State is versioned Reads happen within a snapshot Writes are isolated Changes are applied atomically Observers react after apply -
DNA.inc Compose State Propagation What is a Snapshot? A consistent
view of state at a point in time Reads always return values from the same version Later changes are not visible -
DNA.inc Compose State Propagation What is a Snapshot? 1 var
state by mutableStateOf(1) 2 3 val snapshot = Snapshot.takeSnapshot() 4 5 state = 2 6 7 snapshot.enter { 8 println(state) 9 } -
DNA.inc Compose State Propagation What is a Snapshot? 1 var
state by mutableStateOf(1) 2 3 val snapshot = Snapshot.takeSnapshot() 4 5 state = 2 6 7 snapshot.enter { 8 println(state) 9 } -
DNA.inc Compose State Propagation What is a Snapshot? 1 var
state by mutableStateOf(1) 2 3 val snapshot = Snapshot.takeSnapshot() 4 5 state = 2 6 7 snapshot.enter { 8 println(state) 9 } -
DNA.inc Compose State Propagation What is a Snapshot? 1 var
state by mutableStateOf(1) 2 3 val snapshot = Snapshot.takeSnapshot() 4 5 state = 2 6 7 snapshot.enter { 8 println(state) 9 } -
DNA.inc Compose State Propagation What is a Snapshot? 1 var
state by mutableStateOf(1) 2 3 val snapshot = Snapshot.takeSnapshot() 4 5 state = 2 6 7 snapshot.enter { 8 println(state) 9 } -
DNA.inc Compose State Propagation What is a Snapshot? 1 var
state by mutableStateOf(1) 2 3 val snapshot = Snapshot.takeSnapshot() 4 5 state = 2 6 7 snapshot.enter { 8 println(state) // still 1 9 } -
DNA.inc Compose State Propagation A snapshot sees the world as
it was when the snapshot was created. -
DNA.inc Compose State Propagation Do writes change global state? 1
val snapshot = Snapshot.takeMutableSnapshot() 2 3 snapshot.enter { 4 state = 1 5 } 6 7 println(state) // still old value -
DNA.inc Compose State Propagation Do writes change global state? 1
val snapshot = Snapshot.takeMutableSnapshot() 2 3 snapshot.enter { 4 state = 1 5 } 6 7 println(state) // still old value -
DNA.inc Compose State Propagation Do writes change global state? 1
val snapshot = Snapshot.takeMutableSnapshot() 2 3 snapshot.enter { 4 state = 1 5 } 6 7 println(state) // still old value -
DNA.inc Compose State Propagation When does state change? 1 val
snapshot = Snapshot.takeMutableSnapshot() 2 3 snapshot.enter { 4 state = 1 5 } 6 7 snapshot.apply() 8 9 println(state) // now updated -
DNA.inc Compose State Propagation Changes staged in MutableSnapshots and are
applied atomically to global state. -
DNA.inc Compose State Propagation Entering a snapshot makes it the
current snapshot val snapshot = Snapshot.takeSnapshot() check(snapshot != Snapshot.current) snapshot.enter { check(snapshot == Snapshot,current) } -
DNA.inc Compose State Propagation Snapshot tracks state reads/writes 1 class
MutableStateImpl<T> { 2 override val value: T 3 get() { 4 Snapshot.current.onStateRead(this) 5 .. 6 } 3 set(value: T) { 4 Snapshot.current.onStateWritten(this) 5 .. 6 } 7 } -
DNA.inc Compose State Propagation Snapshot tracks state reads/writes 1 class
MutableStateImpl<T> { 2 override val value: T 3 get() { 4 Snapshot.current.onStateRead(this) 5 .. 6 } 3 set(value: T) { 4 Snapshot.current.onStateWritten(this) 5 .. 6 } 7 } -
DNA.inc Compose State Propagation Apply notifies observers 1 Snapshot.registerApplyObserver {
changedStates, _ -> 2 changedStates.forEach { println("Changed: $it") } 3 } 4 5 Snapshot.withMutableSnapshot { 6 state = 4 7 } -
DNA.inc Compose State Propagation Apply notifies observers 1 Snapshot.registerApplyObserver {
changedStates, _ -> 2 changedStates.forEach { println("Changed: $it") } 3 } 4 5 Snapshot.withMutableSnapshot { 6 state = 4 7 } -
DNA.inc Compose State Propagation Apply notifies observers 1 Snapshot.registerApplyObserver {
changedStates, _ -> 2 changedStates.forEach { println("Changed: $it") } 3 } 4 5 Snapshot.withMutableSnapshot { 6 state = 4 7 } -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation So how does Compose know what
changed? State reads Composable reads State Snapshot tracks read states State writes State Changes Write Tracked Changed states reported apply() Compose recomposes -
DNA.inc Compose State Propagation What this means Reads are tracked
automatically Writes are tracked automatically Dependencies are implicit -
DNA.inc Compose State Propagation No streams needed Side-effect free state
production pipeline Mapping can be cached with derivedStateOf Backpressure is no longer a problem -
DNA.inc Compose State Propagation What does this enable? State can
live anywhere Reads are tracked automatically Updates propagate without streams -
DNA.inc Compose State Propagation What does this enable? State can
live anywhere Reads are tracked automatically Updates propagate without streams What if our repositories just owned state? -
DNA.inc Repositories Owning State Repository owns State 1 @Stable 2
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 } -
DNA.inc Repositories Owning State Repository owns State 1 @Stable 2
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 } -
DNA.inc Repositories Owning State Repository owns State 1 @Stable 2
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 } -
DNA.inc Repositories Owning State Repository owns State 1 @Stable 2
class UserRepository { 3 var favorites = mutableStateMapOf<String, Boolean>() 4 5 fun setFavorite(id: String, favorite: Boolean) { 6 Snapshot.withMutableSnapshot { 7 favorites[id] = favorite 8 } 9 } 10 } -
DNA.inc Repositories Owning State NewsScreen with local filter 1 @Composable
2 fun NewsScreen(viewModel: NewsViewModel) { 3 LazyColumn { 4 item { 5 TextField(viewModel.searchQuery, { viewModel.searchQuery = it }) 6 } 7 items(viewModel.articles) { article -> ArticleListItem(article) } 8 } 9 } -
DNA.inc Repositories Owning State NewsScreen with local filter 1 @Composable
2 fun NewsScreen(viewModel: NewsViewModel) { 3 LazyColumn { 4 item { 5 TextField(viewModel.searchQuery, { viewModel.searchQuery = it }) 6 } 7 items(viewModel.articles) { article -> ArticleListItem(article) } 8 } 9 } -
DNA.inc Repositories Owning State ViewModel maps data to UI state
1 @Stable 2 class NewsViewModel( 3 private val articleRepository: ArticleRepository, 4 private val userRepository: UserRepository 5 ) { 6 var searchQuery by mutableStateOf("") 7 8 val articles: List<ArticleUiState> 9 get() = articleRepository.articles 10 .filter { it.title.contains(searchQuery) } 11 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 12 } -
DNA.inc Repositories Owning State ViewModel maps data to UI state
1 @Stable 2 class NewsViewModel( 3 private val articleRepository: ArticleRepository, 4 private val userRepository: UserRepository 5 ) { 6 var searchQuery by mutableStateOf("") 7 8 val articles: List<ArticleUiState> 9 get() = articleRepository.articles 10 .filter { it.title.contains(searchQuery) } 11 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 12 } -
DNA.inc Repositories Owning State ViewModel maps data to UI state
1 @Stable 2 class NewsViewModel( 3 private val articleRepository: ArticleRepository, 4 private val userRepository: UserRepository 5 ) { 6 var searchQuery by mutableStateOf("") 7 8 val articles: List<ArticleUiState> 9 get() = articleRepository.articles 10 .filter { it.title.contains(searchQuery) } 11 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 12 } -
DNA.inc Repositories Owning State ViewModel maps data to UI state
1 @Stable 2 class NewsViewModel( 3 private val articleRepository: ArticleRepository, 4 private val userRepository: UserRepository 5 ) { 6 var searchQuery by mutableStateOf("") 7 8 val articles: List<ArticleUiState> 9 get() = articleRepository.articles 10 .filter { it.title.contains(searchQuery) } 11 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 12 } -
DNA.inc Repositories Owning State ViewModel maps data to UI state
1 @Stable 2 class NewsViewModel( 3 private val articleRepository: ArticleRepository, 4 private val userRepository: UserRepository 5 ) { 6 var searchQuery by mutableStateOf("") 7 8 val articles: List<ArticleUiState> 9 get() = articleRepository.articles 10 .filter { it.title.contains(searchQuery) } 11 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 12 } -
DNA.inc Repositories Owning State Combining multiple sources 1 @Composable 2
fun NewsScreen(viewModel: NewsViewModel) { 3 LazyColumn { 4 item { 5 TextField(viewModel.searchQuery, { viewModel.searchQuery = it }) 6 } 7 items(viewModel.articles) { article -> ArticleListItem(article) } 8 } 9 } -
DNA.inc Repositories Owning State Combining multiple sources 1 @Composable 2
fun NewsScreen(viewModel: NewsViewModel) { 3 LazyColumn { 4 item { 5 TextField(viewModel.searchQuery, { viewModel.searchQuery = it }) 6 } 7 items(viewModel.articles) { article -> ArticleListItem(article) } 8 } 9 } -
DNA.inc Repositories Owning State Flow vs this approach articlesFlow favoritesFlow
searchQueryFlow combine + map articles favorites searchQuery normal Kotlin logic Same inputs. Same output. Different mechanism. stateIn visibleArticles visibleArticles -
DNA.inc Repositories Owning State Reactive without streams Multiple changing inputs
Automatic dependency tracking Re-evaluation on change -
DNA.inc Flow’s Responsibilities Removing Flow splits responsibilities propagate state changes
→ Snapshot System lifecycle-aware activation → ? -
DNA.inc Flow’s Responsibilities Removing Flow splits responsibilities propagate state changes
→ Snapshot System lifecycle-aware activation → Remembers/Retained StateOwners -
DNA.inc State Activation What about activation? StateFlow → implicit activation
Compose State → explicit activation -
DNA.inc State Activation So How Do We Handle Activation? Work
must be started explicitly Work must be stopped explicitly -
DNA.inc State Activation RememberObserver 1 class MyStateOwner { 2 fun
activate() = Unit 3 fun deactivate() = Unit 4 } 5 6 @Composable 7 fun rememberMyStateOwner(): MyStateOwner = 8 remember { 9 object : RememberObserver { 10 val stateOwner = MyStateOwner() 11 override fun onRemembered() = stateProducer.activate() 12 override fun onForgotten() = stateProducer.deactivate() 13 } 14 }.stateOwner -
DNA.inc State Activation RememberObserver 1 class MyStateOwner { 2 fun
activate() = Unit 3 fun deactivate() = Unit 4 } 5 6 @Composable 7 fun rememberMyStateOwner(): MyStateOwner = 8 remember { 9 object : RememberObserver { 10 val stateOwner = MyStateOwner() 11 override fun onRemembered() = stateProducer.activate() 12 override fun onForgotten() = stateProducer.deactivate() 13 } 14 }.stateOwner -
DNA.inc State Activation RememberObserver 1 class MyStateOwner { 2 fun
activate() = Unit 3 fun deactivate() = Unit 4 } 5 6 @Composable 7 fun rememberMyStateOwner(): MyStateOwner = 8 remember { 9 object : RememberObserver { 10 val stateOwner = MyStateOwner() 11 override fun onRemembered() = stateProducer.activate() 12 override fun onForgotten() = stateProducer.deactivate() 13 } 14 }.stateOwner -
DNA.inc State Activation RememberObserver 1 class MyStateOwner { 2 fun
activate() = Unit 3 fun deactivate() = Unit 4 } 5 6 @Composable 7 fun rememberMyStateOwner(): MyStateOwner = 8 remember { 9 object : RememberObserver { 10 val stateOwner = MyStateOwner() 11 override fun onRemembered() = stateProducer.activate() 12 override fun onForgotten() = stateProducer.deactivate() 13 } 14 }.stateOwner -
DNA.inc State Activation RememberObserver 1 class MyStateOwner { 2 fun
activate() = Unit 3 fun deactivate() = Unit 4 } 5 6 @Composable 7 fun rememberMyStateOwner(): MyStateOwner = 8 remember { 9 object : RememberObserver { 10 val stateOwner = MyStateOwner() 11 override fun onRemembered() = stateProducer.activate() 12 override fun onForgotten() = stateProducer.deactivate() 13 } 14 }.stateOwner -
DNA.inc State Activation RememberObserver 1 class MyStateOwner { 2 fun
activate() = Unit 3 fun deactivate() = Unit 4 } 5 6 @Composable 7 fun rememberMyStateOwner(): MyStateOwner = 8 remember { 9 object : RememberObserver { 10 val stateOwner = MyStateOwner() 11 override fun onRemembered() = stateProducer.activate() 12 override fun onForgotten() = stateProducer.deactivate() 13 } 14 }.stateOwner -
DNA.inc State Activation StateProducer 1 interface StateProducer { 2 suspend
fun activate() 3 } -
DNA.inc State Activation Lifecycle-aware activation 1 @Composable 2 fun NewsScreen()
{ 3 val repository = retainStateProducerWithLifecycle { 4 NewsRepository(dataSource) 5 } 6 7 val articles = repository.articles 8 9 LazyColumn { 10 items(articles) { article -> 11 ArticleListItem(article) 12 } 13 } 14 } -
DNA.inc State Activation Explicit activation 1 override suspend fun activate()
{ 2 while (currentCoroutineContext().isActive) { 3 val result = dataSource.getLatestArticles() 4 5 Snapshot.withMutableSnapshot { 6 articles = result 7 } 8 9 delay(1.minutes) 10 } 11 } -
DNA.inc Compose Without Flow Why? One observability framework instead of
two Simplified state modelling Side-effect free state State is, or is not -
DNA.inc Why? Two observability systems Snapshot-based observability is required Flow-based
observability is optional -
DNA.inc Why? Simpler Modelling Flow Repository → Flow<List<Article>> ViewModel →
StateFlow UI → collectAsState() Derived values → Flow operators Compose State Repository → List<Article> ViewModel → List<ArticleUiState> UI → Reads State Derived Values → Normal Kotlin -
DNA.inc Why? Granularity of Updates Flow Emits one big UI
state Change = new object Recomposition at emission boundary Compose State Tracks individual reads Change = specific field Recomposition at read site -
DNA.inc Why? Granularity of updates 1 class NewsViewModel( 2 private
val articleRepository: ArticleRepository, 3 private val userRepository: UserRepository 4 ) { 5 var searchQuery by mutableStateOf("") 6 7 val articles: List<ArticleUiState> 8 get() = articleRepository.articles 9 .filter { it.title.contains(searchQuery) } 10 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 11 } -
DNA.inc Why? Granularity of updates 1 class NewsViewModel( 2 private
val articleRepository: ArticleRepository, 3 private val userRepository: UserRepository 4 ) { 5 var searchQuery by mutableStateOf("") 6 7 val articles: List<ArticleUiState> 8 get() = articleRepository.articles 9 .filter { it.title.contains(searchQuery) } 10 .map { ArticleUiState(it, userRepository.isFavorite(it)) } 11 } -
DNA.inc Why? Granularity of updates 1 class NewsViewModel( 2 private
val articleRepository: ArticleRepository, 3 private val userRepository: UserRepository 4 ) { 5 var searchQuery by mutableStateOf("") 6 7 val articles: List<ArticleUiState> 8 get() = articleRepository.articles 9 .filter { it.title.contains(searchQuery) } 10 .map { ArticleUiState(it, userRepository::isFavorite) } 11 } -
DNA.inc Why? Granularity of updates 1 class NewsViewModel( 2 private
val articleRepository: ArticleRepository, 3 private val userRepository: UserRepository 4 ) { 5 var searchQuery by mutableStateOf("") 6 7 val articles: List<ArticleUiState> 8 get() = articleRepository.articles 9 .filter { it.title.contains(searchQuery) } 10 .map { ArticleUiState(it, userRepository::isFavorite) } 11 } -
DNA.inc State Activation State is or is not Combine with
4 flows as input will never emit if one of them hangs Map functions in flow can suspend -
DNA.inc State Activation Why not? State reads are hard to
validate at compile time Compose State belongs to the UI layer -
DNA.inc Compose Without Flow Thanks! Let’s talk about Compose Without
Flow [email protected] Bluesky @rmokveld.bsky.social X @remmies Linkedin linkedin.com/remco-mokveld