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