Presenter
@Composable
fun Presenter(eventFlow: Flow): UiState {
val event by eventFlow.collectAsState(null)
return if (event
= =
null) {
UiState.Loading
} else {
UiState.Data(…)
}
}
Slide 44
Slide 44 text
Presenter
@Composable
fun Presenter(eventFlow: Flow): UiState {
val event by eventFlow.collectAsState(null)
return if (event
= =
null) {
UiState.Loading
} else {
UiState.Data(…)
}
}
Slide 45
Slide 45 text
Presenter
@Composable
fun Presenter(eventFlow: Flow): UiState {
val event by eventFlow.collectAsState(null)
return if (event
= =
null) {
UiState.Loading
} else {
UiState.Data(…)
}
}
Slide 46
Slide 46 text
Setup
Molecule
Presenter
(Composable)
Slide 47
Slide 47 text
Setup
Molecule
Presenter
(Composable)
StateFlow
Slide 48
Slide 48 text
Launching Molecule
val scope = CoroutineScope(Dispatchers.Main)
val models: StateFlow = scope.launchMolecule {
userPresenter(postsFlow, likesFlow)
}
Slide 49
Slide 49 text
Launching Molecule
val flow: StateFlow = scope.launchMolecule {
presenter(eventFlow)
}
Slide 50
Slide 50 text
Setup
View Molecule
Slide 51
Slide 51 text
Setup
View View Model
Slide 52
Slide 52 text
View Model
class MyViewModel: ViewModel() {
val stateFlow = moleculeScope.launchMolecule {
val event by eventFlow.collectAsState(null)
return if (event
==
null) {
UiState.Loading
} else {
UiState.Success(…)
}
}
Slide 53
Slide 53 text
Learn More
https:
/
/
youtu.be/rUpZSZedoHI
Slide 54
Slide 54 text
State Flow
● Different ways to create state flows
● Emit, collect
Sharing Policies
val sharedFlow = flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.WhileSubscribed()
)
Slide 103
Slide 103 text
Properties
• Active as long as external scope is alive
• Remains as long as there are collectors.
Slide 104
Slide 104 text
Properties
Active as long as external scope is alive
externalScope.launch {
sharedFlow.collect { }
}
Slide 105
Slide 105 text
flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.WhileSubscribed()
)
Properties
Active as long as external scope is alive
Slide 106
Slide 106 text
Properties
Active as long as external scope is alive
externalScope.cancel()
externalScope.launch {
sharedFlow.collect { }
}
Slide 107
Slide 107 text
Properties
Active as long as external scope is alive
externalScope.cancel()
externalScope.launch {
sharedFlow.collect { }
}
Complete Exceptionally
Slide 108
Slide 108 text
Properties
Active as long as there are collectors.
Slide 109
Slide 109 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
Slide 110
Slide 110 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
Slide 111
Slide 111 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
job.cancel()
Slide 112
Slide 112 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
job.cancel()
Slide 113
Slide 113 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
job.cancel()
Slide 114
Slide 114 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job1 = launch { sharedFlow.collect { } }
val job2 = launch { sharedFlow.collect { } }
Slide 115
Slide 115 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
job1.cancel()
val job2 = launch { sharedFlow.collect { } }
Slide 116
Slide 116 text
Properties
Active as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
job1.cancel()
val job2 = launch { sharedFlow.collect { } }
Remain Active
Slide 117
Slide 117 text
Properties
• Active as long as external scope is alive
• Active as long as there are collectors.
Slide 118
Slide 118 text
Sharing Policies
• While Subscribed
• Eagerly
• Lazily
Slide 119
Slide 119 text
Eagerly
flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.Eagerly()
)
Lazily
Start sharing after the first subscriber appears
Slide 131
Slide 131 text
Sharing Policies
• While Subscribed
• Active while there are active subscribers.
• Eagerly
• Start producer eagerly and never stop
• Lazily
• Start after the first subscriber appears and never stop
Slide 132
Slide 132 text
Manage Backpressure
Slide 133
Slide 133 text
Shared Flow
Buffer
Slide 134
Slide 134 text
Shared Flow
Producer Consumer
Slide 135
Slide 135 text
Shared Flow
Producer Consumer
Generating events fast
Slide 136
Slide 136 text
Shared Flow
Producer Consumer
Listening to events
with delay
Slide 137
Slide 137 text
Shared Flow
Producer Consumer
Slide 138
Slide 138 text
Shared Flow
Producer Consumer
Slide 139
Slide 139 text
Shared Flow
Producer Consumer
What happens when it is full?
Slide 140
Slide 140 text
Buffering Overflow Strategies
• Suspend
• Drop oldest
• Drop latest