From 3d5e0cc230a13d6dcefd88a6508f5311c94f6881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Tue, 7 Jun 2022 22:06:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.=20=E4=BF=AE=E5=A4=8D=E4=B9=A6?= =?UTF-8?q?=E8=AE=B0=E6=96=87=E4=BB=B6=E5=90=8D=E4=B8=BA=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=97=B6=E6=98=BE=E7=A4=BA=E4=B9=B1=E7=A0=81=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=202.=20=E6=B7=BB=E5=8A=A0=E4=B8=8A=E4=BC=A0=E5=92=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E4=B9=A6=E7=B1=8D=E6=97=B6=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E6=A1=86=E4=BB=A5=E5=8F=8A=E5=BC=82=E5=B8=B8=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/info/BookInfoActivity.kt | 20 ++++++++++++++----- .../app/ui/book/remote/RemoteBookActivity.kt | 9 +++++++-- .../app/ui/book/remote/RemoteBookViewModel.kt | 7 ++++++- .../book/remote/manager/RemoteBookWebDav.kt | 18 ++++++++++++----- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 0f93ba53b..eb79ef5e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -40,6 +40,7 @@ import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.widget.dialog.PhotoDialog +import io.legado.app.ui.widget.dialog.WaitDialog import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO @@ -206,11 +207,20 @@ class BookInfoActivity : R.id.menu_upload -> { launch { val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString()) - if (RemoteBookWebDav.upload(uri)) - toastOnUi(getString(R.string.upload_book_success)) - else - toastOnUi(getString(R.string.upload_book_fail)) - + val waitDialog = WaitDialog(this@BookInfoActivity) + waitDialog.setText("上传中.....") + waitDialog.show() + try { + val isUpload = RemoteBookWebDav.upload(uri) + if (isUpload) + toastOnUi(getString(R.string.upload_book_success)) + else + toastOnUi(getString(R.string.upload_book_fail)) + }catch (e : Exception){ + toastOnUi(e.localizedMessage) + }finally { + waitDialog.dismiss() + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt index 0aa43b0fb..cd2319c55 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt @@ -10,6 +10,7 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivityRemoteBookBinding +import io.legado.app.ui.widget.dialog.WaitDialog import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -50,9 +51,13 @@ class RemoteBookActivity : VMBaseActivity Unit) { + fun addToBookshelf(remoteBook: RemoteBook, success: () -> Unit, finally: () -> Unit) { execute { val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook) downloadBookPath?.let { LocalBook.importFile(it) } + }.onSuccess { + success.invoke() + }.onError { + context.toastOnUi(it.localizedMessage) }.onFinally { finally.invoke() } 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 982ac183f..3466ee2a5 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 @@ -19,6 +19,7 @@ import io.legado.app.utils.readBytes import kotlinx.coroutines.runBlocking import splitties.init.appCtx import java.io.File +import java.net.URLDecoder object RemoteBookWebDav : RemoteBookManager() { private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}" @@ -48,6 +49,9 @@ object RemoteBookWebDav : RemoteBookManager() { } } + /** + * 获取远程书籍列表 + */ @Throws(Exception::class) override suspend fun getRemoteBookList(): MutableList { val remoteBooks = mutableListOf() @@ -61,9 +65,10 @@ object RemoteBookWebDav : RemoteBookManager() { remoteWebDavFileList = remoteWebDavFileList!!.reversed() //转化远程文件信息到本地对象 remoteWebDavFileList!!.forEach { webDavFile -> - val webDavFileName = webDavFile.displayName - val webDavUrlName = "${remoteBookUrl}${File.separator}${webDavFile.displayName}" - + var webDavFileName = webDavFile.displayName + var webDavUrlName = "${remoteBookUrl}${File.separator}${webDavFile.displayName}" + webDavFileName = URLDecoder.decode(webDavFileName,"utf-8") + webDavUrlName = URLDecoder.decode(webDavUrlName,"utf-8") // 转码 //val trueFileName = String(webDavFileName.toByteArray(Charset.forName("GBK")), Charset.forName("UTF-8")) //val trueUrlName = String(webDavUrlName.toByteArray(Charset.forName("GBK")), Charset.forName("UTF-8")) @@ -86,6 +91,9 @@ object RemoteBookWebDav : RemoteBookManager() { return remoteBooks } + /** + * 下载指定的远程书籍到本地 + */ override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? { return kotlin.runCatching { AppWebDav.authorization?.let { @@ -98,7 +106,7 @@ object RemoteBookWebDav : RemoteBookManager() { } } }.onFailure { - it.printStackTrace() + throw it }.getOrNull() } @@ -122,7 +130,7 @@ object RemoteBookWebDav : RemoteBookManager() { } } }.onFailure { - return false + throw it } return true }