From ac3019ef2598ac1ae1152dec607fab7e528bc481 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Wed, 2 Nov 2022 10:59:27 +0100 Subject: [PATCH] Fix crash that occurred when inserting a Format (foreign key constraint) --- .../ui/screens/player/StatsForNerds.kt | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/StatsForNerds.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/StatsForNerds.kt index 5539a27..cf54c5c 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/StatsForNerds.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/player/StatsForNerds.kt @@ -70,28 +70,31 @@ fun StatsForNerds( } LaunchedEffect(mediaId) { - Database.format(mediaId).distinctUntilChanged().collectLatest { - if (it?.itag == null) { - withContext(Dispatchers.IO) { - delay(2000) - Innertube.player(PlayerBody(videoId = mediaId))?.onSuccess { response -> - response.streamingData?.highestQualityFormat?.let { format -> - Database.insert( - Format( - songId = mediaId, - itag = format.itag, - mimeType = format.mimeType, - bitrate = format.bitrate, - loudnessDb = response.playerConfig?.audioConfig?.normalizedLoudnessDb, - contentLength = format.contentLength, - lastModified = format.lastModified + Database.format(mediaId).distinctUntilChanged().collectLatest { currentFormat -> + if (currentFormat?.itag == null) { + binder.player.currentMediaItem?.takeIf { it.mediaId == mediaId }?.let { mediaItem -> + withContext(Dispatchers.IO) { + delay(2000) + Innertube.player(PlayerBody(videoId = mediaId))?.onSuccess { response -> + response.streamingData?.highestQualityFormat?.let { format -> + Database.insert(mediaItem) + Database.insert( + Format( + songId = mediaId, + itag = format.itag, + mimeType = format.mimeType, + bitrate = format.bitrate, + loudnessDb = response.playerConfig?.audioConfig?.normalizedLoudnessDb, + contentLength = format.contentLength, + lastModified = format.lastModified + ) ) - ) + } } } } } else { - format = it + format = currentFormat } } }