diff --git a/app/shared/src/commonMain/kotlin/components/SoundList.kt b/app/shared/src/commonMain/kotlin/components/SoundList.kt index c48bdd7..0a63727 100644 --- a/app/shared/src/commonMain/kotlin/components/SoundList.kt +++ b/app/shared/src/commonMain/kotlin/components/SoundList.kt @@ -66,42 +66,56 @@ fun SoundList(errorReporter: ErrorReporter, voiceState: User.VoiceState?) { withContext(Dispatchers.IO) { api.events .onEach { event -> - when (event) { - is VoiceStateUpdateEvent -> reload(event.voiceState) - is InterfaceAvailabilityChangeEvent -> { - playingSound = event.playingSongId - available = event.available - } + fun handleEvent(event: Event) { + when (event) { + is VoiceStateUpdateEvent -> reload(event.voiceState) + is InterfaceAvailabilityChangeEvent -> { + playingSound = event.playingSongId + available = event.available + } - is SoundDeletedEvent -> { - sounds = sounds.map { - it.copy(sounds = it.sounds.filter { sound -> sound.id != event.id }) + is SoundDeletedEvent -> { + sounds = sounds.mapNotNull { + it.copy(sounds = it.sounds.filter { sound -> sound.id != event.id }) + // Remove group if it is empty + .takeIf { group -> group.sounds.isNotEmpty() } + } } - } - is SoundCreatedEvent -> { - val existingGroup = sounds.firstOrNull { it.tag == event.sound.tag } - if (existingGroup == null) { - sounds += SoundGroup(event.sound.tag, listOf(event.sound)) - } else { - val id = sounds.indexOf(existingGroup) - val copy = sounds.toMutableList() - copy[id] = existingGroup.copy(sounds = existingGroup.sounds + event.sound) - sounds = copy + is SoundCreatedEvent -> { + val existingGroup = sounds.firstOrNull { it.tag == event.sound.tag } + if (existingGroup == null) { + sounds += SoundGroup(event.sound.tag, listOf(event.sound)) + } else { + val id = sounds.indexOf(existingGroup) + val copy = sounds.toMutableList() + copy[id] = existingGroup.copy(sounds = existingGroup.sounds + event.sound) + sounds = copy + } } - } - is SoundUpdatedEvent -> { - val groupsCopy = sounds.toMutableList() - val group = groupsCopy.first { it.tag == event.sound.tag } - val copy = group.sounds.toMutableList() - copy[copy.indexOfFirst { it.id == event.sound.id }] = event.sound - groupsCopy[groupsCopy.indexOf(group)] = group.copy(sounds = copy) - sounds = groupsCopy - } + is SoundUpdatedEvent -> { + val currentSoundTag = sounds + .first { it.sounds.any { sound -> sound.id == event.sound.id } } + .tag + val groupsCopy = sounds.toMutableList() + val currentGroup = groupsCopy.first { it.tag == currentSoundTag } + if (currentSoundTag == event.sound.tag) { + val copy = currentGroup.sounds.toMutableList() + copy[copy.indexOfFirst { it.id == event.sound.id }] = event.sound + groupsCopy[groupsCopy.indexOf(currentGroup)] = currentGroup.copy(sounds = copy) + sounds = groupsCopy + } else { + // This is way easier than implementing this logic twice + handleEvent(SoundDeletedEvent(event.sound.id)) + handleEvent(SoundCreatedEvent(event.sound)) + } + } - else -> LOG.warn { "Unknown event type: $event" } + else -> LOG.warn { "Unknown event type: $event" } + } } + handleEvent(event) } .launchIn(this) } diff --git a/build.gradle.kts b/build.gradle.kts index fafc665..152606d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,12 +7,11 @@ plugins { allprojects { group = "dev.schlaubi.tonbrett" - version = "1.16.25" + version = "1.16.26" repositories { mavenCentral() google() - maven("https://jitpack.io") } } diff --git a/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Sound.kt b/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Sound.kt index 495af13..a5b8a81 100644 --- a/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Sound.kt +++ b/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Sound.kt @@ -122,8 +122,7 @@ public data class Sound( public data class SoundGroup( @SerialName("_id") val tag: String?, - val sounds: List, - val tagLowerCase: String? = null + val sounds: List ) internal class SequenceSerializer(childSerializer: KSerializer) : KSerializer> {