fix lifecycle issue

This commit is contained in:
Bnyro 2022-09-18 16:56:34 +02:00
parent 60f2741b7d
commit 07a4838e6f
3 changed files with 20 additions and 17 deletions

View File

@ -1,10 +1,8 @@
package com.github.libretube.dialogs package com.github.libretube.dialogs
import android.app.Dialog import android.app.Dialog
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
@ -13,25 +11,15 @@ import com.github.libretube.databinding.DialogBackupBinding
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import com.github.libretube.util.BackupHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
class BackupDialog() : DialogFragment() { class BackupDialog(
private val createBackupFile: (BackupFile) -> Unit
) : DialogFragment() {
private lateinit var binding: DialogBackupBinding private lateinit var binding: DialogBackupBinding
private lateinit var createBackupFile: ActivityResultLauncher<String>
val backupFile = BackupFile() val backupFile = BackupFile()
override fun onCreate(savedInstanceState: Bundle?) {
createBackupFile = registerForActivityResult(
ActivityResultContracts.CreateDocument("application/json")
) { uri: Uri? ->
BackupHelper(requireContext()).advancedBackup(uri, backupFile)
}
super.onCreate(savedInstanceState)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val backupOptions = listOf( val backupOptions = listOf(
R.string.watch_history, R.string.watch_history,
@ -77,7 +65,7 @@ class BackupDialog() : DialogFragment() {
} }
}.await() }.await()
createBackupFile.launch("libretube_backup.json") createBackupFile(backupFile)
} }
.create() .create()
} }

View File

@ -11,6 +11,7 @@ import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity import com.github.libretube.activities.SettingsActivity
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.dialogs.BackupDialog import com.github.libretube.dialogs.BackupDialog
import com.github.libretube.obj.BackupFile
import com.github.libretube.util.BackupHelper import com.github.libretube.util.BackupHelper
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -25,6 +26,8 @@ class AdvancedSettings : MaterialPreferenceFragment() {
// backup and restore database // backup and restore database
private lateinit var getBackupFile: ActivityResultLauncher<String> private lateinit var getBackupFile: ActivityResultLauncher<String>
private lateinit var createBackupFile: ActivityResultLauncher<String>
private var backupFile = BackupFile()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
getPrefFile = getPrefFile =
@ -45,6 +48,13 @@ class AdvancedSettings : MaterialPreferenceFragment() {
) { uri: Uri? -> ) { uri: Uri? ->
BackupHelper(requireContext()).restoreAdvancedBackup(uri) BackupHelper(requireContext()).restoreAdvancedBackup(uri)
} }
createBackupFile = registerForActivityResult(
CreateDocument("application/json")
) { uri: Uri? ->
BackupHelper(requireContext()).advancedBackup(uri, backupFile)
}
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
@ -83,7 +93,10 @@ class AdvancedSettings : MaterialPreferenceFragment() {
val advancesBackup = findPreference<Preference>("backup") val advancesBackup = findPreference<Preference>("backup")
advancesBackup?.setOnPreferenceClickListener { advancesBackup?.setOnPreferenceClickListener {
BackupDialog() BackupDialog {
backupFile = it
createBackupFile.launch("backup.json")
}
.show(childFragmentManager, null) .show(childFragmentManager, null)
true true
} }

View File

@ -2,10 +2,12 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.util.Log
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import java.io.FileInputStream import java.io.FileInputStream