From d24eef36fe49badb8fe0c982ac107eed76cdb6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Thu, 12 May 2022 18:59:05 +0800 Subject: [PATCH 01/12] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E5=B8=83=E5=B1=80=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/remote/RemoteBookActivity.kt | 34 +++++++++++++++++++ .../app/ui/book/remote/RemoteBookViewModel.kt | 7 ++++ .../main/res/layout/activity_remote_book.xml | 25 ++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt create mode 100644 app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt create mode 100644 app/src/main/res/layout/activity_remote_book.xml 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 new file mode 100644 index 000000000..acf1e5d71 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt @@ -0,0 +1,34 @@ +package io.legado.app.ui.book.remote + +import android.os.Bundle +import androidx.activity.viewModels +import io.legado.app.base.VMBaseActivity + + +import io.legado.app.databinding.ActivityRemoteBookBinding +import io.legado.app.utils.toastOnUi + +import io.legado.app.utils.viewbindingdelegate.viewBinding + +/** + * 展示远程书籍 + * @author qianfanguojin + * @time 2022/05/12 + */ +class RemoteBookActivity : VMBaseActivity() { + override val binding by viewBinding(ActivityRemoteBookBinding::inflate) + override val viewModel by viewModels() + override fun onActivityCreated(savedInstanceState: Bundle?) { + toastOnUi("远程书籍") + } + + private fun initView() { +// binding.layTop.setBackgroundColor(backgroundColor) +// binding.recyclerView.layoutManager = LinearLayoutManager(this) +// binding.recyclerView.adapter = adapter +// binding.selectActionBar.setMainActionText(R.string.add_to_shelf) +// binding.selectActionBar.inflateMenu(R.menu.import_book_sel) +// binding.selectActionBar.setOnMenuItemClickListener(this) +// binding.selectActionBar.setCallBack(this) + } +} \ 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 new file mode 100644 index 000000000..26088cff5 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt @@ -0,0 +1,7 @@ +package io.legado.app.ui.book.remote + +import android.app.Application +import io.legado.app.base.BaseViewModel + +class RemoteBookViewModel(application: Application): BaseViewModel(application){ +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_remote_book.xml b/app/src/main/res/layout/activity_remote_book.xml new file mode 100644 index 000000000..00473e8c1 --- /dev/null +++ b/app/src/main/res/layout/activity_remote_book.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + \ No newline at end of file From 4ecaa4e2bf3d5e0401446205b10b57a4b79e3ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Thu, 12 May 2022 19:52:00 +0800 Subject: [PATCH 02/12] =?UTF-8?q?feat:=20=E8=B0=83=E4=BB=B7=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=20Activity=20=E6=B5=8B=E8=AF=95=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 4 ++++ .../legado/app/ui/main/bookshelf/BaseBookshelfFragment.kt | 4 ++++ app/src/main/res/menu/main_bookshelf.xml | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6328e86ea..afe93fb0b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -280,6 +280,10 @@ + + startActivity() R.id.menu_search -> startActivity() R.id.menu_update_toc -> activityViewModel.upToc(books) R.id.menu_bookshelf_layout -> configBookshelf() diff --git a/app/src/main/res/menu/main_bookshelf.xml b/app/src/main/res/menu/main_bookshelf.xml index 9713c08eb..0ad539401 100644 --- a/app/src/main/res/menu/main_bookshelf.xml +++ b/app/src/main/res/menu/main_bookshelf.xml @@ -10,6 +10,12 @@ android:title="@string/search" app:showAsAction="always" /> + + Date: Fri, 13 May 2022 22:19:17 +0800 Subject: [PATCH 03/12] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E8=A7=86?= =?UTF-8?q?=E5=9B=BE=E4=B8=8E=E6=95=B0=E6=8D=AE=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/remote/RemoteBookActivity.kt | 37 +++++++- .../app/ui/book/remote/RemoteBookAdapter.kt | 43 +++++++++ .../app/ui/book/remote/RemoteBookViewModel.kt | 61 +++++++++++++ .../main/res/layout/activity_remote_book.xml | 20 +++- app/src/main/res/layout/item_remote_book.xml | 91 +++++++++++++++++++ 5 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt create mode 100644 app/src/main/res/layout/item_remote_book.xml 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 acf1e5d71..d6fdc428c 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,28 +1,41 @@ package io.legado.app.ui.book.remote import android.os.Bundle +import android.util.Log import androidx.activity.viewModels +import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.base.VMBaseActivity +import io.legado.app.data.entities.Book import io.legado.app.databinding.ActivityRemoteBookBinding +import io.legado.app.lib.theme.backgroundColor import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.flow.conflate +import kotlinx.coroutines.launch /** * 展示远程书籍 * @author qianfanguojin * @time 2022/05/12 */ -class RemoteBookActivity : VMBaseActivity() { +class RemoteBookActivity : VMBaseActivity(), + RemoteBookAdapter.CallBack { override val binding by viewBinding(ActivityRemoteBookBinding::inflate) override val viewModel by viewModels() + private val adapter by lazy { RemoteBookAdapter(this, this) } + override fun onActivityCreated(savedInstanceState: Bundle?) { - toastOnUi("远程书籍") + initView() + initData() +// toastOnUi("远程书籍") } private fun initView() { + binding.recyclerView.layoutManager = LinearLayoutManager(this) + binding.recyclerView.adapter = adapter // binding.layTop.setBackgroundColor(backgroundColor) // binding.recyclerView.layoutManager = LinearLayoutManager(this) // binding.recyclerView.adapter = adapter @@ -31,4 +44,24 @@ class RemoteBookActivity : VMBaseActivity + adapter.setItems(remoteBooks) + } + } + + +// toastOnUi("1") + viewModel.loadRemoteBookList() + } + override fun showRemoteBookInfo(book: Book) { + TODO("Not yet implemented") + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt new file mode 100644 index 000000000..d7f590408 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt @@ -0,0 +1,43 @@ +package io.legado.app.ui.book.remote + +import android.content.Context +import android.view.ViewGroup +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.RecyclerAdapter +import io.legado.app.data.entities.Book +import io.legado.app.databinding.ItemRemoteBookBinding + + +class RemoteBookAdapter (context: Context, val callBack: CallBack) : + RecyclerAdapter(context){ + + override fun getViewBinding(parent: ViewGroup): ItemRemoteBookBinding { + return ItemRemoteBookBinding.inflate(inflater, parent, false) + } + + override fun onCurrentListChanged() { + + } + + /** + * 绑定RecycleView 中每一个项的视图和数据 + */ + override fun convert( + holder: ItemViewHolder, + binding: ItemRemoteBookBinding, + item: String, + payloads: MutableList + ) { + binding.run { + tvName.text = item + } + } + + override fun registerListener(holder: ItemViewHolder, binding: ItemRemoteBookBinding) { + + } + + interface CallBack { + fun showRemoteBookInfo(book: Book) + } +} \ 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 26088cff5..5d646b725 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 @@ -1,7 +1,68 @@ package io.legado.app.ui.book.remote import android.app.Application +import android.util.Log +import android.widget.Toast import io.legado.app.base.BaseViewModel +import io.legado.app.utils.FileDoc +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.withContext +import java.util.* +import kotlin.reflect.typeOf class RemoteBookViewModel(application: Application): BaseViewModel(application){ + + private var dataCallback : DataCallback? = null + var dataFlowStart: (() -> Unit)? = null + val dataFlow = callbackFlow> { + + val list = Collections.synchronizedList(ArrayList()) + + dataCallback = object : DataCallback { + + override fun setItems(remoteFiles: List) { + list.clear() + list.addAll(remoteFiles) + Log.e("TAG", ": 1", ) + trySend(list) + } + + override fun addItems(remoteFiles: List) { + list.addAll(remoteFiles) + trySend(list) + } + + override fun clear() { + list.clear() + trySend(emptyList()) + } + } + +// withContext(Dispatchers.Main) { +// dataFlowStart?.invoke() +// } + + awaitClose { + dataCallback = null + } + }.flowOn(Dispatchers.Main) + + fun loadRemoteBookList() { + Log.e("TAG", dataCallback.toString(), ) + dataCallback?.setItems(listOf("1", "2", "3")) + } + + interface DataCallback { + + fun setItems(remoteFiles: List) + + fun addItems(remoteFiles: List) + + fun clear() + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_remote_book.xml b/app/src/main/res/layout/activity_remote_book.xml index 00473e8c1..55fc04f3c 100644 --- a/app/src/main/res/layout/activity_remote_book.xml +++ b/app/src/main/res/layout/activity_remote_book.xml @@ -16,10 +16,24 @@ + - + android:layout_height="0dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toBottomOf="@id/refresh_progress_bar"> + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_remote_book.xml b/app/src/main/res/layout/item_remote_book.xml new file mode 100644 index 000000000..f98be731a --- /dev/null +++ b/app/src/main/res/layout/item_remote_book.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 87732f17935f139721c8dd1d38397327660b75e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Sat, 14 May 2022 23:19:51 +0800 Subject: [PATCH 04/12] =?UTF-8?q?fix:=201.=20=E4=BF=AE=E5=A4=8DWebDav?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=97=B6=E9=97=B4=E5=AD=97=E6=AE=B5=E8=A7=A3?= =?UTF-8?q?=E6=9E=90BUG=202.=20=E8=B0=83=E6=95=B4=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E7=B1=BB=E4=B8=BA=20DateTimeFormatter=20+=20?= =?UTF-8?q?LocalDateTime=20=E7=9A=84=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/lib/webdav/WebDav.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index d27e731de..04d152845 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -19,14 +19,16 @@ import java.io.InputStream import java.net.MalformedURLException import java.net.URL import java.net.URLEncoder -import java.text.SimpleDateFormat +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter @Suppress("unused", "MemberVisibilityCanBePrivate") open class WebDav(urlStr: String, val authorization: Authorization) { companion object { - @SuppressLint("SimpleDateFormat") - private val dateFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ") + @SuppressLint("DateTimeFormatter") + private val dateTimeFormatter = DateTimeFormatter.RFC_1123_DATE_TIME // 指定返回哪些属性 @Language("xml") @@ -133,11 +135,11 @@ open class WebDav(urlStr: String, val authorization: Authorization) { .firstOrNull()?.text()?.toLong() ?: 0 }.getOrDefault(0) val lastModify: Long = kotlin.runCatching { - element.getElementsByTag("d:getcontentlength") + element.getElementsByTag("d:getlastmodified") .firstOrNull()?.text()?.let { - dateFormat.parse(it) + LocalDateTime.parse(it, dateTimeFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli() } - }.getOrNull()?.time ?: 0 + }.getOrNull() ?: 0 webDavFile = WebDavFile( baseUrl + fileName, authorization, From 605e6f1110fddfd60f55c8aa349add3e67e801a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Sun, 15 May 2022 01:00:27 +0800 Subject: [PATCH 05/12] =?UTF-8?q?feat:=20=E6=96=87=E4=BB=B6=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=B8=83=E5=B1=80=E8=AE=BE=E8=AE=A1=E5=88=9D=E7=A8=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/res/layout/activity_remote_book.xml | 5 + app/src/main/res/layout/item_remote_book.xml | 171 ++++++++++-------- 2 files changed, 97 insertions(+), 79 deletions(-) diff --git a/app/src/main/res/layout/activity_remote_book.xml b/app/src/main/res/layout/activity_remote_book.xml index 55fc04f3c..ed942a2cc 100644 --- a/app/src/main/res/layout/activity_remote_book.xml +++ b/app/src/main/res/layout/activity_remote_book.xml @@ -36,4 +36,9 @@ + \ No newline at end of file diff --git a/app/src/main/res/layout/item_remote_book.xml b/app/src/main/res/layout/item_remote_book.xml index f98be731a..06c2b5488 100644 --- a/app/src/main/res/layout/item_remote_book.xml +++ b/app/src/main/res/layout/item_remote_book.xml @@ -1,91 +1,104 @@ - + android:layout_height="60dp" + android:background="?attr/selectableItemBackground" + android:orientation="horizontal" + android:baselineAligned="false"> - + + + + + + - - - - - - - - - - - - - + android:layout_height="match_parent" + android:layout_weight="1" + android:paddingTop="5dp" + android:paddingBottom="5dp" + android:orientation="vertical"> - - - - - - - - - + + - - - - - + - - - - - - - - - + - - - - - - - - + + - - - - \ No newline at end of file + + + + + \ No newline at end of file From 4d768beea6e9e987fed03877502233cfda7a0d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Sun, 15 May 2022 13:23:47 +0800 Subject: [PATCH 06/12] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/ui/book/info/BookInfoActivity.kt | 2 ++ app/src/main/res/menu/book_info.xml | 5 +++++ 2 files changed, 7 insertions(+) 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 5b437029d..efa1f1526 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 @@ -127,6 +127,8 @@ class BookInfoActivity : viewModel.bookSource != null menu.findItem(R.id.menu_split_long_chapter)?.isVisible = viewModel.bookData.value?.isLocalTxt() ?: false + menu.findItem(R.id.menu_upload)?.isVisible = + viewModel.bookData.value?.isLocalBook() ?: false return super.onMenuOpened(featureId, menu) } diff --git a/app/src/main/res/menu/book_info.xml b/app/src/main/res/menu/book_info.xml index 911ba2332..675763611 100644 --- a/app/src/main/res/menu/book_info.xml +++ b/app/src/main/res/menu/book_info.xml @@ -14,6 +14,11 @@ android:title="@string/share" app:showAsAction="ifRoom" /> + + Date: Sun, 15 May 2022 14:16:25 +0800 Subject: [PATCH 07/12] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=B8=83=E5=B1=80=E4=B8=8A=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/layout/item_remote_book.xml | 160 ++++++++++--------- 1 file changed, 83 insertions(+), 77 deletions(-) diff --git a/app/src/main/res/layout/item_remote_book.xml b/app/src/main/res/layout/item_remote_book.xml index 06c2b5488..85af79d7f 100644 --- a/app/src/main/res/layout/item_remote_book.xml +++ b/app/src/main/res/layout/item_remote_book.xml @@ -1,104 +1,110 @@ - + android:layout_height="wrap_content"> - - - - - - + - + android:layout_height="wrap_content" + android:layout_margin="8dp" + android:text="@string/app_name" + android:textColor="@color/primaryText" + android:textSize="16sp" + app:layout_constraintStart_toEndOf="@+id/iv_cover" + app:layout_constraintTop_toTopOf="parent" /> + + - + android:layout_height="wrap_content" + android:ellipsize="end" + android:lines="1" + tools:text="128kb" + android:textColor="@color/tv_text_summary" + android:textSize="13sp" /> + - + - - - + - \ No newline at end of file + \ No newline at end of file From 3f1c95b7beafc8fd04089de457093bc6a7e08e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Mon, 16 May 2022 12:50:47 +0800 Subject: [PATCH 08/12] =?UTF-8?q?feat:=20=E4=B8=8B=E8=BD=BD=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/remote/RemoteBookActivity.kt | 16 ++- .../app/ui/book/remote/RemoteBookAdapter.kt | 27 ++++- .../app/ui/book/remote/RemoteBookViewModel.kt | 107 ++++++++++++++---- .../main/res/layout/activity_remote_book.xml | 12 +- app/src/main/res/layout/item_remote_book.xml | 5 +- 5 files changed, 129 insertions(+), 38 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 d6fdc428c..4f8232a91 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 @@ -29,10 +29,13 @@ class RemoteBookActivity : VMBaseActivity + viewModel.dataFlow.collect { remoteBooks -> adapter.setItems(remoteBooks) } } // toastOnUi("1") - viewModel.loadRemoteBookList() + } - override fun showRemoteBookInfo(book: Book) { - TODO("Not yet implemented") + + + override fun download(remoteBook: RemoteBook) { + viewModel.downloadRemoteBook(remoteBook.url) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt index d7f590408..99245f0f6 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt @@ -2,14 +2,23 @@ package io.legado.app.ui.book.remote import android.content.Context import android.view.ViewGroup +import cn.hutool.core.date.LocalDateTimeUtil import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.entities.Book import io.legado.app.databinding.ItemRemoteBookBinding +import io.legado.app.utils.ConvertUtils +import io.legado.app.utils.FileUtils +import io.legado.app.utils.toastOnUi +import splitties.init.appCtx +/** + * 适配器 + * @author qianfanguojin + */ class RemoteBookAdapter (context: Context, val callBack: CallBack) : - RecyclerAdapter(context){ + RecyclerAdapter(context){ override fun getViewBinding(parent: ViewGroup): ItemRemoteBookBinding { return ItemRemoteBookBinding.inflate(inflater, parent, false) @@ -25,19 +34,29 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : override fun convert( holder: ItemViewHolder, binding: ItemRemoteBookBinding, - item: String, + item: RemoteBook, payloads: MutableList ) { binding.run { - tvName.text = item + tvName.text = item.name.substringBeforeLast(".") + tvContentType.text = item.name.substringAfterLast(".") + tvSize.text = ConvertUtils.formatFileSize(item.size) + tvDate.text = LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd") } } override fun registerListener(holder: ItemViewHolder, binding: ItemRemoteBookBinding) { + binding.btnDownload.setOnClickListener { + getItem(holder.layoutPosition)?.let { + context.toastOnUi("开始下载") + callBack.download(it) + } + } + } interface CallBack { - fun showRemoteBookInfo(book: Book) + fun download(remoteBook: RemoteBook) } } \ 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 5d646b725..31cef12b0 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,36 +2,43 @@ package io.legado.app.ui.book.remote import android.app.Application import android.util.Log -import android.widget.Toast import io.legado.app.base.BaseViewModel -import io.legado.app.utils.FileDoc -import kotlinx.coroutines.Dispatchers +import io.legado.app.constant.PreferKey +import io.legado.app.lib.webdav.Authorization +import io.legado.app.lib.webdav.WebDav +import io.legado.app.utils.FileUtils +import io.legado.app.utils.exists +import io.legado.app.utils.externalFiles +import io.legado.app.utils.getPrefString +import kotlinx.coroutines.* import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOn -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.withContext +import splitties.init.appCtx +import java.io.File +import java.nio.charset.Charset import java.util.* -import kotlin.reflect.typeOf class RemoteBookViewModel(application: Application): BaseViewModel(application){ - + private val remoteBookFolderName = "book_remote" private var dataCallback : DataCallback? = null var dataFlowStart: (() -> Unit)? = null - val dataFlow = callbackFlow> { + private var authorization: Authorization? = null + + + val dataFlow = callbackFlow> { - val list = Collections.synchronizedList(ArrayList()) + val list = Collections.synchronizedList(ArrayList()) dataCallback = object : DataCallback { - override fun setItems(remoteFiles: List) { + override fun setItems(remoteFiles: List) { list.clear() list.addAll(remoteFiles) - Log.e("TAG", ": 1", ) trySend(list) } - override fun addItems(remoteFiles: List) { + override fun addItems(remoteFiles: List) { list.addAll(remoteFiles) trySend(list) } @@ -41,28 +48,88 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){ trySend(emptyList()) } } - // withContext(Dispatchers.Main) { // dataFlowStart?.invoke() // } awaitClose { - dataCallback = null +// dataCallback = null } - }.flowOn(Dispatchers.Main) + }.flowOn(Dispatchers.IO) fun loadRemoteBookList() { - Log.e("TAG", dataCallback.toString(), ) - dataCallback?.setItems(listOf("1", "2", "3")) + execute { + dataCallback?.clear() + 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 -> + val remoteWebDavFileList = WebDav("http://txc.qianfanguojin.top/",it).listFiles() + val remoteList = remoteWebDavFileList.map { + RemoteBook(it.displayName,it.urlName,it.size,"epub",it.lastModify) + } + dataCallback?.setItems(remoteList) + } +// dataCallback?.setItems() + } +// dataCallback?.setItems(listOf("1", "2", "3")) } + fun downloadRemoteBook(urlName: String) { + execute { +// kotlin.runCatching { +// val remoteWebDavFile = WebDav("http://txc.qianfanguojin.top/",authorization!!).getFile(url) +// val remoteBook = RemoteBook(remoteWebDavFile.displayName,remoteWebDavFile.urlName,remoteWebDavFile.size,"epub",remoteWebDavFile.lastModify) +// dataCallback?.addItems(listOf(remoteBook)) +// } + + 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 -> + Log.e("TAG", "downloadRemoteBook: 1", ) + val saveFolder = "${appCtx.externalFiles.absolutePath}${File.separator}${remoteBookFolderName}" + FileUtils.createFolderIfNotExist(saveFolder).run{ + +// Log.e("TAG", "downloadRemoteBook: 2 ${appCtx.externalFiles.absoluteFile}", ) + val trueCodeURLName = String(urlName.toByteArray(Charset.forName("GBK")), Charset.forName("UTF-8")) + withTimeout(15000L) { + val webdav = WebDav("http://txc.qianfanguojin.top${trueCodeURLName}", it) + webdav.downloadTo("${saveFolder}${trueCodeURLName}", true).apply { + } + } + } + } + } + } interface DataCallback { - fun setItems(remoteFiles: List) + fun setItems(remoteFiles: List) - fun addItems(remoteFiles: List) + fun addItems(remoteFiles: List) fun clear() } -} \ No newline at end of file +} + +data class RemoteBook( + val name: String, + val url: String, + val size: Long, + val contentType: String, + val lastModify: Long +) \ No newline at end of file diff --git a/app/src/main/res/layout/activity_remote_book.xml b/app/src/main/res/layout/activity_remote_book.xml index ed942a2cc..6af80a958 100644 --- a/app/src/main/res/layout/activity_remote_book.xml +++ b/app/src/main/res/layout/activity_remote_book.xml @@ -36,9 +36,11 @@ - + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_remote_book.xml b/app/src/main/res/layout/item_remote_book.xml index 85af79d7f..ad986eaa6 100644 --- a/app/src/main/res/layout/item_remote_book.xml +++ b/app/src/main/res/layout/item_remote_book.xml @@ -55,7 +55,7 @@ android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" - tools:text="\u4e28" + android:text="\u4e28" android:textColor="@color/tv_text_summary" android:textSize="11sp" /> @@ -96,10 +96,9 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="3dp" - android:id="@+id/tv_download" > From ac27c115e408d9dc106b0decc709b9f4bb42808c 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 21:42:40 +0800 Subject: [PATCH 12/12] =?UTF-8?q?feat:=20=E5=8F=98=E6=9B=B4=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=98=BE=E7=A4=BA=E4=B8=BA=20string=20value=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=80=E4=BA=9B=E6=97=A0=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/info/BookInfoActivity.kt | 9 +++---- .../app/ui/book/remote/RemoteBookActivity.kt | 25 ++++++++----------- .../app/ui/book/remote/RemoteBookAdapter.kt | 7 +----- .../app/ui/book/remote/RemoteBookViewModel.kt | 13 ++-------- app/src/main/res/layout/item_remote_book.xml | 5 ++-- app/src/main/res/menu/book_info.xml | 2 +- app/src/main/res/menu/main_bookshelf.xml | 2 +- app/src/main/res/values/strings.xml | 7 ++++++ 8 files changed, 29 insertions(+), 41 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 25a901c78..68ca73cbb 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 @@ -204,12 +204,11 @@ class BookInfoActivity : R.id.menu_upload -> { launch { val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString()) -// val doc = DocumentFile.fromTreeUri(appCtx,uri) + if (RemoteBookWebDav.upload(uri)) + toastOnUi(getString(R.string.upload_book_success)) + else + toastOnUi(getString(R.string.upload_book_fail)) - // if (uri.isContentScheme()){ -// uri.path -// } - RemoteBookWebDav.upload(uri) } } } 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 c9a7f5f56..0ebcd901e 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 @@ -4,12 +4,15 @@ import android.annotation.SuppressLint import android.os.Bundle import androidx.activity.viewModels import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivityRemoteBookBinding +import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch /** @@ -28,42 +31,34 @@ class RemoteBookActivity : VMBaseActivity + viewModel.dataFlow.conflate().collect { remoteBooks -> adapter.setItems(remoteBooks) } + binding.refreshProgressBar.isAutoLoading = false } - - -// toastOnUi("1") - } + private fun onFinally() { + } @SuppressLint("NotifyDataSetChanged") override fun addToBookshelf(remoteBook: RemoteBook) { viewModel.addToBookshelf(remoteBook){ + toastOnUi(getString(R.string.download_book_fail)) adapter.notifyDataSetChanged() } } diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt index 010c15029..36dd38f86 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt @@ -5,12 +5,8 @@ import android.view.ViewGroup import cn.hutool.core.date.LocalDateTimeUtil import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter -import io.legado.app.data.entities.Book import io.legado.app.databinding.ItemRemoteBookBinding import io.legado.app.utils.ConvertUtils -import io.legado.app.utils.FileUtils -import io.legado.app.utils.toastOnUi -import splitties.init.appCtx /** @@ -38,6 +34,7 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : payloads: MutableList ) { binding.run { + //Todo:需要判断书籍是否已经加入书架,来改变“下载”按钮的文本,暂时还没有比较好的方案 tvName.text = item.filename.substringBeforeLast(".") tvContentType.text = item.contentType tvSize.text = ConvertUtils.formatFileSize(item.size) @@ -48,9 +45,7 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : override fun registerListener(holder: ItemViewHolder, binding: ItemRemoteBookBinding) { binding.btnDownload.setOnClickListener { getItem(holder.layoutPosition)?.let { - context.toastOnUi("开始加入") callBack.addToBookshelf(it) - context.toastOnUi("加入成功") } } 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 34493aec3..44f4071b6 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,30 +2,21 @@ package io.legado.app.ui.book.remote import android.app.Application import android.net.Uri -import android.util.Log +import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel -import io.legado.app.constant.PreferKey -import io.legado.app.lib.webdav.Authorization -import io.legado.app.lib.webdav.WebDav import io.legado.app.model.localBook.LocalBook import io.legado.app.ui.book.remote.manager.RemoteBookWebDav -import io.legado.app.utils.FileUtils -import io.legado.app.utils.externalFiles -import io.legado.app.utils.getPrefString import kotlinx.coroutines.* import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOn -import splitties.init.appCtx -import java.io.File -import java.nio.charset.Charset 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 - private var authorization: Authorization? = null val dataFlow = callbackFlow> { diff --git a/app/src/main/res/layout/item_remote_book.xml b/app/src/main/res/layout/item_remote_book.xml index ca84c2e51..ef032b4e3 100644 --- a/app/src/main/res/layout/item_remote_book.xml +++ b/app/src/main/res/layout/item_remote_book.xml @@ -55,7 +55,7 @@ android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" - android:text="丨" + android:text="@string/separator" android:textColor="@color/tv_text_summary" android:textSize="11sp" /> @@ -102,7 +102,8 @@ android:id="@+id/btn_download" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="加入书架"> + tools:text="加入书架" + android:text="@string/nb_file_add_shelf"> diff --git a/app/src/main/res/menu/book_info.xml b/app/src/main/res/menu/book_info.xml index 675763611..80633d3cb 100644 --- a/app/src/main/res/menu/book_info.xml +++ b/app/src/main/res/menu/book_info.xml @@ -16,7 +16,7 @@ Legado needs storage access to find and read books. please go "App Settings" to allow "Storage permission". + Upload Success + Upload Fail + Download Success + Download Fail + + Upload + Add Remote Home Restore Import Legado data