This commit is contained in:
kunfei
2022-11-01 20:36:37 +08:00
parent 9674abd794
commit 9d3e9eefbd
6 changed files with 78 additions and 3 deletions
@@ -30,6 +30,11 @@ class AddToBookshelfDialog() : BaseDialogFragment(R.layout.dialog_add_to_bookshe
}
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
val bookUrl = arguments?.getString("bookUrl")
if (bookUrl.isNullOrBlank()) {
return
}
}
@@ -31,6 +31,9 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
viewModel.errorLiveData.observe(this) {
loadMoreView.error(it)
}
viewModel.upAdapterLiveData.observe(this) {
adapter.notifyItemRangeChanged(0, adapter.itemCount, it)
}
}
private fun initRecyclerView() {
@@ -80,6 +83,10 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
}
}
override fun isInBookshelf(name: String, author: String): Boolean {
return viewModel.bookshelf.contains("$name-$author")
}
override fun showBookInfo(book: Book) {
startActivity<BookInfoActivity> {
putExtra("name", book.name)
@@ -1,7 +1,9 @@
package io.legado.app.ui.book.explore
import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import androidx.core.view.isVisible
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
@@ -26,9 +28,20 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
item: SearchBook,
payloads: MutableList<Any>
) {
binding.apply {
val bundle = payloads.getOrNull(0) as? Bundle
if (bundle == null) {
bind(binding, item)
} else {
bindChange(binding, item, bundle)
}
}
private fun bind(binding: ItemSearchBinding, item: SearchBook) {
binding.run {
tvName.text = item.name
tvAuthor.text = context.getString(R.string.author_show, item.author)
ivInBookshelf.isVisible = callBack.isInBookshelf(item.name, item.author)
if (item.latestChapterTitle.isNullOrEmpty()) {
tvLasted.gone()
} else {
@@ -53,6 +66,17 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
}
}
private fun bindChange(binding: ItemSearchBinding, item: SearchBook, bundle: Bundle) {
binding.run {
bundle.keySet().forEach {
when (it) {
"isInBookshelf" -> ivInBookshelf.isVisible =
callBack.isInBookshelf(item.name, item.author)
}
}
}
}
override fun registerListener(holder: ItemViewHolder, binding: ItemSearchBinding) {
holder.itemView.setOnClickListener {
getItem(holder.layoutPosition)?.let {
@@ -62,6 +86,11 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
}
interface CallBack {
/**
* 是否已经加入书架
*/
fun isInBookshelf(name: String, author: String): Boolean
fun showBookInfo(book: Book)
}
}
@@ -13,16 +13,33 @@ import io.legado.app.utils.printOnDebug
import io.legado.app.utils.stackTraceStr
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.launch
@OptIn(ExperimentalCoroutinesApi::class)
class ExploreShowViewModel(application: Application) : BaseViewModel(application) {
val bookshelf = hashSetOf<String>()
val upAdapterLiveData = MutableLiveData<String>()
val booksData = MutableLiveData<List<SearchBook>>()
val errorLiveData = MutableLiveData<String>()
private var bookSource: BookSource? = null
private var exploreUrl: String? = null
private var page = 1
init {
viewModelScope.launch {
appDb.bookDao.flowAll().mapLatest { books ->
books.map { "${it.name}-${it.author}" }
}.collect {
bookshelf.clear()
bookshelf.addAll(it)
upAdapterLiveData.postValue("isInBookshelf")
}
}
}
fun initData(intent: Intent) {
execute {
val sourceUrl = intent.getStringExtra("sourceUrl")