Skip to content

Commit 933658c

Browse files
Improved method naming.
1 parent da9528b commit 933658c

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadMonitorService.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ class DownloadMonitorService : Service() {
136136
if (downloadRoomDao.downloads().blockingFirst().isNotEmpty()) {
137137
checkDownloads()
138138
} else {
139-
// dispose to avoid unnecessary request to downloadManager
140-
// when there is no download ongoing.
141-
stopMonitoringDownloads()
139+
stopForegroundServiceForDownloads()
142140
}
143141
}
144142
} catch (ignore: Exception) {
@@ -405,10 +403,11 @@ class DownloadMonitorService : Service() {
405403
updateNotification(downloadModel, downloadEntity.title, downloadEntity.description)
406404
return@let
407405
}
408-
cancelNotification(downloadId)
406+
cancelNotificationAndAssignNewNotificationToForegroundService(downloadId)
409407
} ?: run {
410-
// already downloaded/cancelled so cancel the notification if any running.
411-
cancelNotification(downloadId)
408+
// already downloaded/cancelled so cancel the notification if any running, and
409+
// assign new notification to foreground service.
410+
cancelNotificationAndAssignNewNotificationToForegroundService(downloadId)
412411
}
413412
}
414413
}
@@ -465,12 +464,12 @@ class DownloadMonitorService : Service() {
465464
}
466465
}
467466

468-
private fun cancelNotification(downloadId: Long) {
467+
private fun cancelNotificationAndAssignNewNotificationToForegroundService(downloadId: Long) {
469468
downloadNotificationManager.cancelNotification(downloadId.toInt())
470-
assignNewForegroundNotification()
469+
updateForegroundNotificationOrStopService()
471470
}
472471

473-
private fun assignNewForegroundNotification() {
472+
private fun updateForegroundNotificationOrStopService() {
474473
val activeDownloads = getActiveDownloads()
475474
if (activeDownloads.isNotEmpty()) {
476475
// Promote the first active download to foreground
@@ -487,7 +486,7 @@ class DownloadMonitorService : Service() {
487486
startForeground(foreGroundServiceInformation.second, notification)
488487
} else {
489488
// Stop the service if no active downloads remain
490-
stopMonitoringDownloads()
489+
stopForegroundServiceForDownloads()
491490
}
492491
}
493492

@@ -511,7 +510,7 @@ class DownloadMonitorService : Service() {
511510
downloadNotificationModel,
512511
object : AssignNewForegroundServiceNotification {
513512
override fun assignNewForegroundServiceNotification(downloadId: Long) {
514-
cancelNotification(downloadId)
513+
cancelNotificationAndAssignNewNotificationToForegroundService(downloadId)
515514
}
516515
}
517516
)
@@ -605,7 +604,7 @@ class DownloadMonitorService : Service() {
605604
super.onDestroy()
606605
}
607606

608-
private fun stopMonitoringDownloads() {
607+
private fun stopForegroundServiceForDownloads() {
609608
foreGroundServiceInformation = true to DEFAULT_INT_VALUE
610609
monitoringDisposable?.dispose()
611610
stopForeground(STOP_FOREGROUND_REMOVE)

0 commit comments

Comments
 (0)