|
|
|
|
@ -1,12 +1,15 @@
|
|
|
|
|
package org.koitharu.kotatsu.settings.backup
|
|
|
|
|
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.View
|
|
|
|
|
import androidx.activity.result.ActivityResultCallback
|
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
|
import androidx.documentfile.provider.DocumentFile
|
|
|
|
|
import androidx.preference.Preference
|
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
@ -14,12 +17,19 @@ import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.core.backup.DIR_BACKUPS
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
|
import org.koitharu.kotatsu.core.ui.BasePreferenceFragment
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.resolveFile
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.tryLaunch
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.viewLifecycleScope
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
@AndroidEntryPoint
|
|
|
|
|
class PeriodicalBackupSettingsFragment : BasePreferenceFragment(R.string.periodic_backups),
|
|
|
|
|
ActivityResultCallback<Uri?>, SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
|
ActivityResultCallback<Uri?> {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
lateinit var scheduler: PeriodicalBackupWorker.Scheduler
|
|
|
|
|
|
|
|
|
|
private val outputSelectCall = registerForActivityResult(
|
|
|
|
|
ActivityResultContracts.OpenDocumentTree(),
|
|
|
|
|
@ -32,8 +42,8 @@ class PeriodicalBackupSettingsFragment : BasePreferenceFragment(R.string.periodi
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
settings.subscribe(this)
|
|
|
|
|
bindOutputSummary()
|
|
|
|
|
bindLastBackupInfo()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
|
|
|
|
@ -43,20 +53,12 @@ class PeriodicalBackupSettingsFragment : BasePreferenceFragment(R.string.periodi
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
|
|
|
|
settings.unsubscribe(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onActivityResult(result: Uri?) {
|
|
|
|
|
if (result != null) {
|
|
|
|
|
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
|
|
|
|
context?.contentResolver?.takePersistableUriPermission(result, takeFlags)
|
|
|
|
|
settings.periodicalBackupOutput = result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
|
|
|
|
when (key) {
|
|
|
|
|
AppSettings.KEY_BACKUP_PERIODICAL_OUTPUT -> bindOutputSummary()
|
|
|
|
|
bindOutputSummary()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -65,10 +67,32 @@ class PeriodicalBackupSettingsFragment : BasePreferenceFragment(R.string.periodi
|
|
|
|
|
viewLifecycleScope.launch {
|
|
|
|
|
preference.summary = withContext(Dispatchers.Default) {
|
|
|
|
|
val value = settings.periodicalBackupOutput
|
|
|
|
|
value?.toString() ?: preference.context.run {
|
|
|
|
|
value?.toUserFriendlyString(preference.context) ?: preference.context.run {
|
|
|
|
|
getExternalFilesDir(DIR_BACKUPS) ?: File(filesDir, DIR_BACKUPS)
|
|
|
|
|
}.path
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun bindLastBackupInfo() {
|
|
|
|
|
val preference = findPreference<Preference>(AppSettings.KEY_BACKUP_PERIODICAL_LAST) ?: return
|
|
|
|
|
viewLifecycleScope.launch {
|
|
|
|
|
val lastDate = withContext(Dispatchers.Default) {
|
|
|
|
|
scheduler.getLastSuccessfulBackup()
|
|
|
|
|
}
|
|
|
|
|
preference.summary = lastDate?.let {
|
|
|
|
|
val format = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM, SimpleDateFormat.SHORT)
|
|
|
|
|
preference.context.getString(R.string.last_successful_backup, format.format(it))
|
|
|
|
|
}
|
|
|
|
|
preference.isVisible = lastDate != null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun Uri.toUserFriendlyString(context: Context): String {
|
|
|
|
|
val df = DocumentFile.fromTreeUri(context, this)
|
|
|
|
|
if (df?.canWrite() != true) {
|
|
|
|
|
return context.getString(R.string.invalid_value_message)
|
|
|
|
|
}
|
|
|
|
|
return resolveFile(context)?.path ?: toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|