pull/1990/head
kunfei 2 years ago
parent c9ccda2ca7
commit 8b8faaa30f
  1. 14
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt
  4. 9
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt
  5. 10
      app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt

@ -12,6 +12,7 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityRemoteBookBinding
import io.legado.app.ui.book.remote.manager.RemoteBookWebDav
import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.utils.toastOnUi
@ -50,7 +51,7 @@ class RemoteBookActivity : VMBaseActivity<ActivityRemoteBookBinding, RemoteBookV
adapter.setItems(remoteBooks)
}
}
viewModel.loadRemoteBookList()
viewModel.loadRemoteBookList(RemoteBookWebDav.rootBookUrl)
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -61,14 +62,19 @@ class RemoteBookActivity : VMBaseActivity<ActivityRemoteBookBinding, RemoteBookV
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_refresh -> {
viewModel.loadRemoteBookList()
viewModel.loadRemoteBookList(
viewModel.dirList.lastOrNull()?.path ?: RemoteBookWebDav.rootBookUrl
)
}
}
return super.onCompatOptionsItemSelected(item)
}
override fun openDir(url: String) {
override fun openDir(remoteBook: RemoteBook) {
viewModel.dirList.add(remoteBook)
binding.titleBar.title = remoteBook.filename
viewModel.dataCallback?.clear()
viewModel.loadRemoteBookList(remoteBook.path)
}
@SuppressLint("NotifyDataSetChanged")

@ -51,7 +51,7 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) :
binding.btnDownload.setOnClickListener {
getItem(holder.layoutPosition)?.let {
if (it.isDir) {
callBack.openDir(it.path)
callBack.openDir(it)
} else {
callBack.addToBookshelf(it)
}
@ -61,7 +61,7 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) :
}
interface CallBack {
fun openDir(url: String)
fun openDir(remoteBook: RemoteBook)
fun addToBookshelf(remoteBook: RemoteBook)
}
}

@ -5,7 +5,7 @@ import android.net.Uri
abstract class RemoteBookManager {
protected val remoteBookFolder : String = "books"
abstract suspend fun initRemoteContext()
abstract suspend fun getRemoteBookList(): MutableList<RemoteBook>
abstract suspend fun getRemoteBookList(path: String): MutableList<RemoteBook>
abstract suspend fun upload(localBookUri: Uri): Boolean
abstract suspend fun delete(remoteBookUrl: String): Boolean

@ -13,7 +13,10 @@ import kotlinx.coroutines.flow.flowOn
import java.util.*
class RemoteBookViewModel(application: Application): BaseViewModel(application){
private var dataCallback : DataCallback? = null
val dirList = arrayListOf<RemoteBook>()
var dataCallback: DataCallback? = null
val dataFlow = callbackFlow<List<RemoteBook>> {
@ -49,10 +52,10 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){
}
}
fun loadRemoteBookList() {
fun loadRemoteBookList(path: String) {
execute {
dataCallback?.clear()
val bookList = RemoteBookWebDav.getRemoteBookList()
val bookList = RemoteBookWebDav.getRemoteBookList(path)
dataCallback?.setItems(bookList)
}.onError {
context.toastOnUi("获取webDav书籍出错\n${it.localizedMessage}")

@ -19,7 +19,7 @@ import java.io.File
import java.net.URLDecoder
object RemoteBookWebDav : RemoteBookManager() {
private val remoteBookUrl get() = "${AppWebDav.rootWebDavUrl}${remoteBookFolder}"
val rootBookUrl get() = "${AppWebDav.rootWebDavUrl}${remoteBookFolder}"
init {
runBlocking {
@ -29,7 +29,7 @@ object RemoteBookWebDav : RemoteBookManager() {
override suspend fun initRemoteContext() {
AppWebDav.authorization?.let {
WebDav(remoteBookUrl, it).makeAsDir()
WebDav(rootBookUrl, it).makeAsDir()
}
}
@ -37,11 +37,11 @@ object RemoteBookWebDav : RemoteBookManager() {
* 获取远程书籍列表
*/
@Throws(Exception::class)
override suspend fun getRemoteBookList(): MutableList<RemoteBook> {
override suspend fun getRemoteBookList(path: String): MutableList<RemoteBook> {
val remoteBooks = mutableListOf<RemoteBook>()
AppWebDav.authorization?.let {
//读取文件列表
val remoteWebDavFileList: List<WebDavFile> = WebDav(remoteBookUrl, it).listFiles()
val remoteWebDavFileList: List<WebDavFile> = WebDav(path, it).listFiles()
//转化远程文件信息到本地对象
remoteWebDavFileList.forEach { webDavFile ->
var webDavFileName = webDavFile.displayName
@ -93,7 +93,7 @@ object RemoteBookWebDav : RemoteBookManager() {
if (!NetworkUtils.isAvailable()) return false
val localBookName = localBookUri.path?.substringAfterLast(File.separator)
val putUrl = "${remoteBookUrl}${File.separator}${localBookName}"
val putUrl = "${rootBookUrl}${File.separator}${localBookName}"
AppWebDav.authorization?.let {
if (localBookUri.isContentScheme()) {
WebDav(putUrl, it).upload(

Loading…
Cancel
Save