- val formattedNumberOfBytesDownloadedString = NumberFormat.getInstance().format(downloadedKilobytesCounter)
-
- // Update the UI.
- withContext(Dispatchers.Main) {
- // Check to see if the file size is known.
- if (fileSize == -1L) { // The size of the download file is not known.
- // Update the snackbar.
- savingFileSnackbar.setText(activity.getString(R.string.saving_file_progress, formattedNumberOfBytesDownloadedString, fileNameString))
- } else { // The size of the download file is known.
- // Calculate the download percentage.
- val downloadPercentage = downloadedKilobytesCounter * 100 / fileSize
-
- // Update the snackbar.
- savingFileSnackbar.setText(activity.getString(R.string.saving_file_percentage_progress, downloadPercentage, formattedNumberOfBytesDownloadedString, formattedFileSize,
- fileNameString)
- )
+ val formattedNumberOfBytesDownloadedString = NumberFormat.getInstance().format(downloadedBytesCounterLong)
+
+ // Get the current time.
+ val currentTimeLong = Date().time
+
+ // Update the snackbar if more than 100 milliseconds have passed since the last update.
+ // Updating the snackbar is so resource intensive that it will throttle the download if it is done too frequently.
+ if (currentTimeLong - lastSnackbarUpdateLong > 100) {
+ // Store the update time.
+ lastSnackbarUpdateLong = currentTimeLong
+
+ // Update the UI.
+ withContext(Dispatchers.Main) {
+ // Check to see if the file size is known.
+ if (fileSize == -1L) { // The size of the download file is not known.
+ // Update the snackbar.
+ savingFileSnackbar.setText(activity.getString(R.string.saving_file_progress, formattedNumberOfBytesDownloadedString, fileNameString))
+ } else { // The size of the download file is known.
+ // Calculate the download percentage.
+ val downloadPercentage = downloadedBytesCounterLong * 100 / fileSize
+
+ // Update the snackbar.
+ savingFileSnackbar.setText(activity.getString(R.string.saving_file_percentage_progress, downloadPercentage, formattedNumberOfBytesDownloadedString, formattedFileSize,
+ fileNameString)
+ )
+ }