From 20b1fd907727fa12835915cb708b32539c0f72b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Fri, 13 May 2022 22:19:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=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