From 1c8a0fd000fce3811c7cd3e16d5cd6f0224aeb46 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, 17 May 2022 18:10:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E4=B8=8B=E8=BD=BD=E7=9A=84=E9=83=A8=E5=88=86?= =?UTF-8?q?BUG=EF=BC=8C=E5=8A=9F=E8=83=BD=E5=B7=B2=E7=BB=8F=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/remote/RemoteBookActivity.kt | 12 +++-- .../app/ui/book/remote/RemoteBookAdapter.kt | 9 ++-- .../app/ui/book/remote/RemoteBookManager.kt | 14 +++-- .../app/ui/book/remote/RemoteBookViewModel.kt | 51 ++++++------------- .../book/remote/manager/RemoteBookWebDav.kt | 42 ++++++++++----- app/src/main/res/layout/item_remote_book.xml | 4 +- 6 files changed, 66 insertions(+), 66 deletions(-) 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 c011c5cdd..c9a7f5f56 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 @@ -1,5 +1,6 @@ package io.legado.app.ui.book.remote +import android.annotation.SuppressLint import android.os.Bundle import androidx.activity.viewModels import androidx.recyclerview.widget.LinearLayoutManager @@ -46,8 +47,8 @@ class RemoteBookActivity : VMBaseActivity adapter.setItems(remoteBooks) @@ -60,7 +61,10 @@ class RemoteBookActivity : VMBaseActivity ) { binding.run { - tvName.text = item.filename + tvName.text = item.filename.substringBeforeLast(".") tvContentType.text = item.contentType tvSize.text = ConvertUtils.formatFileSize(item.size) tvDate.text = LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd") @@ -48,8 +48,9 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : override fun registerListener(holder: ItemViewHolder, binding: ItemRemoteBookBinding) { binding.btnDownload.setOnClickListener { getItem(holder.layoutPosition)?.let { - context.toastOnUi("开始下载") - callBack.download(it) + context.toastOnUi("开始加入") + callBack.addToBookshelf(it) + context.toastOnUi("加入成功") } } @@ -57,6 +58,6 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : } interface CallBack { - fun download(remoteBook: RemoteBook) + fun addToBookshelf(remoteBook: RemoteBook) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt index 363649c80..9fd1d1faf 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt @@ -1,13 +1,7 @@ package io.legado.app.ui.book.remote -import android.content.ContentResolver -import android.database.Cursor import android.net.Uri -import android.provider.MediaStore -import android.provider.OpenableColumns -import android.webkit.MimeTypeMap -import java.io.File -import kotlin.random.Random + abstract class RemoteBookManager { @@ -17,5 +11,9 @@ abstract class RemoteBookManager { abstract suspend fun getRemoteBookList(): MutableList abstract suspend fun upload(localBookUri: Uri): Boolean abstract suspend fun delete(remoteBookUrl: String): Boolean - abstract suspend fun getRemoteBook(remoteBookUrl: String): RemoteBook + + /** + * @return String:下载到本地的路径 + */ + abstract suspend fun getRemoteBook(remoteBook: RemoteBook): String? } \ No newline at end of file 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 d1feebd02..34493aec3 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 @@ -55,56 +55,37 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){ // } awaitClose { -// dataCallback = null + dataCallback = null } }.flowOn(Dispatchers.IO) fun loadRemoteBookList() { execute { dataCallback?.clear() - RemoteBookWebDav.getRemoteBookList() + val bookList = RemoteBookWebDav.getRemoteBookList() + dataCallback?.setItems(bookList) } -// dataCallback?.setItems() - } -// dataCallback?.setItems(listOf("1", "2", "3")) + } - fun downloadRemoteBook(urlName: String) { - val saveFolder = "${appCtx.externalFiles.absolutePath}${File.separator}${remoteBookFolderName}" - val trueCodeURLName = String(urlName.toByteArray(Charset.forName("GBK")), Charset.forName("UTF-8")) - val saveFilePath = "${saveFolder}${trueCodeURLName}" - execute { - 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) - authorization = mAuthorization - } - } - authorization?.let { it -> - FileUtils.createFolderIfNotExist(saveFolder).run{ - withTimeout(15000L) { - val webdav = WebDav( - "http://txc.qianfanguojin.top${trueCodeURLName}", - it - ) - webdav.downloadTo(saveFilePath, true) - } - } + fun addToBookshelf(uriList: HashSet, finally: () -> Unit) { + execute { + uriList.forEach { + LocalBook.importFile(Uri.parse(it)) } }.onFinally { - addToBookshelf(hashSetOf("${saveFolder}${trueCodeURLName}")){ - Log.e("TAG", "downloadRemoteBook: add", ) - } + finally.invoke() } - } - fun addToBookshelf(uriList: HashSet, finally: () -> Unit) { + + /** + * 添加书籍到本地书架 + */ + fun addToBookshelf(remoteBook: RemoteBook, finally: () -> Unit) { execute { - uriList.forEach { + val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook) + downloadBookPath?.let { LocalBook.importFile(Uri.parse(it)) } }.onFinally { 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 90e38914b..c099180c3 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 @@ -9,6 +9,7 @@ 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 @@ -16,6 +17,7 @@ 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 @@ -24,7 +26,7 @@ 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() @@ -63,38 +65,52 @@ object RemoteBookWebDav : RemoteBookManager() { @Throws(Exception::class) override suspend fun getRemoteBookList(): MutableList { val remoteBooks = mutableListOf() - authorization?.let { + //读取文件列表 var remoteWebDavFileList : List? = null kotlin.runCatching { remoteWebDavFileList = WebDav(remoteBookUrl, it).listFiles() } - - + //逆序文件排序 remoteWebDavFileList = remoteWebDavFileList!!.reversed() + //转化远程文件信息到本地对象 remoteWebDavFileList!!.forEach { webDavFile -> val webDavFileName = webDavFile.displayName - val webDavUrlName = webDavFile.urlName + val webDavUrlName = "${remoteBookUrl}${File.separator}${webDavFile.displayName}" // 转码 - val trueFileName = String(webDavFileName.toByteArray(Charset.forName("GBK")), Charset.forName("UTF-8")) - val trueUrlName = String(webDavUrlName.toByteArray(Charset.forName("GBK")), Charset.forName("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")) - //分割文件名和后缀 - val filename = trueFileName.substringBeforeLast(".") - val fileExtension = trueFileName.substringAfterLast(".") + //分割后缀 + val fileExtension = webDavFileName.substringAfterLast(".") //扩展名符合阅读的格式则认为是书籍 if (contentTypeList.contains(fileExtension)) { - remoteBooks.add(RemoteBook(filename,trueUrlName,webDavFile.size,fileExtension,webDavFile.lastModify)) + remoteBooks.add(RemoteBook(webDavFileName,webDavUrlName,webDavFile.size,fileExtension,webDavFile.lastModify)) } } } ?: throw NoStackTraceException("webDav没有配置") return remoteBooks } - override suspend fun getRemoteBook(remoteBookUrl: String): RemoteBook { - TODO("Not yet implemented") + 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) + } + } + }.onFailure { + it.printStackTrace() + return null + } + return saveFilePath } /** diff --git a/app/src/main/res/layout/item_remote_book.xml b/app/src/main/res/layout/item_remote_book.xml index 4d5ae1264..ca84c2e51 100644 --- a/app/src/main/res/layout/item_remote_book.xml +++ b/app/src/main/res/layout/item_remote_book.xml @@ -97,12 +97,12 @@ app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="3dp" > +