kunfei 3 жил өмнө
parent
commit
b3db9e7946

+ 3 - 47
app/src/main/java/io/legado/app/help/AppWebDav.kt

@@ -1,8 +1,6 @@
 package io.legado.app.help
 package io.legado.app.help
 
 
-import android.content.Context
 import android.net.Uri
 import android.net.Uri
-import io.legado.app.R
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.PreferKey
 import io.legado.app.constant.PreferKey
 import io.legado.app.data.appDb
 import io.legado.app.data.appDb
@@ -13,24 +11,17 @@ import io.legado.app.help.config.AppConfig
 import io.legado.app.help.coroutine.Coroutine
 import io.legado.app.help.coroutine.Coroutine
 import io.legado.app.help.storage.Backup
 import io.legado.app.help.storage.Backup
 import io.legado.app.help.storage.Restore
 import io.legado.app.help.storage.Restore
-import io.legado.app.lib.dialogs.selector
 import io.legado.app.lib.webdav.Authorization
 import io.legado.app.lib.webdav.Authorization
 import io.legado.app.lib.webdav.WebDav
 import io.legado.app.lib.webdav.WebDav
 import io.legado.app.lib.webdav.WebDavException
 import io.legado.app.lib.webdav.WebDavException
 import io.legado.app.lib.webdav.WebDavFile
 import io.legado.app.lib.webdav.WebDavFile
 import io.legado.app.model.remote.RemoteBookWebDav
 import io.legado.app.model.remote.RemoteBookWebDav
-import io.legado.app.ui.widget.dialog.WaitDialog
 import io.legado.app.utils.*
 import io.legado.app.utils.*
 import io.legado.app.utils.compress.ZipUtils
 import io.legado.app.utils.compress.ZipUtils
-import kotlinx.coroutines.Dispatchers.IO
-import kotlinx.coroutines.Dispatchers.Main
-import kotlinx.coroutines.ensureActive
 import kotlinx.coroutines.runBlocking
 import kotlinx.coroutines.runBlocking
-import kotlinx.coroutines.withContext
 import splitties.init.appCtx
 import splitties.init.appCtx
 import java.io.File
 import java.io.File
 import java.util.*
 import java.util.*
