Merge pull request #1957 from qianfanguojin/master

修复WebDav书籍相关的一些Bug
pull/1966/head
kunfei 3 years ago committed by GitHub
commit 39f0bfe419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  2. 9
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt
  3. 7
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt
  4. 18
      app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt
  5. 13
      app/src/main/res/layout/activity_remote_book.xml

@ -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.book.toc.TocActivityResult
import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.login.SourceLoginActivity
import io.legado.app.ui.widget.dialog.PhotoDialog 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.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -206,11 +207,20 @@ class BookInfoActivity :
R.id.menu_upload -> { R.id.menu_upload -> {
launch { launch {
val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString()) val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString())
if (RemoteBookWebDav.upload(uri)) val waitDialog = WaitDialog(this@BookInfoActivity)
toastOnUi(getString(R.string.upload_book_success)) waitDialog.setText("上传中.....")
else waitDialog.show()
toastOnUi(getString(R.string.upload_book_fail)) 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()
}
} }
} }
} }

@ -10,6 +10,7 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityRemoteBookBinding 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.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
@ -50,9 +51,13 @@ class RemoteBookActivity : VMBaseActivity<ActivityRemoteBookBinding,RemoteBookVi
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
override fun addToBookshelf(remoteBook: RemoteBook) { override fun addToBookshelf(remoteBook: RemoteBook) {
viewModel.addToBookshelf(remoteBook){ val waitDialog = WaitDialog(this)
toastOnUi(getString(R.string.download_book_fail)) waitDialog.show()
viewModel.addToBookshelf(remoteBook, success = {
toastOnUi(getString(R.string.download_book_success))
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
}){
waitDialog.dismiss()
} }
} }
} }

@ -5,6 +5,7 @@ import android.net.Uri
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.ui.book.remote.manager.RemoteBookWebDav import io.legado.app.ui.book.remote.manager.RemoteBookWebDav
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
@ -69,12 +70,16 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){
/** /**
* 添加书籍到本地书架 * 添加书籍到本地书架
*/ */
fun addToBookshelf(remoteBook: RemoteBook, finally: () -> Unit) { fun addToBookshelf(remoteBook: RemoteBook, success: () -> Unit, finally: () -> Unit) {
execute { execute {
val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook) val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook)
downloadBookPath?.let { downloadBookPath?.let {
LocalBook.importFile(it) LocalBook.importFile(it)
} }
}.onSuccess {
success.invoke()
}.onError {
context.toastOnUi(it.localizedMessage)
}.onFinally { }.onFinally {
finally.invoke() finally.invoke()
} }

@ -19,6 +19,7 @@ import io.legado.app.utils.readBytes
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.net.URLDecoder
object RemoteBookWebDav : RemoteBookManager() { object RemoteBookWebDav : RemoteBookManager() {
private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}" private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}"
@ -48,6 +49,9 @@ object RemoteBookWebDav : RemoteBookManager() {
} }
} }
/**
* 获取远程书籍列表
*/
@Throws(Exception::class) @Throws(Exception::class)
override suspend fun getRemoteBookList(): MutableList<RemoteBook> { override suspend fun getRemoteBookList(): MutableList<RemoteBook> {
val remoteBooks = mutableListOf<RemoteBook>() val remoteBooks = mutableListOf<RemoteBook>()
@ -61,9 +65,10 @@ object RemoteBookWebDav : RemoteBookManager() {
remoteWebDavFileList = remoteWebDavFileList!!.reversed() remoteWebDavFileList = remoteWebDavFileList!!.reversed()
//转化远程文件信息到本地对象 //转化远程文件信息到本地对象
remoteWebDavFileList!!.forEach { webDavFile -> remoteWebDavFileList!!.forEach { webDavFile ->
val webDavFileName = webDavFile.displayName var webDavFileName = webDavFile.displayName
val webDavUrlName = "${remoteBookUrl}${File.separator}${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 trueFileName = String(webDavFileName.toByteArray(Charset.forName("GBK")), Charset.forName("UTF-8"))
//val trueUrlName = String(webDavUrlName.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 return remoteBooks
} }
/**
* 下载指定的远程书籍到本地
*/
override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? { override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? {
return kotlin.runCatching { return kotlin.runCatching {
AppWebDav.authorization?.let { AppWebDav.authorization?.let {
@ -98,7 +106,7 @@ object RemoteBookWebDav : RemoteBookManager() {
} }
} }
}.onFailure { }.onFailure {
it.printStackTrace() throw it
}.getOrNull() }.getOrNull()
} }
@ -122,7 +130,7 @@ object RemoteBookWebDav : RemoteBookManager() {
} }
} }
}.onFailure { }.onFailure {
return false throw it
} }
return true return true
} }

@ -30,6 +30,15 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<io.legado.app.ui.widget.anima.RotateLoading
android:id="@+id/rotate_loading"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_margin="6dp"
android:visibility="gone"
android:layout_gravity="center"
app:loading_width="2dp" />
</io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout> </io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout>
<!-- <io.legado.app.ui.widget.SelectActionBar--> <!-- <io.legado.app.ui.widget.SelectActionBar-->
@ -47,10 +56,12 @@
android:layout_margin="16dp" android:layout_margin="16dp"
android:gravity="center" android:gravity="center"
android:visibility="gone" android:visibility="gone"
android:text="webDav book文件夹为空" android:text="webDav books文件夹为空"
app:layout_constraintTop_toBottomOf="@+id/title_bar" app:layout_constraintTop_toBottomOf="@+id/title_bar"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
tools:text="Empty" /> tools:text="Empty" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save