feat: 调整视图与数据分离
This commit is contained in:
@@ -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()
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user