Fix crash that occurred when inserting a Format (foreign key constraint)

This commit is contained in:
vfsfitvnm 2022-11-02 10:59:27 +01:00
parent fab56dd302
commit ac3019ef25

View file

@ -70,28 +70,31 @@ fun StatsForNerds(
} }
LaunchedEffect(mediaId) { LaunchedEffect(mediaId) {
Database.format(mediaId).distinctUntilChanged().collectLatest { Database.format(mediaId).distinctUntilChanged().collectLatest { currentFormat ->
if (it?.itag == null) { if (currentFormat?.itag == null) {
withContext(Dispatchers.IO) { binder.player.currentMediaItem?.takeIf { it.mediaId == mediaId }?.let { mediaItem ->
delay(2000) withContext(Dispatchers.IO) {
Innertube.player(PlayerBody(videoId = mediaId))?.onSuccess { response -> delay(2000)
response.streamingData?.highestQualityFormat?.let { format -> Innertube.player(PlayerBody(videoId = mediaId))?.onSuccess { response ->
Database.insert( response.streamingData?.highestQualityFormat?.let { format ->
Format( Database.insert(mediaItem)
songId = mediaId, Database.insert(
itag = format.itag, Format(
mimeType = format.mimeType, songId = mediaId,
bitrate = format.bitrate, itag = format.itag,
loudnessDb = response.playerConfig?.audioConfig?.normalizedLoudnessDb, mimeType = format.mimeType,
contentLength = format.contentLength, bitrate = format.bitrate,
lastModified = format.lastModified loudnessDb = response.playerConfig?.audioConfig?.normalizedLoudnessDb,
contentLength = format.contentLength,
lastModified = format.lastModified
)
) )
) }
} }
} }
} }
} else { } else {
format = it format = currentFormat
} }
} }
} }