[Backup] Code Cleanup

This commit is contained in:
Prathamesh More 2021-11-27 14:05:31 +05:30
parent d6bc78619b
commit 1fdd537253

View file

@ -23,28 +23,32 @@ object BackupHelper {
zipItems.addAll(getDatabaseZipItems(context)) zipItems.addAll(getDatabaseZipItems(context))
zipItems.addAll(getSettingsZipItems(context)) zipItems.addAll(getSettingsZipItems(context))
getUserImageZipItems(context)?.let { zipItems.addAll(it) } getUserImageZipItems(context)?.let { zipItems.addAll(it) }
withContext(Dispatchers.IO) { zipAll(zipItems, backupFile)
zipAll(zipItems, backupFile)
}
} }
private suspend fun zipAll(zipItems: List<ZipItem>, backupFile: File) { private suspend fun zipAll(zipItems: List<ZipItem>, backupFile: File) {
try { withContext(Dispatchers.IO) {
ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out -> kotlin.runCatching {
for (zipItem in zipItems) { ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out ->
FileInputStream(zipItem.filePath).use { fi -> for (zipItem in zipItems) {
BufferedInputStream(fi).use { origin -> FileInputStream(zipItem.filePath).use { fi ->
val entry = ZipEntry(zipItem.zipPath) BufferedInputStream(fi).use { origin ->
out.putNextEntry(entry) val entry = ZipEntry(zipItem.zipPath)
origin.copyTo(out) 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) { 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() .show()
} }
} }