refactor: ensure `Channel` and `ChannelSettings` indexes match

pull/684/head
andrekir 2023-08-12 07:43:38 -03:00
rodzic 76d01af995
commit 67fedb9ff8
1 zmienionych plików z 7 dodań i 4 usunięć

Wyświetl plik

@ -51,12 +51,15 @@ class ChannelSetRepository @Inject constructor(
* Updates the [ChannelSettings] list with the provided channel. * Updates the [ChannelSettings] list with the provided channel.
*/ */
suspend fun updateChannelSettings(channel: Channel) { suspend fun updateChannelSettings(channel: Channel) {
if (channel.role == Channel.Role.DISABLED) return
channelSetStore.updateData { preference -> channelSetStore.updateData { preference ->
if (preference.settingsCount > channel.index) { val builder = preference.toBuilder()
preference.toBuilder().setSettings(channel.index, channel.settings).build() // Resize to fit channel
} else { while (builder.settingsCount <= channel.index) {
preference.toBuilder().addSettings(channel.settings).build() builder.addSettings(ChannelSettings.getDefaultInstance())
} }
// use setSettings() to ensure settingsList and channel indexes match
builder.setSettings(channel.index, channel.settings).build()
} }
} }