From 1fdd537253a3423186bf2602c252abe8dd1b0173 Mon Sep 17 00:00:00 2001 From: Prathamesh More Date: Sat, 27 Nov 2021 14:05:31 +0530 Subject: [PATCH] [Backup] Code Cleanup --- .../monkey/retromusic/helper/BackupHelper.kt | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt b/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt index e72dd9f1..034e367d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt +++ b/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt @@ -23,28 +23,32 @@ object BackupHelper { zipItems.addAll(getDatabaseZipItems(context)) zipItems.addAll(getSettingsZipItems(context)) getUserImageZipItems(context)?.let { zipItems.addAll(it) } - withContext(Dispatchers.IO) { - zipAll(zipItems, backupFile) - } + zipAll(zipItems, backupFile) } private suspend fun zipAll(zipItems: List, backupFile: File) { - try { - ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out -> - for (zipItem in zipItems) { - FileInputStream(zipItem.filePath).use { fi -> - BufferedInputStream(fi).use { origin -> - val entry = ZipEntry(zipItem.zipPath) - out.putNextEntry(entry) - origin.copyTo(out) + withContext(Dispatchers.IO) { + kotlin.runCatching { + ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out -> + for (zipItem in zipItems) { + FileInputStream(zipItem.filePath).use { fi -> + BufferedInputStream(fi).use { origin -> + val entry = ZipEntry(zipItem.zipPath) + out.putNextEntry(entry) + origin.copyTo(out) + } } } } + }.onFailure { + it.printStackTrace() + withContext(Dispatchers.Main) { + Toast.makeText(App.getContext(), "Couldn't create backup", Toast.LENGTH_SHORT) + .show() + } } - } catch (exception: FileNotFoundException) { - exception.printStackTrace() withContext(Dispatchers.Main) { - Toast.makeText(App.getContext(), "Couldn't create backup", Toast.LENGTH_SHORT) + Toast.makeText(App.getContext(), "Backup created successfully", Toast.LENGTH_SHORT) .show() } }