@@ -26,12 +26,14 @@ import android.util.Log
2626import androidx.activity.ComponentActivity
2727import androidx.activity.compose.setContent
2828import androidx.activity.result.contract.ActivityResultContracts
29- import androidx.compose.foundation.layout.Box
30- import androidx.compose.foundation.layout.Column
3129import androidx.compose.foundation.layout.PaddingValues
3230import androidx.compose.foundation.layout.Spacer
3331import androidx.compose.foundation.layout.fillMaxSize
3432import androidx.compose.foundation.layout.height
33+ import androidx.wear.compose.foundation.lazy.AutoCenteringParams
34+ import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
35+ import androidx.wear.compose.foundation.lazy.items
36+ import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
3537import androidx.compose.runtime.Composable
3638import androidx.compose.runtime.getValue
3739import androidx.compose.runtime.mutableStateOf
@@ -143,51 +145,64 @@ fun ElapsedTime(ambientState: AmbientState) {
143145fun WearApp () {
144146 val context = LocalContext .current
145147 var runningService by rememberSaveable { mutableStateOf<Class <* >? > (null ) }
148+ val listState = rememberScalingLazyListState()
146149
147150 MaterialTheme (
148151 colorScheme = dynamicColorScheme(LocalContext .current) ? : MaterialTheme .colorScheme
149152 ) {
150153 AmbientAware { ambientState ->
151- Box (modifier = Modifier .fillMaxSize(), contentAlignment = Alignment .Center ) {
152- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
154+ ScalingLazyColumn (
155+ modifier = Modifier .fillMaxSize(),
156+ state = listState,
157+ horizontalAlignment = Alignment .CenterHorizontally ,
158+ autoCentering = AutoCenteringParams (itemIndex = 0 )
159+ ) {
160+ item {
153161 Text (text = " Elapsed Time" , style = MaterialTheme .typography.titleLarge)
162+ }
163+ item {
154164 Spacer (modifier = Modifier .height(8 .dp))
165+ }
166+ item {
155167 ElapsedTime (ambientState = ambientState)
168+ }
169+ item {
156170 Spacer (modifier = Modifier .height(8 .dp))
171+ }
157172
158- val services = listOf (
159- AlwaysOnService1 ::class .java,
160- AlwaysOnService2 ::class .java,
161- AlwaysOnService3 ::class .java
162- )
163-
164- services.forEachIndexed { index, serviceClass ->
165- val isRunning = runningService == serviceClass
166- SwitchButton (
167- checked = isRunning,
168- onCheckedChange = { newState ->
169- if ( newState) {
170- if (runningService != null ) {
171- Log .d( TAG , " Stopping ${runningService?.simpleName} " )
172- context.stopService( Intent (context, runningService) )
173- }
174- Log .d( TAG , " Starting ${serviceClass.simpleName} " )
175- val intent = Intent (context, serviceClass)
176- context.startForegroundService(intent )
177- runningService = serviceClass
178- } else {
179- Log .d( TAG , " Stopping ${serviceClass.simpleName} " )
180- context.stopService( Intent (context, serviceClass) )
181- runningService = null
182- }
183- },
184- contentPadding = PaddingValues (horizontal = 8 .dp, vertical = 4 .dp) ,
185- ) {
186- Text (
187- text = " Ongoing Activity ${index + 1 } " ,
188- style = MaterialTheme .typography.bodyExtraSmall ,
189- )
190- }
173+ val services = listOf (
174+ AlwaysOnService1 ::class .java,
175+ AlwaysOnService2 ::class .java,
176+ AlwaysOnService3 ::class .java
177+ )
178+
179+ items( services.size) { index ->
180+ val serviceClass = services[index]
181+ val isRunning = runningService == serviceClass
182+ SwitchButton (
183+ checked = isRunning,
184+ onCheckedChange = { newState ->
185+ if (newState ) {
186+ if (runningService != null ) {
187+ Log .d( TAG , " Stopping ${ runningService?.simpleName} " )
188+ context.stopService( Intent (context, runningService))
189+ }
190+ Log .d( TAG , " Starting ${ serviceClass.simpleName} " )
191+ val intent = Intent (context, serviceClass )
192+ context.startForegroundService(intent)
193+ runningService = serviceClass
194+ } else {
195+ Log .d( TAG , " Stopping ${ serviceClass.simpleName} " )
196+ context.stopService( Intent (context, serviceClass))
197+ runningService = null
198+ }
199+ } ,
200+ contentPadding = PaddingValues (horizontal = 8 .dp, vertical = 4 .dp),
201+ ) {
202+ Text (
203+ text = " Ongoing Activity ${index + 1 } " ,
204+ style = MaterialTheme .typography.bodySmall,
205+ )
191206 }
192207 }
193208 }
0 commit comments