pull/32/head
kunfei 5 years ago
parent eda339e599
commit bce9c17fad
  1. 5
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 2
      app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/booksource/GroupManageDialog.kt
  4. 7
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  5. 91
      app/src/main/java/io/legado/app/ui/main/explore/FindBookAdapter.kt
  6. 15
      app/src/main/java/io/legado/app/ui/main/explore/FindBookFragment.kt
  7. 114
      app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt
  8. 2
      app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.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<Int, BookSource>
@Query("select * from book_sources where enabledExplore = 1 order by customOrder asc")
fun liveExplore(): LiveData<List<BookSource>>
@Query("select bookSourceGroup from book_sources")
fun observeGroup(): LiveData<List<String>>
fun liveGroup(): LiveData<List<String>>
@Query("select distinct enabled from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey")
fun searchIsEnable(searchKey: String = ""): List<Boolean>

@ -123,7 +123,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(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(",", ";"))

@ -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<String>()
it.map { group ->
groups.addAll(group.splitNotBlank(",", ";"))

@ -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

@ -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<BookSource>(context, R.layout.item_find_book) {
var exIndex = 0
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) {
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)
}
}

@ -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<PagedList<BookSource>>? = 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) {

@ -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<BookSource, FindBookAdapter.MyViewHolder>(DIFF_CALLBACK) {
var exIndex = 0
companion object {
var DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() {
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)
}
}

@ -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<String>()
it.map { group ->
groups.addAll(group.splitNotBlank(",", ";"))

Loading…
Cancel
Save