pull/35/head
kunfei 5 years ago
parent 3ebb83a6af
commit a46886a833
  1. 4
      app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt
  2. 64
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt
  3. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/BooksFragment.kt
  4. 21
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt

@ -1,6 +1,6 @@
package io.legado.app.data.dao package io.legado.app.data.dao
import androidx.paging.DataSource import androidx.lifecycle.LiveData
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
@ -11,7 +11,7 @@ import io.legado.app.data.entities.BookGroup
interface BookGroupDao { interface BookGroupDao {
@Query("SELECT * FROM book_groups ORDER BY `order`") @Query("SELECT * FROM book_groups ORDER BY `order`")
fun observeAll(): DataSource.Factory<Int, BookGroup> fun liveDataAll(): LiveData<List<BookGroup>>
@get:Query("SELECT MAX(groupId) FROM book_groups") @get:Query("SELECT MAX(groupId) FROM book_groups")
val maxId: Int val maxId: Int

@ -1,65 +1,19 @@
package io.legado.app.ui.main.bookshelf package io.legado.app.ui.main.bookshelf
import android.view.LayoutInflater import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.help.AdapterDataObserverProxy
import kotlinx.android.synthetic.main.item_book_group.view.* import kotlinx.android.synthetic.main.item_book_group.view.*
class BookGroupAdapter : PagedListAdapter<BookGroup, BookGroupAdapter.MyViewHolder>(DIFF_CALLBACK) { class BookGroupAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<BookGroup>(context, R.layout.item_book_group) {
companion object { override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) {
@JvmField with(holder.itemView) {
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookGroup>() { tv_group.text = item.groupName
override fun areItemsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean = tv_group.setOnClickListener { callBack.open(item) }
oldItem.groupId == newItem.groupId
override fun areContentsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean =
oldItem.groupId == newItem.groupId
&& oldItem.groupName == newItem.groupName
&& oldItem.order == newItem.order
}
}
var callBack: CallBack? = null
private val defaultGroups = arrayOf(BookGroup(-1, "全部"),
BookGroup(-2, "本地"),
BookGroup(-3, "音频"))
private val addBookGroup = BookGroup(-10, "+")
override fun getItemCount(): Int {
return super.getItemCount() + defaultGroups.size + 1
}
override fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) {
super.registerAdapterDataObserver(AdapterDataObserverProxy(observer, defaultGroups.size))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_book_group, parent, false))
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
when {
position < defaultGroups.size -> holder.bind(defaultGroups[position], callBack)
position == itemCount - 1 -> holder.bind(addBookGroup, callBack)
else -> currentList?.get(position - defaultGroups.size)?.let {
holder.bind(it, callBack)
}
}
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(bookGroup: BookGroup, callBack: CallBack?) = with(itemView) {
tv_group.text = bookGroup.groupName
tv_group.setOnClickListener { callBack?.open(bookGroup) }
} }
} }

@ -42,10 +42,14 @@ class BooksFragment : VMBaseFragment<BooksViewModel>(R.layout.fragment_books),
private lateinit var activityViewModel: MainViewModel private lateinit var activityViewModel: MainViewModel
private lateinit var booksAdapter: BooksAdapter private lateinit var booksAdapter: BooksAdapter
private var bookshelfLiveData: LiveData<PagedList<Book>>? = null private var bookshelfLiveData: LiveData<PagedList<Book>>? = null
private val groupId = -1 private var groupId = -1
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
activityViewModel = getViewModelOfActivity(MainViewModel::class.java) activityViewModel = getViewModelOfActivity(MainViewModel::class.java)
arguments?.let {
val position = it.getInt("position", 0)
}
initRecyclerView() initRecyclerView()
upRecyclerData() upRecyclerData()
} }

@ -9,8 +9,6 @@ import android.widget.EditText
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
import io.legado.app.App import io.legado.app.App
@ -36,7 +34,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
private lateinit var booksAdapter: BooksAdapter private lateinit var booksAdapter: BooksAdapter
private lateinit var bookGroupAdapter: BookGroupAdapter private lateinit var bookGroupAdapter: BookGroupAdapter
private var bookGroupLiveData: LiveData<PagedList<BookGroup>>? = null private var bookGroupLiveData: LiveData<List<BookGroup>>? = null
private var position = -1 private var position = -1
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -61,9 +59,8 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
ATH.applyEdgeEffectColor(view_pager_bookshelf) ATH.applyEdgeEffectColor(view_pager_bookshelf)
rv_book_group.layoutManager = rv_book_group.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
bookGroupAdapter = BookGroupAdapter() bookGroupAdapter = BookGroupAdapter(requireContext(), this)
rv_book_group.adapter = bookGroupAdapter rv_book_group.adapter = bookGroupAdapter
bookGroupAdapter.callBack = this
view_pager_bookshelf.adapter = BookshelfAdapter(this) view_pager_bookshelf.adapter = BookshelfAdapter(this)
view_pager_bookshelf.registerOnPageChangeCallback(object : view_pager_bookshelf.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() { ViewPager2.OnPageChangeCallback() {
@ -77,8 +74,18 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
private fun initBookGroupData() { private fun initBookGroupData() {
bookGroupLiveData?.removeObservers(viewLifecycleOwner) bookGroupLiveData?.removeObservers(viewLifecycleOwner)
bookGroupLiveData = LivePagedListBuilder(App.db.bookGroupDao().observeAll(), 10).build() bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, Observer { bookGroupAdapter.submitList(it) }) bookGroupLiveData?.observe(viewLifecycleOwner, Observer {
mutableListOf(
BookGroup(-1, "全部"),
BookGroup(-2, "本地"),
BookGroup(-3, "音频")
).apply {
addAll(it)
}.let {
bookGroupAdapter.setItems(it)
}
})
} }
override fun open(bookGroup: BookGroup) { override fun open(bookGroup: BookGroup) {

Loading…
Cancel
Save