refactor(android): move the download service and worker backend calls into MainActivityServiceCalls.kt

MainActivity.kt should be the dispatcher only. downloadByStrategy, the
download foreground service, background work and native download worker
cases keep their labels and order in the backend `when`; the multi-line
ones now call MainActivity.handle<Name> functions in
MainActivityServiceCalls.kt, and the one-line stop/pause/resume/cancel
cases stay inline.

Bodies move verbatim apart from the receiver label for this@MainActivity.
This commit is contained in:
zarzet committed 2026-10-19 01:24:48 +07:00
1 parent cbf41441ff
commit 00b661be21
2 files changed
+139 -108

No files matched your search

@@ -503,13 +503,7 @@ class MainActivity: FlutterFragmentActivity() {
finishAndRemoveTask()
result.success(null)
}
"downloadByStrategy" -> {
val requestJson = call.arguments as String
val response = withContext(Dispatchers.IO) {
SafDownloadHandler.handle(this@MainActivity, requestJson, coreBackend)
}
result.success(response)
}
"downloadByStrategy" -> handleDownloadByStrategy(call, result)
"getBackendImplementations" -> {
result.success(
mapOf(
@@ -559,103 +553,25 @@ class MainActivity: FlutterFragmentActivity() {
"downloadCoverToFile" -> handleDownloadCoverToFile(call, result)
"extractCoverToFile" -> handleExtractCoverToFile(call, result)
"reEnrichFile" -> handleReEnrichFile(call, result)
"startDownloadService" -> {
val trackName = call.argument<String>("track_name") ?: ""
val artistName = call.argument<String>("artist_name") ?: ""
val queueCount = call.argument<Int>("queue_count") ?: 0
DownloadService.start(this@MainActivity, trackName, artistName, queueCount)
result.success(null)
}
"startDownloadService" -> handleStartDownloadService(call, result)
"stopDownloadService" -> {
DownloadService.stop(this@MainActivity)
result.success(null)
}
"updateDownloadServiceProgress" -> {
val trackName = call.argument<String>("track_name") ?: ""
val artistName = call.argument<String>("artist_name") ?: ""
val progress = (call.argument<Number>("progress") ?: 0).toLong()
val total = (call.argument<Number>("total") ?: 0).toLong()
val queueCount = (call.argument<Number>("queue_count") ?: 0).toInt()
val status = call.argument<String>("status") ?: "downloading"
DownloadService.updateProgress(this@MainActivity, trackName, artistName, progress, total, queueCount, status)
result.success(null)
}
"updateDownloadServiceProgress" -> handleUpdateDownloadServiceProgress(call, result)
"isDownloadServiceRunning" -> {
result.success(DownloadService.isServiceRunning())
}
"startBackgroundWork" -> {
result.success(
BackgroundWorkService.start(
this@MainActivity,
call.argument<String>("kind") ?: "",
call.argument<String>("title") ?: "",
call.argument<String>("text") ?: "",
),
)
}
"updateBackgroundWork" -> {
BackgroundWorkService.update(
call.argument<String>("kind") ?: "",
call.argument<String>("title") ?: "",
call.argument<String>("text") ?: "",
call.argument<Int>("progress") ?: -1,
)
result.success(null)
}
"startBackgroundWork" -> handleStartBackgroundWork(call, result)
"updateBackgroundWork" -> handleUpdateBackgroundWork(call, result)
"stopBackgroundWork" -> {
BackgroundWorkService.stop(call.argument<String>("kind") ?: "")
result.success(null)
}
"startNativeDownloadWorker" -> {
val requestsJson = call.argument<String>("requests_json") ?: "[]"
val settingsJson = call.argument<String>("settings_json") ?: "{}"
val requestsPath = call.argument<String>("requests_path") ?: ""
val settingsPath = call.argument<String>("settings_path") ?: ""
if (requestsPath.isNotBlank()) {
DownloadService.startNativeQueueFromFiles(
this@MainActivity,
requestsPath,
settingsPath
)
} else {
DownloadService.startNativeQueue(this@MainActivity, requestsJson, settingsJson)
}
result.success(null)
}
"appendNativeDownloadWorkerRequests" -> {
val requestsPath = call.argument<String>("requests_path") ?: ""
val runId = call.argument<String>("run_id") ?: ""
if (requestsPath.isNotBlank() && runId.isNotBlank()) {
DownloadService.appendNativeQueueFromFile(
this@MainActivity,
requestsPath,
runId,
)
}
result.success(null)
}
"finishNativeDownloadWorkerPreparation" -> {
val runId = call.argument<String>("run_id") ?: ""
if (runId.isNotBlank()) {
DownloadService.finishNativeQueuePreparation(
this@MainActivity,
runId,
)
}
result.success(null)
}
"acknowledgeNativeDownloadWorkerItems" -> {
val runId = call.argument<String>("run_id") ?: ""
val itemIdsJson = call.argument<String>("item_ids_json") ?: "[]"
if (runId.isNotBlank()) {
DownloadService.acknowledgeNativeQueueItems(
this@MainActivity,
runId,
itemIdsJson,
)
}
result.success(null)
}
"startNativeDownloadWorker" -> handleStartNativeDownloadWorker(call, result)
"appendNativeDownloadWorkerRequests" -> handleAppendNativeDownloadWorkerRequests(call, result)
"finishNativeDownloadWorkerPreparation" -> handleFinishNativeDownloadWorkerPreparation(call, result)
"acknowledgeNativeDownloadWorkerItems" -> handleAcknowledgeNativeDownloadWorkerItems(call, result)
"pauseNativeDownloadWorker" -> {
DownloadService.pauseNativeQueue(this@MainActivity)
result.success(null)
@@ -668,21 +584,7 @@ class MainActivity: FlutterFragmentActivity() {
DownloadService.cancelNativeQueue(this@MainActivity)
result.success(null)
}
"getNativeDownloadWorkerSnapshot" -> {
val sinceStateSerial =
(call.argument<Number>("since_state_serial") ?: 0L).toLong()
// The snapshot can be megabytes late in a large
// batch; read and parse it off the main thread.
val payload = withContext(Dispatchers.IO) {
parseJsonPayload(
DownloadService.getNativeWorkerSnapshot(
this@MainActivity,
sinceStateSerial
)
)
}
result.success(payload)
}
"getNativeDownloadWorkerSnapshot" -> handleGetNativeDownloadWorkerSnapshot(call, result)
"releaseMemory" -> {
withContext(Dispatchers.IO) {
coreBackend.releaseIdleResources()
@@ -0,0 +1,129 @@
package com.zarz.spotiflac
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
// Download and background-work backend calls: the download strategy entry,
// the download foreground service, background work notifications, and the
// native download worker queue. One-line cases stay inline in MainActivity's
// backend `when`, which dispatches the rest here in the order it lists them.
internal suspend fun MainActivity.handleDownloadByStrategy(call: MethodCall, result: MethodChannel.Result) {
val requestJson = call.arguments as String
val response = withContext(Dispatchers.IO) {
SafDownloadHandler.handle(this@handleDownloadByStrategy, requestJson, coreBackend)
}
result.success(response)
}
internal fun MainActivity.handleStartDownloadService(call: MethodCall, result: MethodChannel.Result) {
val trackName = call.argument<String>("track_name") ?: ""
val artistName = call.argument<String>("artist_name") ?: ""
val queueCount = call.argument<Int>("queue_count") ?: 0
DownloadService.start(this@handleStartDownloadService, trackName, artistName, queueCount)
result.success(null)
}
internal fun MainActivity.handleUpdateDownloadServiceProgress(call: MethodCall, result: MethodChannel.Result) {
val trackName = call.argument<String>("track_name") ?: ""
val artistName = call.argument<String>("artist_name") ?: ""
val progress = (call.argument<Number>("progress") ?: 0).toLong()
val total = (call.argument<Number>("total") ?: 0).toLong()
val queueCount = (call.argument<Number>("queue_count") ?: 0).toInt()
val status = call.argument<String>("status") ?: "downloading"
DownloadService.updateProgress(this@handleUpdateDownloadServiceProgress, trackName, artistName, progress, total, queueCount, status)
result.success(null)
}
internal fun MainActivity.handleStartBackgroundWork(call: MethodCall, result: MethodChannel.Result) {
result.success(
BackgroundWorkService.start(
this@handleStartBackgroundWork,
call.argument<String>("kind") ?: "",
call.argument<String>("title") ?: "",
call.argument<String>("text") ?: "",
),
)
}
internal fun MainActivity.handleUpdateBackgroundWork(call: MethodCall, result: MethodChannel.Result) {
BackgroundWorkService.update(
call.argument<String>("kind") ?: "",
call.argument<String>("title") ?: "",
call.argument<String>("text") ?: "",
call.argument<Int>("progress") ?: -1,
)
result.success(null)
}
internal fun MainActivity.handleStartNativeDownloadWorker(call: MethodCall, result: MethodChannel.Result) {
val requestsJson = call.argument<String>("requests_json") ?: "[]"
val settingsJson = call.argument<String>("settings_json") ?: "{}"
val requestsPath = call.argument<String>("requests_path") ?: ""
val settingsPath = call.argument<String>("settings_path") ?: ""
if (requestsPath.isNotBlank()) {
DownloadService.startNativeQueueFromFiles(
this@handleStartNativeDownloadWorker,
requestsPath,
settingsPath
)
} else {
DownloadService.startNativeQueue(this@handleStartNativeDownloadWorker, requestsJson, settingsJson)
}
result.success(null)
}
internal fun MainActivity.handleAppendNativeDownloadWorkerRequests(call: MethodCall, result: MethodChannel.Result) {
val requestsPath = call.argument<String>("requests_path") ?: ""
val runId = call.argument<String>("run_id") ?: ""
if (requestsPath.isNotBlank() && runId.isNotBlank()) {
DownloadService.appendNativeQueueFromFile(
this@handleAppendNativeDownloadWorkerRequests,
requestsPath,
runId,
)
}
result.success(null)
}
internal fun MainActivity.handleFinishNativeDownloadWorkerPreparation(call: MethodCall, result: MethodChannel.Result) {
val runId = call.argument<String>("run_id") ?: ""
if (runId.isNotBlank()) {
DownloadService.finishNativeQueuePreparation(
this@handleFinishNativeDownloadWorkerPreparation,
runId,
)
}
result.success(null)
}
internal fun MainActivity.handleAcknowledgeNativeDownloadWorkerItems(call: MethodCall, result: MethodChannel.Result) {
val runId = call.argument<String>("run_id") ?: ""
val itemIdsJson = call.argument<String>("item_ids_json") ?: "[]"
if (runId.isNotBlank()) {
DownloadService.acknowledgeNativeQueueItems(
this@handleAcknowledgeNativeDownloadWorkerItems,
runId,
itemIdsJson,
)
}
result.success(null)
}
internal suspend fun MainActivity.handleGetNativeDownloadWorkerSnapshot(call: MethodCall, result: MethodChannel.Result) {
val sinceStateSerial =
(call.argument<Number>("since_state_serial") ?: 0L).toLong()
// The snapshot can be megabytes late in a large
// batch; read and parse it off the main thread.
val payload = withContext(Dispatchers.IO) {
parseJsonPayload(
DownloadService.getNativeWorkerSnapshot(
this@handleGetNativeDownloadWorkerSnapshot,
sinceStateSerial
)
)
}
result.success(payload)
}