Skip to content

Commit b21a963

Browse files
Danny1616jpelgromTimoPtr
authored
Add loading indicator to Android Auto favorites screen (#6335)
--------- Co-authored-by: Joris Pelgröm <jpelgrom@users.noreply.github.com> Co-authored-by: Timothy <6560631+TimoPtr@users.noreply.github.com>
1 parent 73a39a7 commit b21a963

File tree

2 files changed

+71
-53
lines changed

2 files changed

+71
-53
lines changed

app/src/main/kotlin/io/homeassistant/companion/android/settings/vehicle/ManageAndroidAutoViewModel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ class ManageAndroidAutoViewModel @Inject constructor(
4040

4141
var sortedEntities by mutableStateOf<List<Entity>>(emptyList())
4242
private set
43+
4344
var entityRegistry by mutableStateOf<List<EntityRegistryResponse>>(emptyList())
4445
private set
4546
var deviceRegistry by mutableStateOf<List<DeviceRegistryResponse>>(emptyList())
4647
private set
4748
var areaRegistry by mutableStateOf<List<AreaRegistryResponse>>(emptyList())
4849
private set
50+
4951
private val entities = mutableMapOf<Int, List<Entity>>()
5052
private val entityRegistries = mutableMapOf<Int, List<EntityRegistryResponse>>()
5153
private val deviceRegistries = mutableMapOf<Int, List<DeviceRegistryResponse>>()
@@ -56,6 +58,9 @@ class ManageAndroidAutoViewModel @Inject constructor(
5658

5759
var defaultServerId by mutableIntStateOf(0)
5860

61+
var isLoading by mutableStateOf(true)
62+
private set
63+
5964
init {
6065
viewModelScope.launch {
6166
servers = serverManager.servers()
@@ -71,6 +76,7 @@ class ManageAndroidAutoViewModel @Inject constructor(
7176
}
7277
}.awaitAll()
7378
loadEntities(serverManager.getServer()?.id ?: 0)
79+
isLoading = false
7480
}
7581
}
7682

app/src/main/kotlin/io/homeassistant/companion/android/settings/vehicle/views/AndroidAutoFavoritesView.kt

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package io.homeassistant.companion.android.settings.vehicle.views
22

33
import android.os.Build
44
import androidx.annotation.RequiresApi
5+
import androidx.compose.foundation.layout.Box
56
import androidx.compose.foundation.layout.PaddingValues
67
import androidx.compose.foundation.layout.fillMaxWidth
78
import androidx.compose.foundation.layout.padding
89
import androidx.compose.foundation.lazy.LazyColumn
910
import androidx.compose.foundation.lazy.rememberLazyListState
11+
import androidx.compose.material.CircularProgressIndicator
1012
import androidx.compose.material.Text
1113
import androidx.compose.runtime.Composable
1214
import androidx.compose.runtime.LaunchedEffect
@@ -15,6 +17,7 @@ import androidx.compose.runtime.mutableIntStateOf
1517
import androidx.compose.runtime.mutableStateOf
1618
import androidx.compose.runtime.remember
1719
import androidx.compose.runtime.setValue
20+
import androidx.compose.ui.Alignment
1821
import androidx.compose.ui.Modifier
1922
import androidx.compose.ui.res.stringResource
2023
import androidx.compose.ui.text.font.FontWeight
@@ -77,62 +80,71 @@ fun AndroidAutoFavoritesSettings(
7780
)
7881
}
7982

80-
if (serversList.size > 1) {
83+
if (androidAutoViewModel.isLoading) {
8184
item {
82-
ServerExposedDropdownMenu(
83-
servers = serversList,
84-
current = selectedServer,
85-
onSelected = {
86-
androidAutoViewModel.loadEntities(it)
87-
selectedServer = it
88-
},
89-
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(bottom = 16.dp),
90-
)
85+
Box(modifier = Modifier.fillMaxWidth().padding(top = 16.dp), contentAlignment = Alignment.Center) {
86+
CircularProgressIndicator()
87+
}
9188
}
92-
}
93-
item {
94-
// TODO use new theme for Material3 components https://github.com/home-assistant/android/issues/6302
95-
HATheme {
96-
EntityPicker(
97-
entities = validEntities,
98-
selectedEntityId = null,
99-
onEntityCleared = { /* Nothing */ },
100-
onEntitySelectedId = {
101-
androidAutoViewModel.onEntitySelected(true, it, selectedServer)
102-
},
103-
addButtonText = stringResource(commonR.string.add_favorite),
104-
entityRegistry = androidAutoViewModel.entityRegistry,
105-
deviceRegistry = androidAutoViewModel.deviceRegistry,
106-
areaRegistry = androidAutoViewModel.areaRegistry,
107-
modifier = Modifier.padding(horizontal = 16.dp).padding(bottom = 16.dp),
108-
)
89+
} else {
90+
if (serversList.size > 1) {
91+
item {
92+
ServerExposedDropdownMenu(
93+
servers = serversList,
94+
current = selectedServer,
95+
onSelected = {
96+
androidAutoViewModel.loadEntities(it)
97+
selectedServer = it
98+
},
99+
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(bottom = 16.dp),
100+
)
101+
}
109102
}
110-
}
111-
if (favoriteEntities.isNotEmpty() && androidAutoViewModel.sortedEntities.isNotEmpty()) {
112-
items(favoriteEntities.size, { favoriteEntities[it] }) { index ->
113-
val favoriteEntity = favoriteEntities[index]
114-
androidAutoViewModel.sortedEntities.firstOrNull {
115-
it.entityId == favoriteEntity.entityId &&
116-
favoriteEntity.serverId == selectedServer
117-
}?.let {
118-
ReorderableItem(
119-
state = reorderState,
120-
key = favoriteEntities[index],
121-
) { isDragging ->
122-
FavoriteEntityRow(
123-
entityName = it.friendlyName,
124-
entityId = it.entityId,
125-
onClick = {
126-
androidAutoViewModel.onEntitySelected(
127-
false,
128-
it.entityId,
129-
selectedServer,
130-
)
131-
},
132-
checked = true,
133-
draggable = true,
134-
isDragging = isDragging,
135-
)
103+
104+
item {
105+
// TODO use new theme for Material3 components https://github.com/home-assistant/android/issues/6302
106+
HATheme {
107+
EntityPicker(
108+
entities = validEntities,
109+
selectedEntityId = null,
110+
onEntityCleared = { /* Nothing */ },
111+
onEntitySelectedId = {
112+
androidAutoViewModel.onEntitySelected(true, it, selectedServer)
113+
},
114+
addButtonText = stringResource(commonR.string.add_favorite),
115+
entityRegistry = androidAutoViewModel.entityRegistry,
116+
deviceRegistry = androidAutoViewModel.deviceRegistry,
117+
areaRegistry = androidAutoViewModel.areaRegistry,
118+
modifier = Modifier.padding(horizontal = 16.dp).padding(bottom = 16.dp),
119+
)
120+
}
121+
}
122+
if (favoriteEntities.isNotEmpty() && androidAutoViewModel.sortedEntities.isNotEmpty()) {
123+
items(favoriteEntities.size, { favoriteEntities[it] }) { index ->
124+
val favoriteEntity = favoriteEntities[index]
125+
androidAutoViewModel.sortedEntities.firstOrNull {
126+
it.entityId == favoriteEntity.entityId &&
127+
favoriteEntity.serverId == selectedServer
128+
}?.let {
129+
ReorderableItem(
130+
state = reorderState,
131+
key = favoriteEntities[index],
132+
) { isDragging ->
133+
FavoriteEntityRow(
134+
entityName = it.friendlyName,
135+
entityId = it.entityId,
136+
onClick = {
137+
androidAutoViewModel.onEntitySelected(
138+
false,
139+
it.entityId,
140+
selectedServer,
141+
)
142+
},
143+
checked = true,
144+
draggable = true,
145+
isDragging = isDragging,
146+
)
147+
}
136148
}
137149
}
138150
}

0 commit comments

Comments
 (0)