Skip to content

Commit a6ac0fe

Browse files
authored
Fix shortcuts screen crash when entered (#5745)
- Fixes the shortcuts setting screen crashing on launch because it expects the list to have a certain number of shortcuts, which is no longer true - Fixes potential race condition for shortcuts
1 parent 94eb818 commit a6ac0fe

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

app/src/main/kotlin/io/homeassistant/companion/android/settings/shortcuts/ManageShortcutsViewModel.kt

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.widget.Toast
1313
import androidx.annotation.RequiresApi
1414
import androidx.compose.runtime.MutableState
1515
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableIntStateOf
1617
import androidx.compose.runtime.mutableStateListOf
1718
import androidx.compose.runtime.mutableStateMapOf
1819
import androidx.compose.runtime.mutableStateOf
@@ -85,11 +86,29 @@ class ManageShortcutsViewModel @Inject constructor(
8586
var delete: MutableState<Boolean>,
8687
)
8788

88-
var shortcuts = mutableStateListOf<Shortcut>()
89+
var shortcuts = mutableStateListOf<Shortcut>().apply {
90+
repeat(6) {
91+
add(
92+
Shortcut(
93+
id = mutableStateOf(""),
94+
serverId = mutableIntStateOf(0),
95+
selectedIcon = mutableStateOf(null),
96+
label = mutableStateOf(""),
97+
desc = mutableStateOf(""),
98+
path = mutableStateOf(""),
99+
type = mutableStateOf("lovelace"),
100+
delete = mutableStateOf(false),
101+
),
102+
)
103+
}
104+
}
89105
private set
90106

91107
init {
92108
viewModelScope.launch {
109+
val currentServerId = currentServerId()
110+
shortcuts.forEach { it.serverId.value = currentServerId }
111+
93112
serverManager.defaultServers.forEach { server ->
94113
launch {
95114
entities[server.id] = try {
@@ -101,32 +120,19 @@ class ManageShortcutsViewModel @Inject constructor(
101120
}
102121
}
103122
}
104-
for (i in 0..5) {
105-
shortcuts.add(
106-
Shortcut(
107-
mutableStateOf(""),
108-
mutableStateOf(currentServerId()),
109-
mutableStateOf(null),
110-
mutableStateOf(""),
111-
mutableStateOf(""),
112-
mutableStateOf(""),
113-
mutableStateOf("lovelace"),
114-
mutableStateOf(false),
115-
),
116-
)
117-
}
118-
}
119-
updateDynamicShortcuts()
120-
Timber.d("We have ${dynamicShortcuts.size} dynamic shortcuts")
121123

122-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
123-
Timber.d("Can we pin shortcuts: ${ShortcutManagerCompat.isRequestPinShortcutSupported(app)}")
124-
Timber.d("We have ${pinnedShortcuts.size} pinned shortcuts")
125-
}
124+
updateDynamicShortcuts()
125+
Timber.d("We have ${dynamicShortcuts.size} dynamic shortcuts")
126126

127-
if (dynamicShortcuts.size > 0) {
128-
for (i in 0 until dynamicShortcuts.size) {
129-
setDynamicShortcutData(dynamicShortcuts[i].id, i)
127+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
128+
Timber.d("Can we pin shortcuts: ${ShortcutManagerCompat.isRequestPinShortcutSupported(app)}")
129+
Timber.d("We have ${pinnedShortcuts.size} pinned shortcuts")
130+
}
131+
132+
if (dynamicShortcuts.size > 0) {
133+
for (i in 0 until dynamicShortcuts.size) {
134+
setDynamicShortcutData(dynamicShortcuts[i].id, i)
135+
}
130136
}
131137
}
132138
}

0 commit comments

Comments
 (0)