From 792a658b7e376eec8d562685ee04c0b96a69e191 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 19 Jun 2019 21:00:45 +0800 Subject: [PATCH] up --- app/build.gradle | 3 +- .../io/legado/app/data/dao/BookSourceDao.kt | 3 ++ .../app/ui/main/findbook/FindBookAdapter.kt | 42 +++++++++++++++++++ .../app/ui/main/findbook/FindBookFragment.kt | 26 ++++++++++++ .../main/res/layout/fragment_find_book.xml | 4 ++ app/src/main/res/layout/item_find_book.xml | 6 +++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt create mode 100644 app/src/main/res/layout/item_find_book.xml diff --git a/app/build.gradle b/app/build.gradle index 01d15e189..819a06b2e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index a60ed4dca..99f2c64c5 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -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 + @Query("select * from book_sources where exploreIsEnabled = 1 order by customOrder asc") + fun observeFind(): DataSource.Factory + @Query("select * from book_sources where origin = :key") fun findByKey(key: String): BookSource? diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt new file mode 100644 index 000000000..59bca7a84 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt @@ -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(DIFF_CALLBACK) { + + companion object { + var DIFF_CALLBACK = object : DiffUtil.ItemCallback() { + 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 + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt index 7a1804151..472d0a733 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt @@ -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>? = 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) }) + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_find_book.xml b/app/src/main/res/layout/fragment_find_book.xml index e7f667141..4ea053240 100644 --- a/app/src/main/res/layout/fragment_find_book.xml +++ b/app/src/main/res/layout/fragment_find_book.xml @@ -13,5 +13,9 @@ app:layout_constraintTop_toTopOf="parent" app:title="@string/find" /> + \ No newline at end of file diff --git a/app/src/main/res/layout/item_find_book.xml b/app/src/main/res/layout/item_find_book.xml new file mode 100644 index 000000000..76978eb04 --- /dev/null +++ b/app/src/main/res/layout/item_find_book.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file