From 0a549402214229af68885cbcabab10c1df0531d8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 21 May 2022 13:20:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 6 +- .../app/api/controller/BookController.kt | 2 +- .../app/help/{storage => }/AppWebDav.kt | 11 ++-- .../java/io/legado/app/help/storage/Backup.kt | 1 + .../main/java/io/legado/app/model/ReadBook.kt | 3 +- .../app/ui/book/cache/CacheViewModel.kt | 2 +- .../app/ui/book/read/ReadBookActivity.kt | 2 +- .../app/ui/book/read/ReadBookViewModel.kt | 2 +- .../app/ui/book/remote/RemoteBookViewModel.kt | 16 ++---- .../book/remote/manager/RemoteBookWebDav.kt | 57 +++++++------------ .../app/ui/config/BackupConfigFragment.kt | 6 +- .../legado/app/ui/config/ConfigViewModel.kt | 2 +- .../io/legado/app/ui/main/MainActivity.kt | 2 +- .../io/legado/app/ui/main/MainViewModel.kt | 2 +- 14 files changed, 49 insertions(+), 65 deletions(-) rename app/src/main/java/io/legado/app/help/{storage => }/AppWebDav.kt (96%) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index ec425f5f0..d5387b0eb 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -14,15 +14,11 @@ import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.constant.PreferKey import io.legado.app.data.appDb -import io.legado.app.help.BookHelp -import io.legado.app.help.CrashHandler -import io.legado.app.help.LifecycleHelp -import io.legado.app.help.RuleBigDataHelp +import io.legado.app.help.* import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ThemeConfig.applyDayNight import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.cronet.CronetLoader -import io.legado.app.help.storage.AppWebDav import io.legado.app.model.BookCover import io.legado.app.utils.defaultSharedPreferences import io.legado.app.utils.getPrefBoolean diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index fae581bdf..d9bd46ac9 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -7,11 +7,11 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookProgress import io.legado.app.data.entities.BookSource +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp import io.legado.app.help.CacheManager import io.legado.app.help.ContentProcessor import io.legado.app.help.glide.ImageLoader -import io.legado.app.help.storage.AppWebDav import io.legado.app.model.BookCover import io.legado.app.model.ReadBook import io.legado.app.model.localBook.LocalBook diff --git a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt b/app/src/main/java/io/legado/app/help/AppWebDav.kt similarity index 96% rename from app/src/main/java/io/legado/app/help/storage/AppWebDav.kt rename to app/src/main/java/io/legado/app/help/AppWebDav.kt index d8b38d316..aae5baace 100644 --- a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt +++ b/app/src/main/java/io/legado/app/help/AppWebDav.kt @@ -1,4 +1,4 @@ -package io.legado.app.help.storage +package io.legado.app.help import android.content.Context import io.legado.app.R @@ -10,6 +10,8 @@ import io.legado.app.data.entities.BookProgress import io.legado.app.exception.NoStackTraceException import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.Coroutine +import io.legado.app.help.storage.Backup +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.WebDav @@ -29,13 +31,14 @@ import java.util.* * webDav初始化会访问网络,不要放到主线程 */ object AppWebDav { - private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" + const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" private val zipFilePath = "${appCtx.externalFiles.absolutePath}${File.separator}backup.zip" private val bookProgressUrl get() = "${rootWebDavUrl}bookProgress/" private val exportsWebDavUrl get() = "$rootWebDavUrl${EncoderUtils.escape("exports")}/" val syncBookProgress get() = appCtx.getPrefBoolean(PreferKey.syncBookProgress, true) var authorization: Authorization? = null + private set val isOk get() = authorization != null @@ -132,7 +135,7 @@ object AppWebDav { suspend fun hasBackUp(): Boolean { authorization?.let { - val url = "${rootWebDavUrl}${backupFileName}" + val url = "$rootWebDavUrl$backupFileName" return WebDav(url, it).exists() } return false @@ -166,7 +169,7 @@ object AppWebDav { } FileUtils.delete(zipFilePath) if (ZipUtils.zipFiles(paths, zipFilePath)) { - val putUrl = "${rootWebDavUrl}${backupFileName}" + val putUrl = "$rootWebDavUrl$backupFileName" WebDav(putUrl, it).upload(zipFilePath) } } diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 4335c4beb..702d28ecc 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -6,6 +6,7 @@ import androidx.documentfile.provider.DocumentFile import io.legado.app.constant.AppLog import io.legado.app.constant.PreferKey import io.legado.app.data.appDb +import io.legado.app.help.AppWebDav import io.legado.app.help.config.LocalConfig import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ThemeConfig diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index 713a21712..614d71fc5 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -4,17 +4,16 @@ import io.legado.app.constant.AppLog import io.legado.app.constant.BookType import io.legado.app.data.appDb import io.legado.app.data.entities.* +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.storage.AppWebDav import io.legado.app.model.webBook.WebBook import io.legado.app.service.BaseReadAloudService import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.provider.ChapterProvider -import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.utils.msg import io.legado.app.utils.toastOnUi import kotlinx.coroutines.CoroutineScope diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 1e29d7662..41be4c7e3 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -18,10 +18,10 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.exception.NoStackTraceException +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor import io.legado.app.help.config.AppConfig -import io.legado.app.help.storage.AppWebDav import io.legado.app.utils.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ensureActive diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 8308029da..c507cb2cf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -24,12 +24,12 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookProgress import io.legado.app.data.entities.BookSource import io.legado.app.exception.NoStackTraceException +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp import io.legado.app.help.IntentData import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadTipConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.storage.AppWebDav import io.legado.app.help.storage.Backup import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.alert diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index fdf8e11a1..4f2094184 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -14,11 +14,11 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookProgress import io.legado.app.data.entities.BookSource import io.legado.app.exception.NoStackTraceException +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.storage.AppWebDav import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook import io.legado.app.model.localBook.LocalBook diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt index 48eb6b934..87a7eff57 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt @@ -2,7 +2,6 @@ package io.legado.app.ui.book.remote import android.app.Application import android.net.Uri -import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel import io.legado.app.model.localBook.LocalBook import io.legado.app.ui.book.remote.manager.RemoteBookWebDav @@ -13,11 +12,7 @@ import kotlinx.coroutines.flow.flowOn import java.util.* class RemoteBookViewModel(application: Application): BaseViewModel(application){ - private val remoteBookFolderName = "book_remote" private var dataCallback : DataCallback? = null - var isRemoteBookLiveData = MutableLiveData() - var dataFlowStart: (() -> Unit)? = null - val dataFlow = callbackFlow> { @@ -41,15 +36,18 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){ trySend(emptyList()) } } -// withContext(Dispatchers.Main) { -// dataFlowStart?.invoke() -// } awaitClose { dataCallback = null } }.flowOn(Dispatchers.IO) + init { + execute { + RemoteBookWebDav.initRemoteContext() + } + } + fun loadRemoteBookList() { execute { dataCallback?.clear() @@ -58,8 +56,6 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){ } } - - fun addToBookshelf(uriList: HashSet, finally: () -> Unit) { execute { uriList.forEach { diff --git a/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt b/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt index c099180c3..995463f58 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt @@ -3,30 +3,22 @@ package io.legado.app.ui.book.remote.manager import android.net.Uri import io.legado.app.constant.PreferKey - import io.legado.app.exception.NoStackTraceException +import io.legado.app.help.AppWebDav import io.legado.app.help.config.AppConfig - -import io.legado.app.lib.webdav.Authorization import io.legado.app.lib.webdav.WebDav -import io.legado.app.lib.webdav.WebDavException import io.legado.app.lib.webdav.WebDavFile -import io.legado.app.ui.book.info.BookInfoActivity - import io.legado.app.ui.book.remote.RemoteBook import io.legado.app.ui.book.remote.RemoteBookManager import io.legado.app.utils.* import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withTimeout import splitties.init.appCtx import java.io.File -import java.nio.charset.Charset object RemoteBookWebDav : RemoteBookManager() { - private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" - private var authorization: Authorization? = null private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}" private val localSaveFolder get() = "${appCtx.externalFiles.absolutePath}${File.separator}${remoteBookFolder}" + init { runBlocking { initRemoteContext() @@ -36,7 +28,7 @@ object RemoteBookWebDav : RemoteBookManager() { private val rootWebDavUrl: String get() { val configUrl = appCtx.getPrefString(PreferKey.webDavUrl)?.trim() - var url = if (configUrl.isNullOrEmpty()) defaultWebDavUrl else configUrl + var url = if (configUrl.isNullOrEmpty()) AppWebDav.defaultWebDavUrl else configUrl if (!url.endsWith("/")) url = "${url}/" AppConfig.webDavDir?.trim()?.let { if (it.isNotEmpty()) { @@ -47,27 +39,17 @@ object RemoteBookWebDav : RemoteBookManager() { } override suspend fun initRemoteContext() { - kotlin.runCatching { - authorization = null - val account = appCtx.getPrefString(PreferKey.webDavAccount) - val password = appCtx.getPrefString(PreferKey.webDavPassword) - if (!account.isNullOrBlank() && !password.isNullOrBlank()) { - val mAuthorization = Authorization(account, password) - WebDav(rootWebDavUrl, mAuthorization).makeAsDir() - WebDav(remoteBookUrl, mAuthorization).makeAsDir() - authorization = mAuthorization - } - }.onFailure { - it.printStackTrace() + AppWebDav.authorization?.let { + WebDav(remoteBookUrl, it).makeAsDir() } } @Throws(Exception::class) override suspend fun getRemoteBookList(): MutableList { val remoteBooks = mutableListOf() - authorization?.let { + AppWebDav.authorization?.let { //读取文件列表 - var remoteWebDavFileList : List? = null + var remoteWebDavFileList: List? = null kotlin.runCatching { remoteWebDavFileList = WebDav(remoteBookUrl, it).listFiles() } @@ -97,13 +79,13 @@ object RemoteBookWebDav : RemoteBookManager() { override suspend fun getRemoteBook(remoteBook: RemoteBook): String? { val saveFilePath= "${localSaveFolder}${File.separator}${remoteBook.filename}" kotlin.runCatching { - authorization?.let { - FileUtils.createFolderIfNotExist(localSaveFolder).run{ - val webdav = WebDav( - remoteBook.urlName, - it - ) - webdav.downloadTo(saveFilePath, true) + AppWebDav.authorization?.let { + FileUtils.createFolderIfNotExist(localSaveFolder).run { + val webdav = WebDav( + remoteBook.urlName, + it + ) + webdav.downloadTo(saveFilePath, true) } } }.onFailure { @@ -122,10 +104,13 @@ object RemoteBookWebDav : RemoteBookManager() { val localBookName = localBookUri.path?.substringAfterLast(File.separator) val putUrl = "${remoteBookUrl}${File.separator}${localBookName}" kotlin.runCatching { - authorization?.let { - if (localBookUri.isContentScheme()){ - WebDav(putUrl, it).upload(byteArray = localBookUri.readBytes(appCtx),contentType = "application/octet-stream") - }else{ + AppWebDav.authorization?.let { + if (localBookUri.isContentScheme()) { + WebDav(putUrl, it).upload( + byteArray = localBookUri.readBytes(appCtx), + contentType = "application/octet-stream" + ) + } else { WebDav(putUrl, it).upload(localBookUri.path!!) } } diff --git a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt index 4e535001f..c42a6187c 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt @@ -17,10 +17,14 @@ import androidx.preference.Preference import io.legado.app.R import io.legado.app.constant.AppLog import io.legado.app.constant.PreferKey +import io.legado.app.help.AppWebDav import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.storage.* +import io.legado.app.help.storage.Backup +import io.legado.app.help.storage.BackupConfig +import io.legado.app.help.storage.ImportOldData +import io.legado.app.help.storage.Restore import io.legado.app.lib.dialogs.alert import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt b/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt index fb79430ae..dc408842b 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt @@ -3,8 +3,8 @@ package io.legado.app.ui.config import android.app.Application import io.legado.app.R import io.legado.app.base.BaseViewModel +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp -import io.legado.app.help.storage.AppWebDav import io.legado.app.utils.FileUtils import io.legado.app.utils.toastOnUi diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index d179f5cd1..7eb32b074 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -21,11 +21,11 @@ import io.legado.app.constant.AppConst.appInfo import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.databinding.ActivityMainBinding +import io.legado.app.help.AppWebDav import io.legado.app.help.BookHelp import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.storage.AppWebDav import io.legado.app.help.storage.Backup import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.elevation diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 7998a9f9e..06022a617 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -10,10 +10,10 @@ import io.legado.app.constant.EventBus import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource +import io.legado.app.help.AppWebDav import io.legado.app.help.DefaultData import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig -import io.legado.app.help.storage.AppWebDav import io.legado.app.model.CacheBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService