mirror of
https://github.com/cmclark00/RetroMusicPlayer.git
synced 2025-05-19 16:45:20 +01:00
Added ability to restore
This commit is contained in:
parent
897b160834
commit
d703a05182
4 changed files with 120 additions and 8 deletions
|
@ -16,8 +16,9 @@ import com.google.android.material.shape.MaterialShapeDrawable
|
|||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
|
||||
class BackupActivity : AbsThemeActivity() {
|
||||
class BackupActivity : AbsThemeActivity(), BackupAdapter.BackupClickedListener {
|
||||
|
||||
private val backupViewModel by viewModels<BackupViewModel>()
|
||||
private var backupAdapter: BackupAdapter? = null
|
||||
|
@ -59,7 +60,7 @@ class BackupActivity : AbsThemeActivity() {
|
|||
}
|
||||
|
||||
private fun initAdapter() {
|
||||
backupAdapter = BackupAdapter(this@BackupActivity, ArrayList())
|
||||
backupAdapter = BackupAdapter(this@BackupActivity, ArrayList(), this)
|
||||
backupAdapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
||||
override fun onChanged() {
|
||||
super.onChanged()
|
||||
|
@ -82,4 +83,10 @@ class BackupActivity : AbsThemeActivity() {
|
|||
adapter = backupAdapter
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackupClicked(file: File) {
|
||||
lifecycleScope.launch {
|
||||
backupViewModel.restoreBackup(this@BackupActivity, file)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,17 @@
|
|||
package code.name.monkey.retromusic.activities.backup
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import code.name.monkey.retromusic.helper.BackupHelper
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
|
||||
class BackupViewModel : ViewModel() {
|
||||
private val backupsMutableLiveData = MutableLiveData<List<File>>()
|
||||
|
@ -14,10 +20,22 @@ class BackupViewModel : ViewModel() {
|
|||
fun loadBackups() {
|
||||
viewModelScope.launch {
|
||||
File(BackupHelper.backupRootPath).listFiles { _, name ->
|
||||
return@listFiles name.endsWith(BackupHelper.backupExt)
|
||||
return@listFiles name.endsWith(BackupHelper.BACKUP_EXTENSION)
|
||||
}?.toList()?.let {
|
||||
backupsMutableLiveData.value = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun restoreBackup(activity: Activity, file: File) {
|
||||
BackupHelper.restoreBackup(activity, file)
|
||||
withContext(Dispatchers.Main) {
|
||||
val intent = Intent(
|
||||
activity,
|
||||
activity::class.java
|
||||
)
|
||||
activity.startActivity(intent)
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue