From bce9c17fad1ce777f485ff47ca97ff984d5b3332 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 5 Sep 2019 08:15:05 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/BookSourceDao.kt | 5 +- .../app/ui/booksource/BookSourceActivity.kt | 2 +- .../app/ui/booksource/GroupManageDialog.kt | 2 +- .../io/legado/app/ui/main/MainActivity.kt | 7 +- .../app/ui/main/explore/FindBookAdapter.kt | 91 ++++++++++++++ .../{findbook => explore}/FindBookFragment.kt | 15 +-- .../app/ui/main/findbook/FindBookAdapter.kt | 114 ------------------ .../app/ui/replacerule/GroupManageDialog.kt | 2 +- 8 files changed, 108 insertions(+), 130 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt rename app/src/main/java/io/legado/app/ui/main/{findbook => explore}/FindBookFragment.kt (72%) delete mode 100644 app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt 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 b616d1733..920be8a00 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 @@ -14,8 +14,11 @@ interface BookSourceDao { @Query("select * from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey order by customOrder asc") fun observeSearch(searchKey: String = ""): DataSource.Factory + @Query("select * from book_sources where enabledExplore = 1 order by customOrder asc") + fun liveExplore(): LiveData> + @Query("select bookSourceGroup from book_sources") - fun observeGroup(): LiveData> + fun liveGroup(): LiveData> @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey") fun searchIsEnable(searchKey: String = ""): List diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt index 14a947dd3..0e815e15b 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt @@ -123,7 +123,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity bookSourceLiveDate = LivePagedListBuilder(dataFactory, 10000).build() bookSourceLiveDate?.observe(this, Observer { adapter.submitList(it) }) - App.db.bookSourceDao().observeGroup().observe(this, Observer { + App.db.bookSourceDao().liveGroup().observe(this, Observer { groups.clear() it.map { group -> groups.addAll(group.splitNotBlank(",", ";")) diff --git a/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt index 7ffff5b8f..5d4996fb0 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt @@ -70,7 +70,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { DividerItemDecoration(requireContext(), RecyclerView.VERTICAL) ) recycler_view.adapter = adapter - App.db.bookSourceDao().observeGroup().observe(viewLifecycleOwner, Observer { + App.db.bookSourceDao().liveGroup().observe(viewLifecycleOwner, Observer { val groups = linkedSetOf() it.map { group -> groups.addAll(group.splitNotBlank(",", ";")) diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index db04c42f2..6fc769e01 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -15,10 +15,13 @@ import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.theme.ATH import io.legado.app.ui.main.bookshelf.BookshelfFragment -import io.legado.app.ui.main.findbook.FindBookFragment +import io.legado.app.ui.main.explore.FindBookFragment import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.rss.RssFragment -import io.legado.app.utils.* +import io.legado.app.utils.getPrefInt +import io.legado.app.utils.getViewModel +import io.legado.app.utils.observeEvent +import io.legado.app.utils.putPrefInt import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch diff --git a/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt new file mode 100644 index 000000000..0148b88f9 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt @@ -0,0 +1,91 @@ +package io.legado.app.ui.main.explore + +import android.content.Context +import android.graphics.drawable.GradientDrawable +import android.view.LayoutInflater +import android.widget.GridLayout +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.BookSource +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.lib.theme.ColorUtils +import io.legado.app.lib.theme.accentColor +import io.legado.app.utils.gone +import io.legado.app.utils.visible +import kotlinx.android.synthetic.main.item_find_book.view.* +import kotlinx.android.synthetic.main.item_text.view.* +import kotlinx.coroutines.CoroutineScope +import org.jetbrains.anko.sdk27.listeners.onClick + + +class FindBookAdapter(context: Context, private val scope: CoroutineScope, val callBack: CallBack) : + SimpleRecyclerAdapter(context, R.layout.item_find_book) { + + var exIndex = 0 + + override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList) { + with(holder.itemView) { + val bgShape: GradientDrawable? = tv_name.background as? GradientDrawable + bgShape?.setStroke(2, ColorUtils.getRandomColor()) + tv_name.text = item.bookSourceName + ll_title.onClick { + val oldEx = exIndex + if (exIndex == holder.layoutPosition) { + exIndex = -1 + } else { + exIndex = holder.layoutPosition + notifyItemChanged(holder.layoutPosition) + } + notifyItemChanged(oldEx) + callBack.scrollTo(holder.layoutPosition) + } + if (exIndex == holder.layoutPosition) { + rotate_loading.loadingColor = context.accentColor + rotate_loading.show() + Coroutine.async(scope) { + item.getExploreRule().getExploreKinds(item.bookSourceUrl) + }.onSuccess { + it?.let { + gl_child.visible() + var rowNum = 0 + var columnNum = 0 + gl_child.removeAllViews() + it.map { kind -> + val tv = LayoutInflater.from(context) + .inflate(R.layout.item_text, gl_child, false) + tv.text_view.text = kind.title + tv.text_view.onClick { + callBack.openExplore( + item.bookSourceUrl, + kind.title, + kind.url + ) + } + val rowSpecs = GridLayout.spec(rowNum, 1.0f) + val colSpecs = GridLayout.spec(columnNum, 1.0f) + val params = GridLayout.LayoutParams(rowSpecs, colSpecs) + gl_child.addView(tv, params) + if (columnNum < 2) { + columnNum++ + } else { + columnNum = 0 + rowNum++ + } + } + } + }.onFinally { + rotate_loading.hide() + } + } else { + rotate_loading.hide() + gl_child.gone() + } + } + } + + interface CallBack { + fun scrollTo(pos: Int) + fun openExplore(sourceUrl: String, title: String, exploreUrl: String) + } +} \ 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/explore/FindBookFragment.kt similarity index 72% rename from app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt rename to app/src/main/java/io/legado/app/ui/main/explore/FindBookFragment.kt index 75988f74b..76ed36885 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/FindBookFragment.kt @@ -1,17 +1,13 @@ -package io.legado.app.ui.main.findbook +package io.legado.app.ui.main.explore 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.BookSource import io.legado.app.lib.theme.ATH import io.legado.app.ui.explore.ExploreShowActivity import io.legado.app.utils.startActivity @@ -22,7 +18,6 @@ class FindBookFragment : BaseFragment(R.layout.fragment_find_book), FindBookAdapter.CallBack { private lateinit var adapter: FindBookAdapter - private var findLiveData: LiveData>? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -37,14 +32,14 @@ class FindBookFragment : BaseFragment(R.layout.fragment_find_book), private fun initRecyclerView() { ATH.applyEdgeEffectColor(rv_find) rv_find.layoutManager = LinearLayoutManager(context) - adapter = FindBookAdapter(this, this) + adapter = FindBookAdapter(requireContext(), this, this) rv_find.adapter = adapter } private fun initData() { - findLiveData?.removeObservers(viewLifecycleOwner) - findLiveData = LivePagedListBuilder(App.db.bookSourceDao().observeFind(), 2000).build() - findLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) + App.db.bookSourceDao().liveExplore().observe(viewLifecycleOwner, Observer { + adapter.setItems(it) + }) } override fun scrollTo(pos: Int) { 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 deleted file mode 100644 index e85f451a9..000000000 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt +++ /dev/null @@ -1,114 +0,0 @@ -package io.legado.app.ui.main.findbook - -import android.graphics.drawable.GradientDrawable -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.GridLayout -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 io.legado.app.help.coroutine.Coroutine -import io.legado.app.lib.theme.ColorUtils -import io.legado.app.utils.gone -import io.legado.app.utils.visible -import kotlinx.android.synthetic.main.item_find_book.view.* -import kotlinx.android.synthetic.main.item_text.view.* -import kotlinx.coroutines.CoroutineScope -import org.jetbrains.anko.sdk27.listeners.onClick - - -class FindBookAdapter(private val scope: CoroutineScope, val callBack: CallBack) : - PagedListAdapter(DIFF_CALLBACK) { - - var exIndex = 0 - - companion object { - var DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = - oldItem.bookSourceUrl == newItem.bookSourceUrl - - override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = - oldItem.bookSourceUrl == newItem.bookSourceUrl - && oldItem.bookSourceName == newItem.bookSourceName - } - } - - 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): Unit = - with(holder.itemView) { - currentList?.get(position)?.let { bookSource -> - val bgShape: GradientDrawable? = tv_name.background as? GradientDrawable - bgShape?.setStroke(2, ColorUtils.getRandomColor()) - tv_name.text = bookSource.bookSourceName - ll_title.onClick { - val oldEx = exIndex - if (exIndex == position) { - exIndex = -1 - } else { - exIndex = position - notifyItemChanged(position) - } - notifyItemChanged(oldEx) - callBack.scrollTo(position) - } - if (exIndex == position) { - rotate_loading.show() - Coroutine.async(scope) { - bookSource.getExploreRule().getExploreKinds(bookSource.bookSourceUrl) - }.onSuccess { - it?.let { - gl_child.visible() - var rowNum = 0 - var columnNum = 0 - gl_child.removeAllViews() - it.map { kind -> - val tv = LayoutInflater.from(context) - .inflate(R.layout.item_text, gl_child, false) - tv.text_view.text = kind.title - tv.text_view.onClick { - callBack.openExplore( - bookSource.bookSourceUrl, - kind.title, - kind.url - ) - } - val rowSpecs = GridLayout.spec(rowNum, 1.0f) - val colSpecs = GridLayout.spec(columnNum, 1.0f) - val params = GridLayout.LayoutParams(rowSpecs, colSpecs) - gl_child.addView(tv, params) - if (columnNum < 2) { - columnNum++ - } else { - columnNum = 0 - rowNum++ - } - } - } - }.onFinally { - rotate_loading.hide() - } - } else { - rotate_loading.hide() - gl_child.gone() - } - } - } - - class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) - - interface CallBack { - - fun scrollTo(pos: Int) - - fun openExplore(sourceUrl: String, title: String, exploreUrl: String) - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt index fc6886bf8..03e76b6c6 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt @@ -70,7 +70,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { DividerItemDecoration(requireContext(), RecyclerView.VERTICAL) ) recycler_view.adapter = adapter - App.db.bookSourceDao().observeGroup().observe(viewLifecycleOwner, Observer { + App.db.bookSourceDao().liveGroup().observe(viewLifecycleOwner, Observer { val groups = linkedSetOf() it.map { group -> groups.addAll(group.splitNotBlank(",", ";"))