[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,13 +23,12 @@ object BackupHelper {
zipItems.addAll(getDatabaseZipItems(context))
zipItems.addAll(getSettingsZipItems(context))
getUserImageZipItems(context)?.let { zipItems.addAll(it) }
withContext(Dispatchers.IO) {
zipAll(zipItems, backupFile)
}
}
private suspend fun zipAll(zipItems: List<ZipItem>, backupFile: File) {
try {
withContext(Dispatchers.IO) {
kotlin.runCatching {
ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out ->
for (zipItem in zipItems) {
FileInputStream(zipItem.filePath).use { fi ->
@ -41,13 +40,18 @@ object BackupHelper {
}
}
}
} catch (exception: FileNotFoundException) {
exception.printStackTrace()
}.onFailure {
it.printStackTrace()
withContext(Dispatchers.Main) {
Toast.makeText(App.getContext(), "Couldn't create backup", Toast.LENGTH_SHORT)
.show()
}
}
withContext(Dispatchers.Main) {
Toast.makeText(App.getContext(), "Backup created successfully", Toast.LENGTH_SHORT)
.show()
}
}
}
private fun getDatabaseZipItems(context: Context): List<ZipItem> {