Skip to content

Commit

Permalink
Fix exception in client when updating sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 21, 2023
1 parent 89369b9 commit aba4558
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
72 changes: 43 additions & 29 deletions app/shared/src/commonMain/kotlin/components/SoundList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public data class Sound(
public data class SoundGroup(
@SerialName("_id")
val tag: String?,
val sounds: List<Sound>,
val tagLowerCase: String? = null
val sounds: List<Sound>
)

internal class SequenceSerializer<T>(childSerializer: KSerializer<T>) : KSerializer<Sequence<T>> {
Expand Down

0 comments on commit aba4558

Please sign in to comment.