@@ -243,24 +243,36 @@ The datasource exposes a `Flow` which is built with the `flow{}` flow builder. I
243
243
A ` LiveData ` property that exposes the ` UiState ` in the ` ViewModel ` is created by using the ` .asLiveData() ` terminal operator.
244
244
This use case also shows how to use the ` map ` intermediate operator and the ` onStart ` lifecycle operator.
245
245
246
+ [[ code viewmodel] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase1/FlowUseCase1ViewModel.kt )]
247
+ [[ code datasource] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase1/StockPriceDataSource.kt )]
248
+
246
249
### 2. Basic Flow Intermediate Operators
247
250
248
251
The second use case is an extension of the first one.
249
252
It uses some basic intermediate operators, like ` withIndex ` , ` map ` , ` take ` and ` filter ` .
250
253
254
+ [[ code viewmodel] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase2/FlowUseCase2ViewModel.kt )]
255
+ [[ code datasource] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase2/StockPriceDataSource.kt )]
256
+
251
257
### 3. Flow Exception Handling
252
258
253
259
The third use case shows how to properly implement exception handling with flows.
254
260
255
261
It uses the ` catch ` operator to handle exceptions of our flow in the ` ViewModel ` and uses the ` retry ` operator to retry failed network requests in the ` DataSource ` .
256
262
263
+ [[ code viewmodel] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase3/FlowUseCase3ViewModel.kt )]
264
+ [[ code datasource] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase3/StockPriceDataSource.kt )]
265
+
257
266
### 4. Exposing Flows in the ViewModel
258
267
259
268
This use case shows how to expose flows (a ` StateFlow ` to be precise) in the ` ViewModel ` instead of a ` LiveData ` property.
260
269
The ` statIn ` operator is used to convert the ordinary, cold ` Flow ` into a hot ` StateFlow ` .
261
270
262
271
In the ` Activity ` , the ` repeadOnLifecycle ` suspend function is used to collect emissions of the ` StateFlow ` in a lifecycle-aware manner.
263
272
273
+ [[ code viewmodel] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase4/FlowUseCase1ViewModel.kt )]
274
+ [[ code datasource] ( app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase4/StockPriceDataSource.kt )]
275
+
264
276
## Author
265
277
266
278
![ LukasLechner] ( documentation/images/lukle.png )
0 commit comments