16
16
package com.android.ai.samples.geminilivetodo.ui
17
17
18
18
import android.Manifest
19
+ import android.app.Activity
20
+ import androidx.activity.compose.LocalActivity
19
21
import androidx.annotation.RequiresPermission
20
22
import androidx.compose.animation.Animatable
21
23
import androidx.compose.animation.animateColor
@@ -25,6 +27,7 @@ import androidx.compose.animation.core.infiniteRepeatable
25
27
import androidx.compose.animation.core.rememberInfiniteTransition
26
28
import androidx.compose.animation.core.tween
27
29
import androidx.compose.foundation.background
30
+ import androidx.compose.foundation.layout.Box
28
31
import androidx.compose.foundation.layout.Column
29
32
import androidx.compose.foundation.layout.Row
30
33
import androidx.compose.foundation.layout.Spacer
@@ -43,6 +46,7 @@ import androidx.compose.material.icons.filled.MicOff
43
46
import androidx.compose.material3.AlertDialog
44
47
import androidx.compose.material3.Button
45
48
import androidx.compose.material3.Checkbox
49
+ import androidx.compose.material3.CircularProgressIndicator
46
50
import androidx.compose.material3.ExperimentalMaterial3Api
47
51
import androidx.compose.material3.FabPosition
48
52
import androidx.compose.material3.FloatingActionButton
@@ -64,6 +68,7 @@ import androidx.compose.runtime.setValue
64
68
import androidx.compose.ui.Alignment
65
69
import androidx.compose.ui.Modifier
66
70
import androidx.compose.ui.graphics.Color
71
+ import androidx.compose.ui.platform.LocalContext
67
72
import androidx.compose.ui.res.stringResource
68
73
import androidx.compose.ui.text.TextStyle
69
74
import androidx.compose.ui.text.style.TextDecoration
@@ -86,16 +91,18 @@ fun TodoScreen(viewModel: TodoScreenViewModel = hiltViewModel()) {
86
91
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
87
92
var text by remember { mutableStateOf(" " ) }
88
93
94
+ val activity = LocalActivity .current as Activity
95
+
89
96
LaunchedEffect (Unit ) {
90
- viewModel.initializeGeminiLive()
97
+ viewModel.initializeGeminiLive(activity )
91
98
}
92
99
93
100
Scaffold (
94
101
topBar = {
95
102
TopAppBar (
96
103
colors = TopAppBarDefaults .topAppBarColors(
97
104
containerColor = MaterialTheme .colorScheme.primaryContainer,
98
- titleContentColor = MaterialTheme .colorScheme.onPrimary ,
105
+ titleContentColor = MaterialTheme .colorScheme.primary ,
99
106
),
100
107
title = { Text (stringResource(R .string.gemini_live_title)) },
101
108
)
@@ -127,7 +134,13 @@ fun TodoScreen(viewModel: TodoScreenViewModel = hiltViewModel()) {
127
134
128
135
when (uiState) {
129
136
is TodoScreenUiState .Initial -> {
130
- // Show a loading indicator or initial state
137
+ Box (
138
+ modifier = Modifier
139
+ .fillMaxSize(),
140
+ contentAlignment = Alignment .Center ,
141
+ ) {
142
+ CircularProgressIndicator ()
143
+ }
131
144
}
132
145
is TodoScreenUiState .Success -> {
133
146
val todos = (uiState as TodoScreenUiState .Success ).todos
@@ -163,7 +176,11 @@ fun TodoScreen(viewModel: TodoScreenViewModel = hiltViewModel()) {
163
176
}
164
177
165
178
@Composable
166
- fun TodoInput (text : String , onTextChange : (String ) -> Unit , onAddClick : () -> Unit ) {
179
+ fun TodoInput (
180
+ text : String ,
181
+ onTextChange : (String ) -> Unit ,
182
+ onAddClick : () -> Unit ,
183
+ ) {
167
184
Row (
168
185
modifier = Modifier
169
186
.fillMaxWidth()
@@ -188,7 +205,10 @@ fun TodoInput(text: String, onTextChange: (String) -> Unit, onAddClick: () -> Un
188
205
}
189
206
190
207
@Composable
191
- fun MicButton (uiState : TodoScreenUiState , onToggle : () -> Unit ) {
208
+ fun MicButton (
209
+ uiState : TodoScreenUiState ,
210
+ onToggle : () -> Unit ,
211
+ ) {
192
212
if (uiState is TodoScreenUiState .Success ) {
193
213
val successState = uiState as TodoScreenUiState .Success
194
214
val micIcon = when {
@@ -237,7 +257,12 @@ fun MicButton(uiState: TodoScreenUiState, onToggle: () -> Unit) {
237
257
}
238
258
239
259
@Composable
240
- fun TodoItem (modifier : Modifier , task : Todo , onToggle : () -> Unit , onDelete : () -> Unit ) {
260
+ fun TodoItem (
261
+ modifier : Modifier ,
262
+ task : Todo ,
263
+ onToggle : () -> Unit ,
264
+ onDelete : () -> Unit ,
265
+ ) {
241
266
val defaultBackgroundColor = Color .Transparent
242
267
val backgroundColor = remember { Animatable (defaultBackgroundColor) }
243
268
0 commit comments