Horis 2 ani în urmă
părinte
comite
bf6ef0ac92

+ 9 - 0
app/src/main/java/io/legado/app/base/BaseViewModel.kt

@@ -27,6 +27,15 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
         return Coroutine.async(scope, context, start, executeContext, block)
         return Coroutine.async(scope, context, start, executeContext, block)
     }
     }
 
 
+    fun <T> executeLazy(
+        scope: CoroutineScope = viewModelScope,
+        context: CoroutineContext = Dispatchers.IO,
+        executeContext: CoroutineContext = Dispatchers.Main,
+        block: suspend CoroutineScope.() -> T
+    ): Coroutine<T> {
+        return Coroutine.async(scope, context, CoroutineStart.LAZY, executeContext, block)
+    }
+
     fun <R> submit(
     fun <R> submit(
         scope: CoroutineScope = viewModelScope,
         scope: CoroutineScope = viewModelScope,
         context: CoroutineContext = Dispatchers.IO,
         context: CoroutineContext = Dispatchers.IO,

+ 5 - 1
app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookActivity.kt

@@ -170,7 +170,11 @@ class RemoteBookActivity : BaseImportBookActivity<RemoteBookViewModel>(),
 
 
     private fun upPath() {
     private fun upPath() {
         binding.tvGoBack.isEnabled = viewModel.dirList.isNotEmpty()
         binding.tvGoBack.isEnabled = viewModel.dirList.isNotEmpty()
-        var path = "books" + File.separator
+        var path = if (viewModel.isDefaultWebdav) {
+            "books" + File.separator
+        } else {
+            File.separator
+        }
         viewModel.dirList.forEach {
         viewModel.dirList.forEach {
             path = path + it.filename + File.separator
             path = path + it.filename + File.separator
         }
         }

+ 5 - 2
app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookViewModel.kt

@@ -90,14 +90,17 @@ class RemoteBookViewModel(application: Application) : BaseViewModel(application)
     }.flowOn(Dispatchers.IO)
     }.flowOn(Dispatchers.IO)
 
 
     private var remoteBookWebDav: RemoteBookWebDav? = null
     private var remoteBookWebDav: RemoteBookWebDav? = null
+    var isDefaultWebdav = false
 
 
     fun initData(onSuccess: () -> Unit) {
     fun initData(onSuccess: () -> Unit) {
         execute {
         execute {
+            isDefaultWebdav = false
             appDb.serverDao.get(AppConfig.remoteServerId)?.getWebDavConfig()?.let {
             appDb.serverDao.get(AppConfig.remoteServerId)?.getWebDavConfig()?.let {
                 val authorization = Authorization(it)
                 val authorization = Authorization(it)
                 remoteBookWebDav = RemoteBookWebDav(it.url, authorization, AppConfig.remoteServerId)
                 remoteBookWebDav = RemoteBookWebDav(it.url, authorization, AppConfig.remoteServerId)
                 return@execute
                 return@execute
             }
             }
+            isDefaultWebdav = true
             remoteBookWebDav = AppWebDav.defaultBookWebDav
             remoteBookWebDav = AppWebDav.defaultBookWebDav
                 ?: throw NoStackTraceException("webDav没有配置")
                 ?: throw NoStackTraceException("webDav没有配置")
         }.onError {
         }.onError {
@@ -108,7 +111,7 @@ class RemoteBookViewModel(application: Application) : BaseViewModel(application)
     }
     }
 
 
     fun loadRemoteBookList(path: String?, loadCallback: (loading: Boolean) -> Unit) {
     fun loadRemoteBookList(path: String?, loadCallback: (loading: Boolean) -> Unit) {
-        execute {
+        executeLazy {
             val bookWebDav = remoteBookWebDav
             val bookWebDav = remoteBookWebDav
                 ?: throw NoStackTraceException("没有配置webDav")
                 ?: throw NoStackTraceException("没有配置webDav")
             dataCallback?.clear()
             dataCallback?.clear()
@@ -122,7 +125,7 @@ class RemoteBookViewModel(application: Application) : BaseViewModel(application)
             loadCallback.invoke(true)
             loadCallback.invoke(true)
         }.onFinally {
         }.onFinally {
             loadCallback.invoke(false)
             loadCallback.invoke(false)
-        }
+        }.start()
     }
     }
 
 
     fun addToBookshelf(remoteBooks: HashSet<RemoteBook>, finally: () -> Unit) {
     fun addToBookshelf(remoteBooks: HashSet<RemoteBook>, finally: () -> Unit) {