From 67fedb9ff850a15a30d23282d89b9f62c99c0ea0 Mon Sep 17 00:00:00 2001 From: andrekir Date: Sat, 12 Aug 2023 07:43:38 -0300 Subject: [PATCH] refactor: ensure `Channel` and `ChannelSettings` indexes match --- .../mesh/repository/datastore/ChannelSetRepository.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/repository/datastore/ChannelSetRepository.kt b/app/src/main/java/com/geeksville/mesh/repository/datastore/ChannelSetRepository.kt index 8ab147b7..40e7ce91 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/datastore/ChannelSetRepository.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/datastore/ChannelSetRepository.kt @@ -51,12 +51,15 @@ class ChannelSetRepository @Inject constructor( * Updates the [ChannelSettings] list with the provided channel. */ suspend fun updateChannelSettings(channel: Channel) { + if (channel.role == Channel.Role.DISABLED) return channelSetStore.updateData { preference -> - if (preference.settingsCount > channel.index) { - preference.toBuilder().setSettings(channel.index, channel.settings).build() - } else { - preference.toBuilder().addSettings(channel.settings).build() + val builder = preference.toBuilder() + // Resize to fit channel + while (builder.settingsCount <= channel.index) { + builder.addSettings(ChannelSettings.getDefaultInstance()) } + // use setSettings() to ensure settingsList and channel indexes match + builder.setSettings(channel.index, channel.settings).build() } }