[Backup & Restore] Code Cleanup

This commit is contained in:
Prathamesh More 2021-12-31 11:33:05 +05:30
parent 1d77b3155a
commit 7db6186ff8

View file

@ -26,7 +26,7 @@ object BackupHelper : KoinComponent {
suspend fun createBackup(context: Context, name: String) { suspend fun createBackup(context: Context, name: String) {
val backupFile = val backupFile =
File(backupRootPath + File.separator + name + APPEND_EXTENSION) File(backupRootPath.child(name) + APPEND_EXTENSION)
if (backupFile.parentFile?.exists() != true) { if (backupFile.parentFile?.exists() != true) {
backupFile.parentFile?.mkdirs() backupFile.parentFile?.mkdirs()
} }
@ -86,7 +86,7 @@ object BackupHelper : KoinComponent {
playlistZipItems.add( playlistZipItems.add(
ZipItem( ZipItem(
playlistFile.absolutePath, playlistFile.absolutePath,
PLAYLISTS_PATH + File.separator + playlistFile.name PLAYLISTS_PATH.child(playlistFile.name)
) )
) )
} }
@ -96,12 +96,12 @@ object BackupHelper : KoinComponent {
} }
private fun getSettingsZipItems(context: Context): List<ZipItem> { private fun getSettingsZipItems(context: Context): List<ZipItem> {
val sharedPrefPath = context.filesDir.parentFile?.absolutePath + "/shared_prefs/" val sharedPrefPath = File(context.filesDir.parentFile, "shared_prefs")
return listOf( return listOf(
"${BuildConfig.APPLICATION_ID}_preferences.xml", // App settings pref path "${BuildConfig.APPLICATION_ID}_preferences.xml", // App settings pref path
"$THEME_PREFS_KEY_DEFAULT.xml" // appthemehelper pref path "$THEME_PREFS_KEY_DEFAULT.xml" // appthemehelper pref path
).map { ).map {
ZipItem(sharedPrefPath + it, "$SETTINGS_PATH${File.separator}$it") ZipItem(File(sharedPrefPath, it).absolutePath, SETTINGS_PATH.child(it))
} }
} }
@ -109,29 +109,29 @@ object BackupHelper : KoinComponent {
return context.filesDir.listFiles { _, name -> return context.filesDir.listFiles { _, name ->
name.endsWith(".jpg") name.endsWith(".jpg")
}?.map { }?.map {
ZipItem(it.absolutePath, "$IMAGES_PATH${File.separator}${it.name}") ZipItem(it.absolutePath, IMAGES_PATH.child(it.name))
} }
} }
private fun getCustomArtistZipItems(context: Context): List<ZipItem> { private fun getCustomArtistZipItems(context: Context): List<ZipItem> {
val zipItemList = mutableListOf<ZipItem>() val zipItemList = mutableListOf<ZipItem>()
val sharedPrefPath = context.filesDir.parentFile?.absolutePath + "/shared_prefs/" val sharedPrefPath = File(context.filesDir.parentFile, "shared_prefs")
zipItemList.addAll( zipItemList.addAll(
File(context.filesDir, "custom_artist_images") File(context.filesDir, "custom_artist_images")
.listFiles()?.map { .listFiles()?.map {
ZipItem( ZipItem(
it.absolutePath, it.absolutePath,
"$CUSTOM_ARTISTS_PATH${File.separator}custom_artist_images${File.separator}${it.name}" CUSTOM_ARTISTS_PATH.child("custom_artist_images").child(it.name)
) )
}?.toList() ?: listOf() }?.toList() ?: listOf()
) )
File(sharedPrefPath + File.separator + "custom_artist_image.xml").let { File(sharedPrefPath, "custom_artist_image.xml").let {
if (it.exists()) { if (it.exists()) {
zipItemList.add( zipItemList.add(
ZipItem( ZipItem(
it.absolutePath, it.absolutePath,
"$CUSTOM_ARTISTS_PATH${File.separator}prefs${File.separator}custom_artist_image.xml" CUSTOM_ARTISTS_PATH.child("prefs").child("custom_artist_image.xml")
) )
) )
} }
@ -171,9 +171,10 @@ object BackupHelper : KoinComponent {
} }
private fun restoreImages(context: Context, zipIn: ZipInputStream, zipEntry: ZipEntry) { private fun restoreImages(context: Context, zipIn: ZipInputStream, zipEntry: ZipEntry) {
val filePath = val file = File(
context.filesDir.path + File.separator + zipEntry.getFileName() context.filesDir.path, zipEntry.getFileName()
BufferedOutputStream(FileOutputStream(filePath)).use { bos -> )
BufferedOutputStream(FileOutputStream(file)).use { bos ->
zipIn.copyTo(bos) zipIn.copyTo(bos)
} }
} }
@ -247,9 +248,9 @@ object BackupHelper : KoinComponent {
zipIn: ZipInputStream, zipIn: ZipInputStream,
zipEntry: ZipEntry zipEntry: ZipEntry
) { ) {
val filePath = val file =
context.filesDir.parentFile?.absolutePath + "/shared_prefs/" + zipEntry.getFileName() File(context.filesDir.parentFile, "shared_prefs".child(zipEntry.getFileName()))
BufferedOutputStream(FileOutputStream(filePath)).use { bos -> BufferedOutputStream(FileOutputStream(file)).use { bos ->
zipIn.copyTo(bos) zipIn.copyTo(bos)
} }
} }
@ -309,6 +310,10 @@ fun CharSequence.sanitize(): String {
.replace("&", "_") .replace("&", "_")
} }
fun String.child(child: String): String {
return this + File.separator + child
}
enum class BackupContent { enum class BackupContent {
SETTINGS, SETTINGS,
USER_IMAGES, USER_IMAGES,