Skip to content

Commit 546f25d

Browse files
committed
better logic
1 parent bfc9b80 commit 546f25d

1 file changed

Lines changed: 38 additions & 31 deletions

File tree

composeApp/src/androidMain/kotlin/paige/navic/domain/manager/base/BaseDownloadManager.android.kt

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.coroutines.delay
1111
import kotlinx.coroutines.withContext
1212
import paige.navic.domain.manager.StorageManager
1313
import paige.navic.domain.models.DomainSong
14+
import paige.navic.util.core.Logger
1415
import java.io.File
1516
import kotlin.time.Duration.Companion.milliseconds
1617

@@ -42,44 +43,50 @@ actual class BaseDownloadManager(
4243
var downloading = true
4344
var finalPath = ""
4445

45-
while (downloading) {
46-
val query = DownloadManager.Query().setFilterById(downloadId)
47-
val cursor: Cursor = downloadManager.query(query)
46+
try {
47+
while (downloading) {
48+
val query = DownloadManager.Query().setFilterById(downloadId)
49+
val cursor: Cursor = downloadManager.query(query)
4850

49-
if (cursor.moveToFirst()) {
50-
val status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS))
51-
val bytesDownloaded = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
52-
val bytesTotal = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
51+
if (cursor.moveToFirst()) {
52+
val status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS))
53+
val bytesDownloaded = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
54+
val bytesTotal = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
5355

54-
if (bytesTotal > 0) {
55-
onProgress(bytesDownloaded.toFloat() / bytesTotal.toFloat())
56-
}
56+
if (bytesTotal > 0) {
57+
onProgress(bytesDownloaded.toFloat() / bytesTotal.toFloat())
58+
}
5759

58-
when (status) {
59-
DownloadManager.STATUS_SUCCESSFUL -> {
60-
val localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI))
61-
val sourceFile = File(Uri.parse(localUri).path!!)
62-
63-
finalPath = storageManager.getDownloadPath(song.id, extension)
64-
val destinationFile = File(finalPath)
65-
66-
withContext(Dispatchers.IO) {
67-
sourceFile.copyTo(destinationFile, overwrite = true)
68-
sourceFile.delete()
60+
when (status) {
61+
DownloadManager.STATUS_SUCCESSFUL -> {
62+
val localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI))
63+
val sourceFile = File(Uri.parse(localUri).path!!)
64+
65+
finalPath = storageManager.getDownloadPath(song.id, extension)
66+
val destinationFile = File(finalPath)
67+
68+
withContext(Dispatchers.IO) {
69+
sourceFile.copyTo(destinationFile, overwrite = true)
70+
sourceFile.delete()
71+
}
72+
73+
downloading = false
74+
}
75+
DownloadManager.STATUS_FAILED -> {
76+
val reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON))
77+
downloading = false
78+
Logger.e("BaseDownloadManager", "Download failed for ${song.id} with reason: $reason")
79+
throw Exception("Download failed with reason: $reason")
6980
}
70-
71-
downloading = false
72-
}
73-
DownloadManager.STATUS_FAILED -> {
74-
downloading = false
75-
throw Exception("Download failed with status: $status")
7681
}
7782
}
83+
cursor.close()
84+
if (downloading) {
85+
delay(500.milliseconds)
86+
}
7887
}
79-
cursor.close()
80-
if (downloading) {
81-
delay(500.milliseconds)
82-
}
88+
} finally {
89+
downloadManager.remove(downloadId)
8390
}
8491

8592
return finalPath

0 commit comments

Comments
 (0)