1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.lifecycle.compose.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.material.Text
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.remember
24 import androidx.lifecycle.Lifecycle
25 import androidx.lifecycle.compose.LifecycleEventEffect
26 import androidx.lifecycle.compose.LifecycleResumeEffect
27 import androidx.lifecycle.compose.LifecycleStartEffect
28 import androidx.lifecycle.compose.collectAsStateWithLifecycle
29 import kotlinx.coroutines.delay
30 import kotlinx.coroutines.flow.MutableStateFlow
31 import kotlinx.coroutines.flow.StateFlow
32 import kotlinx.coroutines.flow.asStateFlow
33 import kotlinx.coroutines.flow.flow
34 
35 @Sampled
36 @Composable
StateFlowCollectAsStateWithLifecyclenull37 fun StateFlowCollectAsStateWithLifecycle() {
38     class ExampleState {
39         private val _uiState = MutableStateFlow("")
40         val uiState: StateFlow<String> = _uiState.asStateFlow()
41     }
42 
43     val state = remember { ExampleState() }
44 
45     val uiState by state.uiState.collectAsStateWithLifecycle()
46     Text(text = uiState)
47 }
48 
49 @Sampled
50 @Composable
FlowCollectAsStateWithLifecyclenull51 fun FlowCollectAsStateWithLifecycle() {
52     class ExampleState {
53         val counter = flow {
54             var count = 0
55             while (true) {
56                 emit(count++)
57                 delay(1000)
58             }
59         }
60     }
61 
62     val state = remember { ExampleState() }
63     val count by state.counter.collectAsStateWithLifecycle(initialValue = 0)
64     Text(text = "$count")
65 }
66 
67 private interface TimeAnalytics {
stopTimeTrackingnull68     fun stopTimeTracking(): TimeAnalytics
69 }
70 
71 private interface DataAnalytics {
72     fun trackScreenView(screenName: String)
73 
74     fun startTimeTracking(): TimeAnalytics
75 
76     fun sendDisposalAnalytics(timeAnalytics: TimeAnalytics)
77 }
78 
79 @Sampled
80 @Composable
lifecycleEventEffectSamplenull81 fun lifecycleEventEffectSample() {
82     @Composable
83     fun Analytics(dataAnalytics: DataAnalytics) {
84         LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { dataAnalytics.trackScreenView("screen1") }
85 
86         // ...
87     }
88 }
89 
90 @Sampled
91 @Composable
lifecycleStartEffectSamplenull92 fun lifecycleStartEffectSample() {
93     @Composable
94     fun Analytics(dataAnalytics: DataAnalytics) {
95         LifecycleStartEffect(dataAnalytics) {
96             val timeTracker = dataAnalytics.startTimeTracking()
97 
98             onStopOrDispose { timeTracker.stopTimeTracking() }
99         }
100 
101         // ...
102     }
103 }
104 
105 @Sampled
106 @Composable
lifecycleResumeEffectSamplenull107 fun lifecycleResumeEffectSample() {
108     @Composable
109     fun Analytics(dataAnalytics: DataAnalytics) {
110         LifecycleResumeEffect(dataAnalytics) {
111             val timeTracker = dataAnalytics.startTimeTracking()
112 
113             onPauseOrDispose { timeTracker.stopTimeTracking() }
114         }
115 
116         // ...
117     }
118 }
119