|
|
|
@ -1,6 +1,5 @@
|
|
|
|
package org.koitharu.kotatsu.settings.backup
|
|
|
|
package org.koitharu.kotatsu.settings.backup
|
|
|
|
|
|
|
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.view.ViewGroup
|
|
|
|
@ -19,20 +18,17 @@ import org.koitharu.kotatsu.core.util.ext.tryLaunch
|
|
|
|
import org.koitharu.kotatsu.core.util.progress.Progress
|
|
|
|
import org.koitharu.kotatsu.core.util.progress.Progress
|
|
|
|
import org.koitharu.kotatsu.databinding.DialogProgressBinding
|
|
|
|
import org.koitharu.kotatsu.databinding.DialogProgressBinding
|
|
|
|
import java.io.File
|
|
|
|
import java.io.File
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@AndroidEntryPoint
|
|
|
|
@AndroidEntryPoint
|
|
|
|
class BackupDialogFragment : AlertDialogFragment<DialogProgressBinding>() {
|
|
|
|
class BackupDialogFragment : AlertDialogFragment<DialogProgressBinding>() {
|
|
|
|
|
|
|
|
|
|
|
|
private val viewModel by viewModels<BackupViewModel>()
|
|
|
|
private val viewModel by viewModels<BackupViewModel>()
|
|
|
|
|
|
|
|
|
|
|
|
private var backup: File? = null
|
|
|
|
|
|
|
|
private val saveFileContract = registerForActivityResult(
|
|
|
|
private val saveFileContract = registerForActivityResult(
|
|
|
|
ActivityResultContracts.CreateDocument("application/zip"),
|
|
|
|
ActivityResultContracts.CreateDocument("application/zip"),
|
|
|
|
) { uri ->
|
|
|
|
) { uri ->
|
|
|
|
val file = backup
|
|
|
|
if (uri != null) {
|
|
|
|
if (uri != null && file != null) {
|
|
|
|
viewModel.saveBackup(uri)
|
|
|
|
saveBackup(file, uri)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
dismiss()
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -51,6 +47,7 @@ class BackupDialogFragment : AlertDialogFragment<DialogProgressBinding>() {
|
|
|
|
viewModel.progress.observe(viewLifecycleOwner, this::onProgressChanged)
|
|
|
|
viewModel.progress.observe(viewLifecycleOwner, this::onProgressChanged)
|
|
|
|
viewModel.onBackupDone.observeEvent(viewLifecycleOwner, this::onBackupDone)
|
|
|
|
viewModel.onBackupDone.observeEvent(viewLifecycleOwner, this::onBackupDone)
|
|
|
|
viewModel.onError.observeEvent(viewLifecycleOwner, this::onError)
|
|
|
|
viewModel.onError.observeEvent(viewLifecycleOwner, this::onError)
|
|
|
|
|
|
|
|
viewModel.onBackupSaved.observeEvent(viewLifecycleOwner) { onBackupSaved() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onBuildDialog(builder: MaterialAlertDialogBuilder): MaterialAlertDialogBuilder {
|
|
|
|
override fun onBuildDialog(builder: MaterialAlertDialogBuilder): MaterialAlertDialogBuilder {
|
|
|
|
@ -81,26 +78,14 @@ class BackupDialogFragment : AlertDialogFragment<DialogProgressBinding>() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun onBackupDone(file: File) {
|
|
|
|
private fun onBackupDone(file: File) {
|
|
|
|
this.backup = file
|
|
|
|
|
|
|
|
if (!saveFileContract.tryLaunch(file.name)) {
|
|
|
|
if (!saveFileContract.tryLaunch(file.name)) {
|
|
|
|
Toast.makeText(requireContext(), R.string.operation_not_supported, Toast.LENGTH_SHORT).show()
|
|
|
|
Toast.makeText(requireContext(), R.string.operation_not_supported, Toast.LENGTH_SHORT).show()
|
|
|
|
dismiss()
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun saveBackup(file: File, output: Uri) {
|
|
|
|
private fun onBackupSaved() {
|
|
|
|
try {
|
|
|
|
|
|
|
|
requireContext().contentResolver.openFileDescriptor(output, "w")?.use { fd ->
|
|
|
|
|
|
|
|
FileOutputStream(fd.fileDescriptor).use {
|
|
|
|
|
|
|
|
it.write(file.readBytes())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Toast.makeText(requireContext(), R.string.backup_saved, Toast.LENGTH_SHORT).show()
|
|
|
|
Toast.makeText(requireContext(), R.string.backup_saved, Toast.LENGTH_SHORT).show()
|
|
|
|
dismiss()
|
|
|
|
dismiss()
|
|
|
|
} catch (e: InterruptedException) {
|
|
|
|
|
|
|
|
throw e
|
|
|
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
|
|
|
onError(e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|