Skip to content

Commit

Permalink
Improve RAM error snackbar during file import
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyDL-Infomaniak committed May 23, 2024
1 parent 2fbd6d4 commit 3f9c57a
Showing 1 changed file with 12 additions and 3 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,7 @@ class ImportFilesDialog : DialogFragment() {
val errorCount = importCount - successCount

if (errorCount > 0) {
val errorMessage = if (isLowMemory()) {
val errorMessage = if (isMemoryError) {
getString(R.string.uploadOutOfMemoryError)
} else {
resources.getQuantityString(R.plurals.snackBarUploadError, errorCount, errorCount)
Expand All @@ -95,7 +97,12 @@ class ImportFilesDialog : DialogFragment() {
initUpload(uri)
}.onFailure { exception ->
exception.printStackTrace()
Sentry.captureException(exception)

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

errorCount++
}
Expand All @@ -109,7 +116,7 @@ class ImportFilesDialog : DialogFragment() {

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

if (cursor.moveToFirst()) {
val fileName = cursor.getFileName(uri)
Expand Down Expand Up @@ -150,4 +157,6 @@ class ImportFilesDialog : DialogFragment() {
}
}
}

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

0 comments on commit 3f9c57a

Please sign in to comment.