[Backup & Restore] Code Cleanup
This commit is contained in:
parent
ab5015d4b1
commit
cbf410f0cd
3 changed files with 23 additions and 12 deletions
|
@ -47,7 +47,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
else
|
||||
backupAdapter?.swapDataset(listOf())
|
||||
}
|
||||
backupViewModel.loadBackups()
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
val openFilePicker = registerForActivityResult(ActivityResultContracts.OpenDocument()) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
it?.let {
|
||||
|
@ -95,11 +95,11 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
MaterialDialog(requireContext()).show {
|
||||
cornerRadius(res = R.dimen.m3_card_corner_radius)
|
||||
title(res = R.string.action_rename)
|
||||
input(prefill = System.currentTimeMillis().toString()) { _, text ->
|
||||
input(prefill = BackupHelper.getTimeStamp()) { _, text ->
|
||||
// Text submitted with the action button
|
||||
lifecycleScope.launch {
|
||||
BackupHelper.createBackup(requireContext(), text.sanitize())
|
||||
backupViewModel.loadBackups()
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
}
|
||||
}
|
||||
positiveButton(android.R.string.ok)
|
||||
|
@ -129,7 +129,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
backupViewModel.loadBackups()
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
return true
|
||||
}
|
||||
R.id.action_share -> {
|
||||
|
@ -147,10 +147,10 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
input(prefill = file.nameWithoutExtension) { _, text ->
|
||||
// Text submitted with the action button
|
||||
val renamedFile =
|
||||
File(file.parent + File.separator + text + BackupHelper.APPEND_EXTENSION)
|
||||
File(file.parent, "$text${BackupHelper.APPEND_EXTENSION}")
|
||||
if (!renamedFile.exists()) {
|
||||
file.renameTo(renamedFile)
|
||||
backupViewModel.loadBackups()
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
} else {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package code.name.monkey.retromusic.fragments.backup
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
|
@ -19,8 +20,8 @@ class BackupViewModel : ViewModel() {
|
|||
private val backupsMutableLiveData = MutableLiveData<List<File>>()
|
||||
val backupsLiveData: LiveData<List<File>> = backupsMutableLiveData
|
||||
|
||||
fun loadBackups() {
|
||||
File(BackupHelper.backupRootPath).listFiles { _, name ->
|
||||
fun loadBackups(context: Context) {
|
||||
BackupHelper.getBackupRoot(context).listFiles { _, name ->
|
||||
return@listFiles name.endsWith(BackupHelper.BACKUP_EXTENSION)
|
||||
}?.toList()?.let {
|
||||
backupsMutableLiveData.value = it
|
||||
|
|
|
@ -16,6 +16,8 @@ import kotlinx.coroutines.withContext
|
|||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import java.io.*
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipInputStream
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
@ -26,7 +28,7 @@ object BackupHelper : KoinComponent {
|
|||
|
||||
suspend fun createBackup(context: Context, name: String) {
|
||||
val backupFile =
|
||||
File(backupRootPath.child(name) + APPEND_EXTENSION)
|
||||
File(getBackupRoot(context), name + APPEND_EXTENSION)
|
||||
if (backupFile.parentFile?.exists() != true) {
|
||||
backupFile.parentFile?.mkdirs()
|
||||
}
|
||||
|
@ -255,9 +257,13 @@ object BackupHelper : KoinComponent {
|
|||
}
|
||||
}
|
||||
|
||||
val backupRootPath =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
|
||||
.toString() + "/RetroMusic/Backups/"
|
||||
fun getBackupRoot(context: Context): File {
|
||||
return File(
|
||||
context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
|
||||
"RetroMusic/Backups"
|
||||
)
|
||||
}
|
||||
|
||||
const val BACKUP_EXTENSION = "rmbak"
|
||||
const val APPEND_EXTENSION = ".$BACKUP_EXTENSION"
|
||||
private const val PLAYLISTS_PATH = "Playlists"
|
||||
|
@ -293,6 +299,10 @@ object BackupHelper : KoinComponent {
|
|||
private fun ZipEntry.getFileName(): String {
|
||||
return name.substring(name.lastIndexOf(File.separator) + 1)
|
||||
}
|
||||
|
||||
fun getTimeStamp(): String {
|
||||
return SimpleDateFormat("dd-MMM yyyy HHmmss", Locale.getDefault()).format(Date())
|
||||
}
|
||||
}
|
||||
|
||||
data class ZipItem(val filePath: String, val zipPath: String)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue