Skip to content

Native Android client for Spotify, built with Jetpack Compose

Keep disk IO off the main thread (recap, offline index, deletes)

liliapk011fd17231da
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
index 856f5d6e..e630ae00 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
@@ -179,7 +179,8 @@ object OfflineDownloader {
/** Removes the song: the file if downloaded, else its download. */
fun remove(trackId: String) {
cancel(trackId)
- OfflineStore.remove(trackId)
+ // File deletion on the IO scope, not the (UI) caller's thread.
+ scope.launch { OfflineStore.remove(trackId) }
}
fun removeAll() {
@@ -187,8 +188,8 @@ object OfflineDownloader {
ids.forEach { running.remove(it)?.cancel() }
running.values.forEach { it.cancel() }
requests.clear()
- OfflineCollections.removeAll()
- OfflineStore.removeAll()
+ // Deleting a large library can take a while: IO scope, not the caller.
+ scope.launch { OfflineCollections.removeAll(); OfflineStore.removeAll() }
}
private fun savePending() {
@@ -215,7 +216,9 @@ object OfflineDownloader {
val outcome: OfflineStatus? = try {
setStatus(r.trackId, OfflineStatus.Downloading(-1f))
val t = download(r)
- withContext(Dispatchers.Main) { OfflineStore.add(t); requests.remove(r.trackId) }
+ // Already on the IO scope: the index rewrite stays off the main thread.
+ OfflineStore.add(t)
+ withContext(Dispatchers.Main) { requests.remove(r.trackId) }
null
} catch (e: CancellationException) {
throw e
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt b/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt
index d4b27571..1554aeaa 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt
@@ -183,9 +183,14 @@ private fun formatMinutes(min: Long) = if (min >= 60) "${min / 60} h ${min % 60}
fun RecapScreen(navController: NavController) {
val periods = listOf(R.string.recap_month to 30L, R.string.recap_year to 365L, R.string.recap_all to 36_500L)
var period by remember { mutableStateOf(1) }
- val recap = remember(period) {
- ListenHistory.recap(System.currentTimeMillis() - periods[period].second * 86_400_000L)
+ // Off the main thread: the history file grows with every listen, and reading
+ // and ranking it during composition janked the screen open.
+ val loaded by produceState<ListenHistory.Recap?>(null, period) {
+ value = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {
+ ListenHistory.recap(System.currentTimeMillis() - periods[period].second * 86_400_000L)
+ }
}
+ val recap = loaded ?: ListenHistory.Recap(0, 0, emptyMap(), emptyList(), emptyList(), emptyList())
Column(Modifier.fillMaxSize().background(Background).statusBarsPadding()) {
ScreenHeader(stringResource(R.string.recap_title), navController)
LazyColumn(contentPadding = PaddingValues(bottom = 130.dp)) {