-import kotlin.coroutines.coroutineContext
 
 
 /**
 /**
  * webDav初始化会访问网络,不要放到主线程
  * webDav初始化会访问网络,不要放到主线程
@@ -47,7 +38,7 @@ object AppWebDav {
 
 
     val isOk get() = authorization != null
     val isOk get() = authorization != null
 
 
-    private val isJianGuoYun get() = rootWebDavUrl.startsWith(defaultWebDavUrl, true)
+    val isJianGuoYun get() = rootWebDavUrl.startsWith(defaultWebDavUrl, true)
 
 
     init {
     init {
         runBlocking {
         runBlocking {
@@ -55,7 +46,7 @@ object AppWebDav {
         }
         }
     }
     }
 
 
-    val rootWebDavUrl: String
+    private val rootWebDavUrl: String
         get() {
         get() {
             val configUrl = appCtx.getPrefString(PreferKey.webDavUrl)?.trim()
             val configUrl = appCtx.getPrefString(PreferKey.webDavUrl)?.trim()
             var url = if (configUrl.isNullOrEmpty()) defaultWebDavUrl else configUrl
             var url = if (configUrl.isNullOrEmpty()) defaultWebDavUrl else configUrl
@@ -86,7 +77,7 @@ object AppWebDav {
     }
     }
 
 
     @Throws(Exception::class)
     @Throws(Exception::class)
-    private suspend fun getBackupNames(): ArrayList<String> {
+    suspend fun getBackupNames(): ArrayList<String> {
         val names = arrayListOf<String>()
         val names = arrayListOf<String>()
         authorization?.let {
         authorization?.let {
             var files = WebDav(rootWebDavUrl, it).listFiles()
             var files = WebDav(rootWebDavUrl, it).listFiles()
@@ -101,41 +92,6 @@ object AppWebDav {
         return names
         return names
     }
     }
 
 
-    suspend fun showRestoreDialog(context: Context) {
-        val names = withContext(IO) { getBackupNames() }
-        if (isJianGuoYun && names.size > 700) {
-            context.toastOnUi("由于坚果云限制,部分备份可能未显示")
-        }
-        if (names.isNotEmpty()) {
-            coroutineContext.ensureActive()
-            withContext(Main) {
-                context.selector(
-                    title = context.getString(R.string.select_restore_file),
-                    items = names
-                ) { _, index ->
-                    if (index in 0 until names.size) {
-                        val waitDialog = WaitDialog(context)
-                        waitDialog.setText("恢复中…")
-                        waitDialog.show()
-                        val task = Coroutine.async {
-                            restoreWebDav(names[index])
-                        }.onError {
-                            AppLog.put("WebDav恢复出错\n${it.localizedMessage}", it)
-                            appCtx.toastOnUi("WebDav恢复出错\n${it.localizedMessage}")
-                        }.onFinally(Main) {
-                            waitDialog.dismiss()
-                        }
-                        waitDialog.setOnCancelListener {
-                            task.cancel()
-                        }
-                    }
-                }
-            }
-        } else {
-            throw NoStackTraceException("Web dav no back up file")
-        }
-    }
-
     @Throws(WebDavException::class)
     @Throws(WebDavException::class)
     suspend fun restoreWebDav(name: String) {
     suspend fun restoreWebDav(name: String) {
         authorization?.let {
         authorization?.let {

+ 53 - 1
app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt

@@ -1,5 +1,6 @@
 package io.legado.app.ui.config
 package io.legado.app.ui.config
 
 
+import android.content.Context
 import android.content.SharedPreferences
 import android.content.SharedPreferences
 import android.net.Uri
 import android.net.Uri
 import android.os.Bundle
 import android.os.Bundle
@@ -17,6 +18,7 @@ import androidx.preference.Preference
 import io.legado.app.R
 import io.legado.app.R
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.PreferKey
 import io.legado.app.constant.PreferKey
+import io.legado.app.exception.NoStackTraceException
 import io.legado.app.help.AppWebDav
 import io.legado.app.help.AppWebDav
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.config.LocalConfig
 import io.legado.app.help.config.LocalConfig
@@ -26,6 +28,7 @@ import io.legado.app.help.storage.BackupConfig
 import io.legado.app.help.storage.ImportOldData
 import io.legado.app.help.storage.ImportOldData
 import io.legado.app.help.storage.Restore
 import io.legado.app.help.storage.Restore
 import io.legado.app.lib.dialogs.alert
 import io.legado.app.lib.dialogs.alert
+import io.legado.app.lib.dialogs.selector
 import io.legado.app.lib.permission.Permissions
 import io.legado.app.lib.permission.Permissions
 import io.legado.app.lib.permission.PermissionsCompat
 import io.legado.app.lib.permission.PermissionsCompat
 import io.legado.app.lib.prefs.fragment.PreferenceFragment
 import io.legado.app.lib.prefs.fragment.PreferenceFragment
@@ -35,10 +38,14 @@ import io.legado.app.ui.document.HandleFileContract
 import io.legado.app.ui.widget.dialog.TextDialog
 import io.legado.app.ui.widget.dialog.TextDialog
 import io.legado.app.ui.widget.dialog.WaitDialog
 import io.legado.app.ui.widget.dialog.WaitDialog
 import io.legado.app.utils.*
 import io.legado.app.utils.*
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.Dispatchers.Main
 import kotlinx.coroutines.Dispatchers.Main
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.Job
+import kotlinx.coroutines.ensureActive
+import kotlinx.coroutines.withContext
 import splitties.init.appCtx
 import splitties.init.appCtx
 import kotlin.collections.set
 import kotlin.collections.set
+import kotlin.coroutines.coroutineContext
 
 
 class BackupConfigFragment : PreferenceFragment(),
 class BackupConfigFragment : PreferenceFragment(),
     SharedPreferences.OnSharedPreferenceChangeListener,
     SharedPreferences.OnSharedPreferenceChangeListener,
@@ -319,7 +326,7 @@ class BackupConfigFragment : PreferenceFragment(),
         waitDialog.show()
         waitDialog.show()
         Coroutine.async {
         Coroutine.async {
             restoreJob = coroutineContext[Job]
             restoreJob = coroutineContext[Job]
-            AppWebDav.showRestoreDialog(requireContext())
+            showRestoreDialog(requireContext())
         }.onError {
         }.onError {
             AppLog.put("恢复备份出错WebDavError\n${it.localizedMessage}", it)
             AppLog.put("恢复备份出错WebDavError\n${it.localizedMessage}", it)
             alert {
             alert {
@@ -335,6 +342,46 @@ class BackupConfigFragment : PreferenceFragment(),
         }
         }
     }
     }
 
 
+    private suspend fun showRestoreDialog(context: Context) {
+        val names = withContext(Dispatchers.IO) { AppWebDav.getBackupNames() }
+        if (AppWebDav.isJianGuoYun && names.size > 700) {
+            context.toastOnUi("由于坚果云限制,部分备份可能未显示")
+        }
+        if (names.isNotEmpty()) {
+            coroutineContext.ensureActive()
+            withContext(Main) {
+                context.selector(
+                    title = context.getString(R.string.select_restore_file),
+                    items = names
+                ) { _, index ->
+                    if (index in 0 until names.size) {
+                        listView.post {
+                            restoreWebDav(names[index])
+                        }
+                    }
+                }
+            }
+        } else {
+            throw NoStackTraceException("Web dav no back up file")
+        }
+    }
+
+    private fun restoreWebDav(name: String) {
+        waitDialog.setText("恢复中…")
+        waitDialog.show()
+        val task = Coroutine.async {
+            AppWebDav.restoreWebDav(name)
+        }.onError {
+            AppLog.put("WebDav恢复出错\n${it.localizedMessage}", it)
+            appCtx.toastOnUi("WebDav恢复出错\n${it.localizedMessage}")
+        }.onFinally(Main) {
+            waitDialog.dismiss()
+        }
+        waitDialog.setOnCancelListener {
+            task.cancel()
+        }
+    }
+
     private fun restoreFromLocal() {
     private fun restoreFromLocal() {
         restoreDoc.launch {
         restoreDoc.launch {
             title = getString(R.string.select_restore_file)
             title = getString(R.string.select_restore_file)
@@ -343,4 +390,9 @@ class BackupConfigFragment : PreferenceFragment(),
         }
         }
     }
     }
 
 
+    override fun onDestroyView() {
+        super.onDestroyView()
+        waitDialog.dismiss()
+    }
+
 }
 }