pull/32/head
gedoor 6 years ago
parent c31fc00744
commit 792a658b7e
  1. 3
      app/build.gradle
  2. 3
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  3. 42
      app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt
  4. 26
      app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt
  5. 4
      app/src/main/res/layout/fragment_find_book.xml
  6. 6
      app/src/main/res/layout/item_find_book.xml

@ -79,11 +79,12 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
//androidX
implementation 'androidx.core:core-ktx:1.2.0-alpha01'
implementation 'androidx.core:core-ktx:1.2.0-alpha02'
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'androidx.preference:preference:1.1.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'com.google.android:flexbox:1.1.0'
//lifecycle
def lifecycle_version = '2.0.0'

@ -13,6 +13,9 @@ interface BookSourceDao {
@Query("select * from book_sources where name like :searchKey or `group` like :searchKey or origin like :searchKey order by customOrder asc")
fun observeSearch(searchKey: String = ""): DataSource.Factory<Int, BookSource>
@Query("select * from book_sources where exploreIsEnabled = 1 order by customOrder asc")
fun observeFind(): DataSource.Factory<Int, BookSource>
@Query("select * from book_sources where origin = :key")
fun findByKey(key: String): BookSource?

@ -0,0 +1,42 @@
package io.legado.app.ui.main.findbook
import android.view.LayoutInflater
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.data.entities.BookSource
import kotlinx.android.synthetic.main.item_find_book.view.*
class FindBookAdapter:PagedListAdapter<BookSource, FindBookAdapter.MyViewHolder>(DIFF_CALLBACK) {
companion object {
var DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() {
override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.origin == newItem.origin
override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean =
oldItem.origin == newItem.origin
&& oldItem.name == newItem.name
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_find_book, parent, false))
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
currentList?.get(position)?.let {
holder.bind(it)
}
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(bookSource: BookSource) = with(itemView) {
tv_name.text = bookSource.name
}
}
}

@ -3,18 +3,44 @@ package io.legado.app.ui.main.findbook
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 com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexboxLayoutManager
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.BookSource
import kotlinx.android.synthetic.main.fragment_find_book.*
import kotlinx.android.synthetic.main.view_title_bar.*
class FindBookFragment : BaseFragment(R.layout.fragment_find_book) {
private lateinit var adapter: FindBookAdapter
private var findLiveData:LiveData<PagedList<BookSource>>? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar)
initRecyclerView()
initData()
}
override fun onCompatCreateOptionsMenu(menu: Menu) {
menuInflater.inflate(R.menu.find_book, menu)
}
private fun initRecyclerView() {
rv_find.layoutManager = FlexboxLayoutManager(context)
adapter = FindBookAdapter()
rv_find.adapter = adapter
}
private fun initData() {
findLiveData?.removeObservers(viewLifecycleOwner)
findLiveData = LivePagedListBuilder(App.db.bookSourceDao().observeFind(), 10).build()
findLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
}
}

@ -13,5 +13,9 @@
app:layout_constraintTop_toTopOf="parent"
app:title="@string/find" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_find"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_name"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Loading…
Cancel
Save