diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 60501adb2..15ab3e0c0 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -7,6 +7,7 @@ import androidx.room.RoomDatabase import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import io.legado.app.data.dao.BookDao +import io.legado.app.data.dao.BookGroupDao import io.legado.app.data.dao.BookSourceDao import io.legado.app.data.dao.ReplaceRuleDao import io.legado.app.data.entities.Book @@ -49,6 +50,8 @@ abstract class AppDatabase : RoomDatabase() { } abstract fun bookDao(): BookDao - abstract fun replaceRuleDao(): ReplaceRuleDao + abstract fun bookGroupDao(): BookGroupDao abstract fun bookSourceDao(): BookSourceDao + abstract fun replaceRuleDao(): ReplaceRuleDao + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 2a1f741ad..3fabd852e 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -27,7 +27,7 @@ interface BookDao { val allBookCount: Int @Query("SELECT * FROM books ORDER BY durChapterTime DESC limit 0,10") - fun recentRead(): List + fun recentRead(): DataSource.Factory @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg books: Book) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt new file mode 100644 index 000000000..04a7dfe28 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt @@ -0,0 +1,36 @@ +package io.legado.app.ui.main.bookshelf + +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.data.entities.BookGroup + +class BookGroupAdapter : PagedListAdapter(DIFF_CALLBACK) { + + companion object { + @JvmField + val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean = + oldItem.groupId == newItem.groupId + + override fun areContentsTheSame(oldItem: BookGroup, newItem: BookGroup): Boolean = + oldItem.groupId == newItem.groupId + && oldItem.groupName == newItem.groupName + && oldItem.order == newItem.order + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun onBindViewHolder(holder: MyViewHolder, position: Int) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index b0705383e..48160f347 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -3,19 +3,31 @@ package io.legado.app.ui.main.bookshelf import android.os.Bundle import android.view.Menu import android.view.View +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.paging.LivePagedListBuilder +import androidx.paging.PagedList import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookGroup import kotlinx.android.synthetic.main.fragment_bookshelf.* import kotlinx.android.synthetic.main.view_title_bar.* class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) { private lateinit var recentReadAdapter: RecentReadAdapter + private lateinit var bookGroupAdapter: BookGroupAdapter + private var bookGroupLiveData: LiveData>? = null + private var recentReadLiveData: LiveData>? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) initRecyclerView() + initBookGroupData() + initRecentReadData() } override fun onCompatCreateOptionsMenu(menu: Menu) { @@ -24,9 +36,23 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) { private fun initRecyclerView() { rv_bookshelf.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) + bookGroupAdapter = BookGroupAdapter() + rv_bookshelf.adapter = bookGroupAdapter rv_read_books.layoutManager = LinearLayoutManager(context) recentReadAdapter = RecentReadAdapter() rv_read_books.adapter = recentReadAdapter } + private fun initBookGroupData() { + bookGroupLiveData?.removeObservers(viewLifecycleOwner) + bookGroupLiveData = LivePagedListBuilder(App.db.bookGroupDao().observeAll(), 10).build() + bookGroupLiveData?.observe(viewLifecycleOwner, Observer { bookGroupAdapter.submitList(it) }) + } + + private fun initRecentReadData() { + recentReadLiveData?.removeObservers(viewLifecycleOwner) + recentReadLiveData = LivePagedListBuilder(App.db.bookDao().recentRead(), 20).build() + recentReadLiveData?.observe(viewLifecycleOwner, Observer { recentReadAdapter.submitList(it) }) + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index de7e9e7e6..0816a2f4a 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -33,7 +33,7 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) initRecyclerView() - initDataObservers() + initDataObserve() initSearchView() } @@ -69,7 +69,7 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour search_view.setOnQueryTextListener(this) } - private fun initDataObservers(searchKey: String = "") { + private fun initDataObserve(searchKey: String = "") { bookSourceLiveDate?.removeObservers(viewLifecycleOwner) val dataFactory = if (searchKey.isEmpty()) App.db.bookSourceDao().observeAll() else App.db.bookSourceDao().observeSearch( @@ -80,7 +80,7 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour } override fun onQueryTextChange(newText: String?): Boolean { - newText?.let { initDataObservers("%$it%") } + newText?.let { initDataObserve("%$it%") } return false }