pull/32/head
gedoor 6 years ago
parent 8206131f1f
commit 9b8176ed17
  1. 5
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 2
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  3. 36
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookGroupAdapter.kt
  4. 26
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  5. 6
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt

@ -7,6 +7,7 @@ import androidx.room.RoomDatabase
import androidx.room.migration.Migration import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import io.legado.app.data.dao.BookDao 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.BookSourceDao
import io.legado.app.data.dao.ReplaceRuleDao import io.legado.app.data.dao.ReplaceRuleDao
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -49,6 +50,8 @@ abstract class AppDatabase : RoomDatabase() {
} }
abstract fun bookDao(): BookDao abstract fun bookDao(): BookDao
abstract fun replaceRuleDao(): ReplaceRuleDao abstract fun bookGroupDao(): BookGroupDao
abstract fun bookSourceDao(): BookSourceDao abstract fun bookSourceDao(): BookSourceDao
abstract fun replaceRuleDao(): ReplaceRuleDao
} }

@ -27,7 +27,7 @@ interface BookDao {
val allBookCount: Int val allBookCount: Int
@Query("SELECT * FROM books ORDER BY durChapterTime DESC limit 0,10") @Query("SELECT * FROM books ORDER BY durChapterTime DESC limit 0,10")
fun recentRead(): List<Book> fun recentRead(): DataSource.Factory<Int, Book>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg books: Book) fun insert(vararg books: Book)

@ -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<BookGroup, BookGroupAdapter.MyViewHolder>(DIFF_CALLBACK) {
companion object {
@JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookGroup>() {
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) {
}
}

@ -3,19 +3,31 @@ package io.legado.app.ui.main.bookshelf
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.View 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 androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseFragment 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.fragment_bookshelf.*
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) { class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) {
private lateinit var recentReadAdapter: RecentReadAdapter private lateinit var recentReadAdapter: RecentReadAdapter
private lateinit var bookGroupAdapter: BookGroupAdapter
private var bookGroupLiveData: LiveData<PagedList<BookGroup>>? = null
private var recentReadLiveData: LiveData<PagedList<Book>>? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar) setSupportToolbar(toolbar)
initRecyclerView() initRecyclerView()
initBookGroupData()
initRecentReadData()
} }
override fun onCompatCreateOptionsMenu(menu: Menu) { override fun onCompatCreateOptionsMenu(menu: Menu) {
@ -24,9 +36,23 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) {
private fun initRecyclerView() { private fun initRecyclerView() {
rv_bookshelf.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) rv_bookshelf.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
bookGroupAdapter = BookGroupAdapter()
rv_bookshelf.adapter = bookGroupAdapter
rv_read_books.layoutManager = LinearLayoutManager(context) rv_read_books.layoutManager = LinearLayoutManager(context)
recentReadAdapter = RecentReadAdapter() recentReadAdapter = RecentReadAdapter()
rv_read_books.adapter = 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) })
}
} }

@ -33,7 +33,7 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar) setSupportToolbar(toolbar)
initRecyclerView() initRecyclerView()
initDataObservers() initDataObserve()
initSearchView() initSearchView()
} }
@ -69,7 +69,7 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour
search_view.setOnQueryTextListener(this) search_view.setOnQueryTextListener(this)
} }
private fun initDataObservers(searchKey: String = "") { private fun initDataObserve(searchKey: String = "") {
bookSourceLiveDate?.removeObservers(viewLifecycleOwner) bookSourceLiveDate?.removeObservers(viewLifecycleOwner)
val dataFactory = val dataFactory =
if (searchKey.isEmpty()) App.db.bookSourceDao().observeAll() else App.db.bookSourceDao().observeSearch( 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 { override fun onQueryTextChange(newText: String?): Boolean {
newText?.let { initDataObservers("%$it%") } newText?.let { initDataObserve("%$it%") }
return false return false
} }

Loading…
Cancel
Save