kunfei 4 лет назад
Родитель
Сommit
9d3e9eefbd

+ 5 - 0
app/src/main/java/io/legado/app/ui/association/AddToBookshelfDialog.kt

@@ -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
+        }
 
     }
 

+ 7 - 0
app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt

@@ -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)

+ 30 - 1
app/src/main/java/io/legado/app/ui/book/explore/ExploreShowAdapter.kt

@@ -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)
     }
 }

+ 18 - 1
app/src/main/java/io/legado/app/ui/book/explore/ExploreShowViewModel.kt

@@ -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")

+ 17 - 1
app/src/main/res/layout/dialog_add_to_bookshelf.xml

@@ -1,6 +1,22 @@
 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+
+    <androidx.appcompat.widget.Toolbar
+        android:id="@+id/tool_bar"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="@color/background_menu"
+        android:elevation="5dp"
+        android:theme="?attr/actionBarStyle"
+        app:displayHomeAsUp="false"
+        app:fitStatusBar="false"
+        app:layout_constraintTop_toTopOf="parent"
+        app:popupTheme="@style/AppTheme.PopupOverlay"
+        app:title="@string/add_book_url"
+        app:titleTextAppearance="@style/ToolbarTitle" />
+
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 1 - 0
app/src/main/res/layout/item_search.xml

@@ -26,6 +26,7 @@
         android:layout_margin="8dp"
         android:scaleType="centerCrop"
         android:src="@color/md_green_600"
+        android:visibility="invisible"
         app:layout_constraintRight_toRightOf="@id/iv_cover"
         app:layout_constraintTop_toTopOf="@id/iv_cover" />