From 0539ff4780baefaa1e420073ed8a62a9dc9c69eb Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 10 Sep 2025 21:50:42 +0530 Subject: [PATCH] Fixed: Show the downloaded size and total file size in the download notification. * Displaying the downloaded size and total size in the download notification. * Displaying the same information in the Download screen UI. We have moved the Download Progress text ("In Progress", "Downloading", "Paused") above the progress bar, and now show the size information in its place. --- .../library/online/DownloadBookItem.kt | 28 +++++++++--- .../FetchDownloadNotificationManager.kt | 44 ++++++++++++++++--- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/DownloadBookItem.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/DownloadBookItem.kt index af3f2284f..a5bb5ba6d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/DownloadBookItem.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/online/DownloadBookItem.kt @@ -54,6 +54,7 @@ import org.kiwix.kiwixmobile.core.utils.ComposeDimens.ONE_DP import org.kiwix.kiwixmobile.core.utils.ComposeDimens.SIXTEEN_DP import org.kiwix.kiwixmobile.core.utils.ComposeDimens.TEN_DP import org.kiwix.kiwixmobile.core.utils.ComposeDimens.TWO_DP +import org.kiwix.kiwixmobile.core.zim_manager.Byte import org.kiwix.kiwixmobile.ui.BookDescription import org.kiwix.kiwixmobile.ui.BookIcon import org.kiwix.kiwixmobile.ui.BookTitle @@ -113,6 +114,7 @@ private fun DownloadBookContent( BookTitle(item.title) Spacer(modifier = Modifier.height(TWO_DP)) BookDescription(item.description.orEmpty()) + DownloadStateText(item) ContentLoadingProgressBar( progressBarStyle = ProgressBarStyle.HORIZONTAL, modifier = Modifier.padding(horizontal = ONE_DP, vertical = FIVE_DP), @@ -183,17 +185,33 @@ private fun DownloadStateRow(item: LibraryDownloadItem) { verticalAlignment = Alignment.CenterVertically ) { Text( - text = item.downloadState.toReadableState(LocalContext.current).toString(), + text = item.readableEta.toString(), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onTertiary, - modifier = Modifier - .weight(1f) - .testTag(DOWNLOADING_STATE_TEXT_TESTING_TAG) + modifier = Modifier.weight(1f) ) Text( - text = item.readableEta.toString(), + text = getDownloadedSizeText(item.bytesDownloaded, item.totalSizeBytes), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onTertiary ) } } + +private fun getDownloadedSizeText(downloadedBytes: Long, totalBytes: Long): String { + if (totalBytes <= 0 || downloadedBytes <= 0) return "" + val downloadedText = Byte(downloadedBytes.toString()).humanReadable + val totalText = Byte(totalBytes.toString()).humanReadable + return "$downloadedText/$totalText" +} + +@Composable +private fun DownloadStateText(item: LibraryDownloadItem) { + Text( + text = item.downloadState.toReadableState(LocalContext.current).toString(), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onTertiary, + modifier = Modifier + .testTag(DOWNLOADING_STATE_TEXT_TESTING_TAG) + ) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/FetchDownloadNotificationManager.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/FetchDownloadNotificationManager.kt index 150a79d98..83152c4a1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/FetchDownloadNotificationManager.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/FetchDownloadNotificationManager.kt @@ -65,6 +65,7 @@ import org.kiwix.kiwixmobile.core.Intents import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao import org.kiwix.kiwixmobile.core.main.CoreMainActivity +import org.kiwix.kiwixmobile.core.zim_manager.Byte import javax.inject.Inject const val DOWNLOAD_NOTIFICATION_TITLE = "OPEN_ZIM_FILE" @@ -117,13 +118,38 @@ class FetchDownloadNotificationManager @Inject constructor( return when { downloadNotification.isCompleted -> context.getString(R.string.complete) downloadNotification.isFailed -> context.getString(R.string.download_failed_state) - downloadNotification.isPaused -> context.getString(R.string.paused_state) + downloadNotification.isPaused -> buildSubtitle( + context.getString(R.string.paused_state), + downloadNotification.downloaded, + downloadNotification.total + ) + downloadNotification.isQueued -> context.getString(R.string.resuming_state) downloadNotification.etaInMilliSeconds < 0 -> context.getString(R.string.downloading_state) - else -> super.getSubtitleText(context, downloadNotification) + else -> buildSubtitle( + super.getSubtitleText(context, downloadNotification), + downloadNotification.downloaded, + downloadNotification.total + ) } } + private fun buildSubtitle( + mainText: String, + downloaded: Long, + total: Long + ): String { + val sizeText = getDownloadedSizeText(downloaded, total) + return "$mainText • $sizeText" + } + + private fun getDownloadedSizeText(downloadedBytes: Long, totalBytes: Long): String { + if (downloadedBytes <= 0 || totalBytes <= 0) return "" + val downloadedText = Byte(downloadedBytes.toString()).humanReadable + val totalText = Byte(totalBytes.toString()).humanReadable + return "$downloadedText/$totalText" + } + @RequiresApi(Build.VERSION_CODES.O) private fun createChannel(channelId: String, context: Context) = NotificationChannel( @@ -250,12 +276,12 @@ class FetchDownloadNotificationManager @Inject constructor( download: Download ) { val notificationBuilder = getNotificationBuilder(download.id, download.id) - val cancelNotification = getCancelNotification(fetch, download, notificationBuilder) - downloadNotificationManager.notify(download.id, cancelNotification) + val pauseNotification = getPauseNotification(fetch, download, notificationBuilder) + downloadNotificationManager.notify(download.id, pauseNotification) } @Suppress("InjectDispatcher") - private fun getCancelNotification( + private fun getPauseNotification( fetch: Fetch, download: Download, notificationBuilder: Builder @@ -270,7 +296,13 @@ class FetchDownloadNotificationManager @Inject constructor( return notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setSmallIcon(android.R.drawable.stat_sys_download_done) .setContentTitle(notificationTitle) - .setContentText(context.getString(R.string.paused_state)) + .setContentText( + buildSubtitle( + context.getString(R.string.paused_state), + download.downloaded, + download.total + ) + ) // Set the ongoing true so that could not cancel the pause notification. // However, on Android 14 and above user can cancel the notification by swipe right so we // can't control that see https://developer.android.com/about/versions/14/behavior-changes-all#non-dismissable-notifications