Skip to content

Commit e44c8e8

Browse files
dev: remove cancelled download status
1 parent 669d3b7 commit e44c8e8

9 files changed

Lines changed: 33 additions & 21 deletions

File tree

android/src/main/kotlin/project/pipepipe/app/PipePipeApplication.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class PipePipeApplication : Application() {
6363
SharedContext.platformDatabaseActions = AndroidPlatformDatabaseActions(this)
6464
SharedContext.platformDatabaseActions.initializeDatabase()
6565

66+
// Clean up cancelled downloads (legacy status)
67+
GlobalScope.launch {
68+
runCatching { DatabaseOperations.deleteAllCancelledDownloads() }
69+
}
70+
6671
// Initialize youtubedl-android and FFmpeg
6772
try {
6873
YoutubeDL.getInstance().init(this)

android/src/main/kotlin/project/pipepipe/app/download/DownloadManager.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class DownloadManager(private val context: Context) {
232232
}
233233

234234
/**
235-
* Cancel a download
235+
* Cancel a download (delete directly)
236236
*/
237237
fun cancelDownload(downloadId: Long) {
238238
Log.d(TAG, "Canceling download: $downloadId")
@@ -241,8 +241,12 @@ class DownloadManager(private val context: Context) {
241241
activeWorkers.remove(downloadId)
242242
updateActiveDownloadIds()
243243

244+
// Cancel progress notification
245+
DownloadService.cancelProgressNotification(context, downloadId)
246+
244247
scope.launch {
245-
DatabaseOperations.updateDownloadStatus(downloadId, DownloadStatus.CANCELED.name, null)
248+
// Delete from database
249+
DatabaseOperations.deleteDownload(downloadId)
246250

247251
// Start next queued download
248252
startNextInQueue()

android/src/main/kotlin/project/pipepipe/app/download/DownloadWorker.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class DownloadWorker(
119119
onStateChange(DownloadStatus.PAUSED, null)
120120
} else {
121121
Log.d(TAG, "Download canceled by user")
122-
onStateChange(DownloadStatus.CANCELED, null)
123122
}
124123
} catch (e: Exception) {
125124
Log.e(TAG, "Download error", e)

android/src/main/kotlin/project/pipepipe/app/service/DownloadService.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,10 @@ class DownloadService : Service() {
166166
notificationManager.notify(PROGRESS_NOTIFICATION_BASE_ID + downloadId.toInt(), notification)
167167
}
168168
}
169+
170+
fun cancelProgressNotification(context: Context, downloadId: Long) {
171+
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
172+
notificationManager.cancel(PROGRESS_NOTIFICATION_BASE_ID + downloadId.toInt())
173+
}
169174
}
170175
}

android/src/main/kotlin/project/pipepipe/app/ui/item/DownloadItem.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private fun TitleRow(
262262
)
263263
}
264264

265-
DownloadStatus.FAILED, DownloadStatus.CANCELED -> {
265+
DownloadStatus.FAILED -> {
266266
DropdownMenuItem(
267267
text = { Text(stringResource(MR.strings.retry)) },
268268
leadingIcon = { Icon(Icons.Default.Refresh, contentDescription = null) },
@@ -389,7 +389,6 @@ private fun BottomStatusSection(state: DownloadItemState) {
389389
DownloadStatus.PREPROCESSING -> stringResource(MR.strings.download_pre_processing)
390390
DownloadStatus.POSTPROCESSING -> stringResource(MR.strings.download_post_processing)
391391
DownloadStatus.FAILED -> "Failed: ${state.errorMessage?.take(30) ?: "Unknown error"}"
392-
DownloadStatus.CANCELED -> "Canceled"
393392
DownloadStatus.DOWNLOADING -> state.downloadSpeed
394393
?: stringResource(MR.strings.download_pre_processing)
395394

library/src/commonMain/kotlin/project/pipepipe/app/database/DatabaseOperations.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,4 +745,8 @@ object DatabaseOperations {
745745
suspend fun findDownloadByUrlAndType(url: String, downloadType: String) = withContext(Dispatchers.IO) {
746746
database.appDatabaseQueries.findDownloadByUrlAndType(url, downloadType).executeAsOneOrNull()
747747
}
748+
749+
suspend fun deleteAllCancelledDownloads() = withContext(Dispatchers.IO) {
750+
database.appDatabaseQueries.deleteAllCancelledDownloads()
751+
}
748752
}

library/src/commonMain/kotlin/project/pipepipe/app/uistate/BaseUiState.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ enum class DownloadStatus {
170170
POSTPROCESSING,
171171
PAUSED,
172172
COMPLETED,
173-
FAILED,
174-
CANCELED;
173+
FAILED;
175174

176-
fun isTerminal() = this in listOf(COMPLETED, FAILED, CANCELED)
175+
fun isTerminal() = this in listOf(COMPLETED, FAILED)
177176
fun isActive() = this in listOf(QUEUED, FETCHING_INFO, PREPROCESSING, DOWNLOADING, POSTPROCESSING)
178177
}
179178

library/src/commonMain/kotlin/project/pipepipe/app/viewmodel/DownloadViewModel.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,13 @@ class DownloadViewModel : BaseViewModel<DownloadUiState>(DownloadUiState()) {
2323
*/
2424
private fun observeDownloads() {
2525
viewModelScope.launch {
26-
try {
27-
// Get all downloads and observe changes
28-
val downloads = DatabaseOperations.getAllDownloads()
29-
val downloadItems = downloads.map { it.toDownloadItemState() }
30-
31-
setState { state ->
32-
state.copy(
33-
downloads = downloadItems,
34-
activeDownloadCount = downloadItems.count { it.status.isActive() }
35-
)
36-
}
37-
} catch (e: Exception) {
38-
// Handle error silently or log
26+
val downloads = DatabaseOperations.getAllDownloads()
27+
val downloadItems = downloads.map { it.toDownloadItemState() }
28+
setState { state ->
29+
state.copy(
30+
downloads = downloadItems,
31+
activeDownloadCount = downloadItems.count { it.status.isActive() }
32+
)
3933
}
4034
}
4135
}

library/src/commonMain/sqldelight/project/pipepipe/database/AppDatabase.sq

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,6 @@ SELECT * FROM downloads WHERE url = ? AND quality = ? AND download_type = ? LIMI
695695

696696
findDownloadByUrlAndType:
697697
SELECT * FROM downloads WHERE url = ? AND download_type = ? LIMIT 1;
698+
699+
deleteAllCancelledDownloads:
700+
DELETE FROM downloads WHERE status = 'CANCELED';

0 commit comments

Comments
 (0)