Skip to content

Commit 95a6ddc

Browse files
Update UI
1 parent 7a39764 commit 95a6ddc

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnActivity.kt

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import android.util.Log
2626
import androidx.activity.ComponentActivity
2727
import androidx.activity.compose.setContent
2828
import androidx.activity.result.contract.ActivityResultContracts
29-
import androidx.compose.foundation.layout.Box
30-
import androidx.compose.foundation.layout.Column
3129
import androidx.compose.foundation.layout.PaddingValues
3230
import androidx.compose.foundation.layout.Spacer
3331
import androidx.compose.foundation.layout.fillMaxSize
3432
import 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
3537
import androidx.compose.runtime.Composable
3638
import androidx.compose.runtime.getValue
3739
import androidx.compose.runtime.mutableStateOf
@@ -143,51 +145,64 @@ fun ElapsedTime(ambientState: AmbientState) {
143145
fun 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
}

wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnService.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ abstract class AlwaysOnServiceBase : LifecycleService() {
6262
return START_STICKY
6363
}
6464

65-
66-
6765
override fun onDestroy() {
6866
Log.d(TAG, "onDestroy: Service destroyed")
6967
isRunning = false
@@ -248,4 +246,4 @@ class AlwaysOnService3 : AlwaysOnServiceBase() {
248246
ongoingActivity.apply(applicationContext)
249247
startForeground(NOTIFICATION_ID, notificationBuilder.build())
250248
}
251-
}
249+
}

0 commit comments

Comments
 (0)