feat: 调整视图与数据分离

pull/1894/head
谢意帆 3 years ago
parent 4ecaa4e2bf
commit 20b1fd9077
  1. 37
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt
  2. 43
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt
  3. 61
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt
  4. 20
      app/src/main/res/layout/activity_remote_book.xml
  5. 91
      app/src/main/res/layout/item_remote_book.xml

@ -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<ActivityRemoteBookBinding,RemoteBookViewModel>() {
class RemoteBookActivity : VMBaseActivity<ActivityRemoteBookBinding,RemoteBookViewModel>(),
RemoteBookAdapter.CallBack {
override val binding by viewBinding(ActivityRemoteBookBinding::inflate)
override val viewModel by viewModels<RemoteBookViewModel>()
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<ActivityRemoteBookBinding,RemoteBookVi
// binding.selectActionBar.setOnMenuItemClickListener(this)
// binding.selectActionBar.setCallBack(this)
}
private fun initData() {
// viewModel.getRemoteBooks().observe(this, {
// adapter.submitList(it)
// })
launch {
Log.e("TAG", "2")
viewModel.dataFlow.conflate().collect { remoteBooks ->
adapter.setItems(remoteBooks)
}
}
// toastOnUi("1")
viewModel.loadRemoteBookList()
}
override fun showRemoteBookInfo(book: Book) {
TODO("Not yet implemented")
}
}

@ -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<String, ItemRemoteBookBinding>(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<Any>
) {
binding.run {
tvName.text = item
}
}
override fun registerListener(holder: ItemViewHolder, binding: ItemRemoteBookBinding) {
}
interface CallBack {
fun showRemoteBookInfo(book: Book)
}
}

@ -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<List<String>> {
val list = Collections.synchronizedList(ArrayList<String>())
dataCallback = object : DataCallback {
override fun setItems(remoteFiles: List<String>) {
list.clear()
list.addAll(remoteFiles)
Log.e("TAG", ": 1", )
trySend(list)
}
override fun addItems(remoteFiles: List<String>) {
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<String>)
fun addItems(remoteFiles: List<String>)
fun clear()
}
}

@ -16,10 +16,24 @@
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="2dp"-->
<!-- app:layout_constraintTop_toBottomOf="@id/lay_top" />-->
<io.legado.app.ui.widget.anima.RefreshProgressBar
android:id="@+id/refresh_progress_bar"
android:layout_width="match_parent"
android:layout_height="2dp"
app:layout_constraintTop_toBottomOf="@id/titleBar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
<io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/refresh_progress_bar">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<io.legado.app.ui.widget.image.CoverImageView
android:id="@+id/iv_cover"
android:layout_width="80dp"
android:layout_height="110dp"
android:layout_margin="8dp"
android:contentDescription="@string/img_cover"
android:scaleType="centerCrop"
android:src="@drawable/image_cover_default"
android:transitionName="img_cover"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="UnusedAttribute" />
<io.legado.app.ui.widget.text.BadgeView
android:id="@+id/bv_originCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:singleLine="true"
android:text="@string/app_name"
android:textColor="@color/primaryText"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@id/bv_originCount"
app:layout_constraintStart_toEndOf="@+id/iv_cover"
app:layout_constraintTop_toTopOf="parent" />
<!-- <LinearLayout-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="0dp"-->
<!-- android:orientation="vertical"-->
<!-- android:layout_marginTop="3dp"-->
<!-- app:layout_constraintBottom_toBottomOf="@id/iv_cover"-->
<!-- app:layout_constraintLeft_toLeftOf="@+id/tv_name"-->
<!-- app:layout_constraintRight_toRightOf="@id/tv_name"-->
<!-- app:layout_constraintTop_toBottomOf="@+id/tv_name">-->
<!-- <TextView-->
<!-- android:id="@+id/tv_author"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:ellipsize="end"-->
<!-- android:lines="1"-->
<!-- android:text="@string/author"-->
<!-- android:textColor="@color/primaryText"-->
<!-- android:textSize="12sp" />-->
<!-- <io.legado.app.ui.widget.LabelsBar-->
<!-- android:id="@+id/ll_kind"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:orientation="horizontal" />-->
<!-- <TextView-->
<!-- android:id="@+id/tv_lasted"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:ellipsize="end"-->
<!-- android:lines="1"-->
<!-- android:text="@string/last_read"-->
<!-- android:textColor="@color/primaryText"-->
<!-- android:textSize="12sp" />-->
<!-- <io.legado.app.ui.widget.text.MultilineTextView-->
<!-- android:id="@+id/tv_introduce"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:ellipsize="end"-->
<!-- android:text="@string/book_intro"-->
<!-- android:textColor="@color/primaryText"-->
<!-- android:textSize="12sp" />-->
<!-- </LinearLayout>-->
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save