From e0dfa7eb69d0e62ceb627f715c9e0a558e397fc2 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:46:53 +0700 Subject: [PATCH] refactor(android): scope Rust download state handles consistently --- .../com/zarz/spotiflac/SelectedCoreBackend.kt | 62 +++++++------------ 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/android/app/src/rust/kotlin/com/zarz/spotiflac/SelectedCoreBackend.kt b/android/app/src/rust/kotlin/com/zarz/spotiflac/SelectedCoreBackend.kt index 7db44ff3..18b93bed 100644 --- a/android/app/src/rust/kotlin/com/zarz/spotiflac/SelectedCoreBackend.kt +++ b/android/app/src/rust/kotlin/com/zarz/spotiflac/SelectedCoreBackend.kt @@ -4,6 +4,7 @@ import android.content.Context import android.net.Uri import com.spotiflac.backend.CancellationDomain import com.spotiflac.backend.CancellationRegistry +import com.spotiflac.backend.DownloadState import com.spotiflac.backend.ExtensionManager import com.spotiflac.backend.ExtensionRepository import com.spotiflac.backend.LyricsRequest @@ -89,6 +90,11 @@ internal object RustCoreBackend : CoreBackend { return checkNotNull(manager) { "Rust backend is not initialized" } } + private fun ExtensionManager.withDownloadState(block: (DownloadState) -> T): T = + environment().use { environment -> + environment.downloadState().use(block) + } + private fun directoryAliases(value: String, sources: String, data: String): List { require(File(value).isAbsolute) { "Output directories must be absolute" } val path = File(value).canonicalPath @@ -535,11 +541,7 @@ internal object RustCoreBackend : CoreBackend { override fun waitForDownloadProgressDelta(since: Long, timeoutMs: Long): String { val current = owner() - return current.environment().use { environment -> - environment.downloadState().use { state -> - state.waitProgressDelta(since, timeoutMs) - } - } + return current.withDownloadState { state -> state.waitProgressDelta(since, timeoutMs) } } override fun openDownloadProgress(): CoreDownloadProgress { @@ -558,38 +560,22 @@ internal object RustCoreBackend : CoreBackend { override fun initItemProgress(itemId: String) { val current = owner() - current.environment().use { environment -> - environment.downloadState().use { state -> - state.initItemProgress(itemId) - } - } + current.withDownloadState { state -> state.initItemProgress(itemId) } } override fun clearItemProgress(itemId: String) { val current = owner() - current.environment().use { environment -> - environment.downloadState().use { state -> - state.clearItemProgress(itemId) - } - } + current.withDownloadState { state -> state.clearItemProgress(itemId) } } override fun cancelDownload(itemId: String) { val current = owner() - current.environment().use { environment -> - environment.downloadState().use { state -> - state.cancelDownload(itemId) - } - } + current.withDownloadState { state -> state.cancelDownload(itemId) } } override fun resetDownloadCancel(itemId: String) { val current = owner() - current.environment().use { environment -> - environment.downloadState().use { state -> - state.resetDownloadCancel(itemId) - } - } + current.withDownloadState { state -> state.resetDownloadCancel(itemId) } } override fun completeAuthCallback(state: String, code: String, sessionGrant: Boolean, onResolved: (String) -> Unit) { @@ -876,30 +862,24 @@ internal object RustCoreBackend : CoreBackend { completeSessionGrant(current, string("extension_id"), string("grant")) true } - "getAllDownloadProgress" -> current.environment().use { environment -> - environment.downloadState().use { state -> state.allProgress() } - } + "getAllDownloadProgress" -> current.withDownloadState { state -> state.allProgress() } "cleanupConnections" -> current.environment().use { it.cleanupConnections(); null } "resetDownloadCancels", "cancelDownloads" -> { val values = args["item_ids"] as? List<*> ?: error("Expected item_ids list") val itemIds = values.map { it as? String ?: error("Expected string item ID") } - current.environment().use { environment -> - environment.downloadState().use { state -> - if (method == "cancelDownloads") state.cancelDownloads(itemIds) - else state.resetDownloadCancels(itemIds) - } + current.withDownloadState { state -> + if (method == "cancelDownloads") state.cancelDownloads(itemIds) + else state.resetDownloadCancels(itemIds) } null } - "clearItemProgress", "cancelDownload", "resetDownloadCancel" -> current.environment().use { environment -> - environment.downloadState().use { state -> - when (method) { - "clearItemProgress" -> state.clearItemProgress(string("item_id")) - "cancelDownload" -> state.cancelDownload(string("item_id")) - else -> state.resetDownloadCancel(string("item_id")) - } - null + "clearItemProgress", "cancelDownload", "resetDownloadCancel" -> current.withDownloadState { state -> + when (method) { + "clearItemProgress" -> state.clearItemProgress(string("item_id")) + "cancelDownload" -> state.cancelDownload(string("item_id")) + else -> state.resetDownloadCancel(string("item_id")) } + null } "getLyricsLRC", "getLyricsLRCWithSource" -> withMediaFiles(listOf(string("file_path"))) { val request = lyricsRequest("file_path")