Skip to content

Commit

Permalink
Merge pull request #1312 from Infomaniak/fix/snackbar-islowmemory
Browse files Browse the repository at this point in the history
Fix wrong snackbar being displayed when importing files on low memory
  • Loading branch information
LunarX authored May 24, 2024
2 parents 8e11fd2 + 3f9c57a commit 9de2d2b
Showing 1 changed file with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class ImportFilesDialog : DialogFragment() {
private var currentImportFile: IOFile? = null
private var successCount = 0

private var isMemoryError: Boolean = false

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val countMessage = requireContext().resources.getQuantityString(R.plurals.preparingToUpload, importCount, importCount)
dialogBinding.description.text = countMessage
Expand All @@ -74,7 +76,12 @@ class ImportFilesDialog : DialogFragment() {
val errorCount = importCount - successCount

if (errorCount > 0) {
val errorMessage = resources.getQuantityString(R.plurals.snackBarUploadError, errorCount, errorCount)
val errorMessage = if (isMemoryError) {
getString(R.string.uploadOutOfMemoryError)
} else {
resources.getQuantityString(R.plurals.snackBarUploadError, errorCount, errorCount)
}

showSnackbar(errorMessage, showAboveFab = true)
}

Expand All @@ -90,18 +97,14 @@ class ImportFilesDialog : DialogFragment() {
initUpload(uri)
}.onFailure { exception ->
exception.printStackTrace()
Sentry.captureException(exception)

errorCount++
}
}
if (exception is NotEnoughRamException) {
isMemoryError = true
} else {
Sentry.captureException(exception)
}

if (errorCount > 0) {
withContext(Dispatchers.Main) {
showSnackbar(
title = resources.getQuantityString(R.plurals.snackBarUploadError, errorCount, errorCount),
showAboveFab = true,
)
errorCount++
}
}

Expand All @@ -113,32 +116,27 @@ class ImportFilesDialog : DialogFragment() {

private suspend fun initUpload(uri: Uri) = withContext(Dispatchers.IO) {
requireContext().contentResolver.query(uri, null, null, null, null)?.use { cursor ->
if (isLowMemory()) throw NotEnoughRamException()

if (cursor.moveToFirst()) {
val fileName = cursor.getFileName(uri)
val (fileCreatedAt, fileModifiedAt) = getFileDates(cursor)

when {
isLowMemory() -> withContext(Dispatchers.Main) {
showSnackbar(R.string.uploadOutOfMemoryError, showAboveFab = true)
}
else -> {
val outputFile = getOutputFile(uri, fileModifiedAt)
ensureActive()
UploadFile(
uri = outputFile.toUri().toString(),
driveId = navArgs.driveId,
fileCreatedAt = fileCreatedAt,
fileModifiedAt = fileModifiedAt,
fileName = fileName,
fileSize = outputFile.length(),
remoteFolder = navArgs.folderId,
type = UploadFile.Type.UPLOAD.name,
userId = AccountUtils.currentUserId,
).store()
successCount++
currentImportFile = null
}
}
val outputFile = getOutputFile(uri, fileModifiedAt)
ensureActive()
UploadFile(
uri = outputFile.toUri().toString(),
driveId = navArgs.driveId,
fileCreatedAt = fileCreatedAt,
fileModifiedAt = fileModifiedAt,
fileName = fileName,
fileSize = outputFile.length(),
remoteFolder = navArgs.folderId,
type = UploadFile.Type.UPLOAD.name,
userId = AccountUtils.currentUserId,
).store()
successCount++
currentImportFile = null
}
}
}
Expand All @@ -159,4 +157,6 @@ class ImportFilesDialog : DialogFragment() {
}
}
}

private class NotEnoughRamException : Exception("Low device memory.")
}

0 comments on commit 9de2d2b

Please sign in to comment.