[Backup] Code Cleanup

This commit is contained in:
Prathamesh More 2021-12-30 16:21:16 +05:30
parent 4af4771cfe
commit 48da7dced3
4 changed files with 17 additions and 40 deletions

View file

@ -21,7 +21,7 @@ class BackupAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder( return ViewHolder(
LayoutInflater.from(activity).inflate(R.layout.item_list_card, parent, false) LayoutInflater.from(activity).inflate(R.layout.item_list_backup, parent, false)
) )
} }

View file

@ -29,13 +29,15 @@ class BackupViewModel : ViewModel() {
suspend fun restoreBackup(activity: Activity, inputStream: InputStream?, contents: List<BackupContent>) { suspend fun restoreBackup(activity: Activity, inputStream: InputStream?, contents: List<BackupContent>) {
BackupHelper.restoreBackup(activity, inputStream, contents) BackupHelper.restoreBackup(activity, inputStream, contents)
withContext(Dispatchers.Main) { if (contents.contains(BackupContent.SETTINGS)) {
val intent = Intent( withContext(Dispatchers.Main) {
activity, val intent = Intent(
MainActivity::class.java activity,
) MainActivity::class.java
activity.startActivity(intent) )
exitProcess(0) activity.startActivity(intent)
exitProcess(0)
}
} }
} }
} }

View file

@ -144,7 +144,6 @@ object BackupHelper {
restorePreferences(context, it, entry) restorePreferences(context, it, entry)
} else if (entry.isImageEntry() && contents.contains(USER_IMAGES)) { } else if (entry.isImageEntry() && contents.contains(USER_IMAGES)) {
restoreImages(context, it, entry) restoreImages(context, it, entry)
} else if (entry.isCustomArtistImageEntry() && contents.contains( } else if (entry.isCustomArtistImageEntry() && contents.contains(
CUSTOM_ARTIST_IMAGES CUSTOM_ARTIST_IMAGES
) )
@ -168,11 +167,7 @@ object BackupHelper {
val filePath = val filePath =
context.filesDir.path + File.separator + zipEntry.getFileName() context.filesDir.path + File.separator + zipEntry.getFileName()
BufferedOutputStream(FileOutputStream(filePath)).use { bos -> BufferedOutputStream(FileOutputStream(filePath)).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) zipIn.copyTo(bos)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
} }
} }
@ -184,11 +179,7 @@ object BackupHelper {
file.delete() file.delete()
} }
BufferedOutputStream(FileOutputStream(file)).use { bos -> BufferedOutputStream(FileOutputStream(file)).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) zipIn.copyTo(bos)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
} }
} }
@ -196,11 +187,7 @@ object BackupHelper {
val filePath = val filePath =
context.filesDir.parent!! + File.separator + DATABASES_PATH + File.separator + zipEntry.getFileName() context.filesDir.parent!! + File.separator + DATABASES_PATH + File.separator + zipEntry.getFileName()
BufferedOutputStream(FileOutputStream(filePath)).use { bos -> BufferedOutputStream(FileOutputStream(filePath)).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) zipIn.copyTo(bos)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
} }
} }
@ -211,11 +198,7 @@ object BackupHelper {
val filePath = val filePath =
context.filesDir.parent!! + File.separator + DATABASES_PATH + File.separator + zipEntry.getFileName() context.filesDir.parent!! + File.separator + DATABASES_PATH + File.separator + zipEntry.getFileName()
BufferedOutputStream(FileOutputStream(filePath)).use { bos -> BufferedOutputStream(FileOutputStream(filePath)).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) zipIn.copyTo(bos)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
} }
} }
@ -237,11 +220,7 @@ object BackupHelper {
) )
) )
).use { bos -> ).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) zipIn.copyTo(bos)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
} }
} }
@ -253,11 +232,7 @@ object BackupHelper {
val filePath = val filePath =
context.filesDir.parentFile?.absolutePath + "/shared_prefs/" + zipEntry.getFileName() context.filesDir.parentFile?.absolutePath + "/shared_prefs/" + zipEntry.getFileName()
BufferedOutputStream(FileOutputStream(filePath)).use { bos -> BufferedOutputStream(FileOutputStream(filePath)).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) zipIn.copyTo(bos)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
} }
} }

View file

@ -24,9 +24,9 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/title" app:layout_constraintEnd_toStartOf="@id/title"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/ic_restore"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView <com.google.android.material.textview.MaterialTextView
android:id="@+id/title" android:id="@+id/title"
android:layout_width="0dp" android:layout_width="0dp"