Remove unnecessary database call in AlbumScreen
This commit is contained in:
@@ -80,7 +80,6 @@ import java.util.Date
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
@ExperimentalAnimationApi
|
@ExperimentalAnimationApi
|
||||||
@Composable
|
@Composable
|
||||||
@@ -92,7 +91,34 @@ fun AlbumScreen(browseId: String) {
|
|||||||
album
|
album
|
||||||
?.takeIf { album.timestamp != null }
|
?.takeIf { album.timestamp != null }
|
||||||
?.let(Result.Companion::success)
|
?.let(Result.Companion::success)
|
||||||
?: fetchAlbum(browseId)
|
?: YouTube.album(browseId)?.map { youtubeAlbum ->
|
||||||
|
Database.upsert(
|
||||||
|
Album(
|
||||||
|
id = browseId,
|
||||||
|
title = youtubeAlbum.title,
|
||||||
|
thumbnailUrl = youtubeAlbum.thumbnail?.url,
|
||||||
|
year = youtubeAlbum.year,
|
||||||
|
authorsText = youtubeAlbum.authors?.joinToString("") { it.name },
|
||||||
|
shareUrl = youtubeAlbum.url,
|
||||||
|
timestamp = System.currentTimeMillis()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
youtubeAlbum.items?.forEachIndexed { position, albumItem ->
|
||||||
|
albumItem.toMediaItem(browseId, youtubeAlbum)?.let { mediaItem ->
|
||||||
|
Database.insert(mediaItem)
|
||||||
|
Database.upsert(
|
||||||
|
SongAlbumMap(
|
||||||
|
songId = mediaItem.mediaId,
|
||||||
|
albumId = browseId,
|
||||||
|
position = position
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
null
|
||||||
|
}
|
||||||
}.distinctUntilChanged()
|
}.distinctUntilChanged()
|
||||||
}.collectAsState(initial = null, context = Dispatchers.IO)
|
}.collectAsState(initial = null, context = Dispatchers.IO)
|
||||||
|
|
||||||
@@ -112,7 +138,8 @@ fun AlbumScreen(browseId: String) {
|
|||||||
|
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
state = lazyListState,
|
state = lazyListState,
|
||||||
contentPadding = WindowInsets.systemBars.asPaddingValues().add(bottom = Dimensions.collapsedPlayer),
|
contentPadding = WindowInsets.systemBars.asPaddingValues()
|
||||||
|
.add(bottom = Dimensions.collapsedPlayer),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.background(colorPalette.background0)
|
.background(colorPalette.background0)
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
@@ -307,9 +334,6 @@ fun AlbumScreen(browseId: String) {
|
|||||||
albumResult
|
albumResult
|
||||||
?.getOrNull()
|
?.getOrNull()
|
||||||
?.let(Database::delete)
|
?.let(Database::delete)
|
||||||
runBlocking(Dispatchers.IO) {
|
|
||||||
fetchAlbum(browseId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -402,35 +426,3 @@ private fun LoadingOrError(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun fetchAlbum(browseId: String): Result<Album>? {
|
|
||||||
return YouTube.album(browseId)
|
|
||||||
?.map { youtubeAlbum ->
|
|
||||||
val album = Album(
|
|
||||||
id = browseId,
|
|
||||||
title = youtubeAlbum.title,
|
|
||||||
thumbnailUrl = youtubeAlbum.thumbnail?.url,
|
|
||||||
year = youtubeAlbum.year,
|
|
||||||
authorsText = youtubeAlbum.authors?.joinToString("") { it.name },
|
|
||||||
shareUrl = youtubeAlbum.url,
|
|
||||||
timestamp = System.currentTimeMillis()
|
|
||||||
)
|
|
||||||
|
|
||||||
Database.upsert(album)
|
|
||||||
|
|
||||||
youtubeAlbum.items?.forEachIndexed { position, albumItem ->
|
|
||||||
albumItem.toMediaItem(browseId, youtubeAlbum)?.let { mediaItem ->
|
|
||||||
Database.insert(mediaItem)
|
|
||||||
Database.upsert(
|
|
||||||
SongAlbumMap(
|
|
||||||
songId = mediaItem.mediaId,
|
|
||||||
albumId = browseId,
|
|
||||||
position = position
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
album
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user