pull/32/head
kunfei 5 years ago
parent d194145c8e
commit c0fa0821a1
  1. 4
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 38
      app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt
  3. 97
      app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt
  4. 10
      app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt
  5. 43
      app/src/main/java/io/legado/app/ui/booksource/DiffCallBack.kt

@ -9,10 +9,10 @@ import io.legado.app.data.entities.BookSource
interface BookSourceDao { interface BookSourceDao {
@Query("select * from book_sources order by customOrder asc") @Query("select * from book_sources order by customOrder asc")
fun observeAll(): DataSource.Factory<Int, BookSource> fun liveDataAll(): LiveData<List<BookSource>>
@Query("select * from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey order by customOrder asc") @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> fun liveDataSearch(searchKey: String = ""): LiveData<List<BookSource>>
@Query("select * from book_sources where enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' order by customOrder asc") @Query("select * from book_sources where enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' order by customOrder asc")
fun liveExplore(): LiveData<List<BookSource>> fun liveExplore(): LiveData<List<BookSource>>

@ -9,8 +9,7 @@ import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder import androidx.recyclerview.widget.DiffUtil
import androidx.paging.PagedList
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -42,15 +41,16 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
private val qrRequestCode = 101 private val qrRequestCode = 101
private lateinit var adapter: BookSourceAdapter private lateinit var adapter: BookSourceAdapter
private var bookSourceLiveDate: LiveData<PagedList<BookSource>>? = null private var bookSourceLiveDate: LiveData<List<BookSource>>? = null
private var groups = hashSetOf<String>() private var groups = hashSetOf<String>()
private var groupMenu: SubMenu? = null private var groupMenu: SubMenu? = null
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
initRecyclerView() initRecyclerView()
initDataObserve()
initSearchView() initSearchView()
initLiveDataBookSource()
initLiveDataGroup()
} }
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -104,7 +104,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
this.setDrawable(it) this.setDrawable(it)
} }
}) })
adapter = BookSourceAdapter(this) adapter = BookSourceAdapter(this, this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
val itemTouchCallback = ItemTouchCallback() val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.onItemTouchCallbackListener = adapter
@ -120,12 +120,22 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
search_view.setOnQueryTextListener(this) search_view.setOnQueryTextListener(this)
} }
private fun initDataObserve(searchKey: String = "") { private fun initLiveDataBookSource(searchKey: String? = null) {
bookSourceLiveDate?.removeObservers(this) bookSourceLiveDate?.removeObservers(this)
val dataFactory = App.db.bookSourceDao().observeSearch("%$searchKey%") bookSourceLiveDate = if (searchKey.isNullOrEmpty()) {
bookSourceLiveDate = LivePagedListBuilder(dataFactory, 10000).build() App.db.bookSourceDao().liveDataAll()
bookSourceLiveDate?.observe(this, Observer { adapter.submitList(it) }) } else {
App.db.bookSourceDao().liveDataSearch("%$searchKey%")
}
bookSourceLiveDate?.observe(this, Observer {
search_view.queryHint = getString(R.string.search_book_source_num, it.size)
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it))
adapter.setItemsNoNotify(it)
diffResult.dispatchUpdatesTo(adapter)
})
}
private fun initLiveDataGroup() {
App.db.bookSourceDao().liveGroup().observe(this, Observer { App.db.bookSourceDao().liveGroup().observe(this, Observer {
groups.clear() groups.clear()
it.map { group -> it.map { group ->
@ -144,7 +154,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
override fun onQueryTextChange(newText: String?): Boolean { override fun onQueryTextChange(newText: String?): Boolean {
newText?.let { newText?.let {
initDataObserve(it) initLiveDataBookSource(it)
} }
return false return false
} }
@ -153,10 +163,6 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
return false return false
} }
override fun upCount(count: Int) {
search_view.queryHint = getString(R.string.search_book_source_num, count)
}
override fun del(bookSource: BookSource) { override fun del(bookSource: BookSource) {
viewModel.del(bookSource) viewModel.del(bookSource)
} }
@ -169,6 +175,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
startActivity<SourceEditActivity>(Pair("data", bookSource.bookSourceUrl)) startActivity<SourceEditActivity>(Pair("data", bookSource.bookSourceUrl))
} }
override fun upOrder() {
viewModel.upOrder()
}
override fun topSource(bookSource: BookSource) { override fun topSource(bookSource: BookSource) {
viewModel.topSource(bookSource) viewModel.topSource(bookSource)
} }

@ -1,107 +1,64 @@
package io.legado.app.ui.booksource package io.legado.app.ui.booksource
import android.view.LayoutInflater import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedList
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R 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.data.entities.BookSource
import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import kotlinx.android.synthetic.main.item_book_source.view.* import kotlinx.android.synthetic.main.item_book_source.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
class BookSourceAdapter(val callBack: CallBack) : class BookSourceAdapter(context: Context, val callBack: CallBack) :
PagedListAdapter<BookSource, BookSourceAdapter.MyViewHolder>(DIFF_CALLBACK), SimpleRecyclerAdapter<BookSource>(context, R.layout.item_book_source),
OnItemTouchCallbackListener { OnItemTouchCallbackListener {
companion object {
@JvmField
val 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
&& oldItem.bookSourceGroup == newItem.bookSourceGroup
&& oldItem.enabled == newItem.enabled
}
}
override fun onSwiped(adapterPosition: Int) { override fun onSwiped(adapterPosition: Int) {
} }
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
currentList?.let { val srcItem = getItem(srcPosition)
val srcSource = it[srcPosition] val targetItem = getItem(targetPosition)
val targetSource = it[targetPosition] if (srcItem != null && targetItem != null) {
srcSource?.let { a -> if (srcItem.customOrder == targetItem.customOrder) {
targetSource?.let { b -> callBack.upOrder()
a.customOrder = targetPosition } else {
b.customOrder = srcPosition val srcOrder = srcItem.customOrder
callBack.update(a, b) srcItem.customOrder = targetItem.customOrder
} targetItem.customOrder = srcOrder
callBack.update(srcItem, targetItem)
} }
} }
return true return true
} }
override fun onCurrentListChanged( override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) {
previousList: PagedList<BookSource>?, with(holder.itemView) {
currentList: PagedList<BookSource>?
) {
super.onCurrentListChanged(previousList, currentList)
callBack.upCount(itemCount)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.item_book_source,
parent,
false
)
)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
getItem(position)?.let { holder.bind(it, callBack) }
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(bookSource: BookSource, callBack: CallBack) = with(itemView) {
this.setBackgroundColor(context.backgroundColor) this.setBackgroundColor(context.backgroundColor)
if (bookSource.bookSourceGroup.isNullOrEmpty()) { if (item.bookSourceGroup.isNullOrEmpty()) {
cb_book_source.text = bookSource.bookSourceName cb_book_source.text = item.bookSourceName
} else { } else {
cb_book_source.text = cb_book_source.text =
String.format("%s (%s)", bookSource.bookSourceName, bookSource.bookSourceGroup) String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup)
} }
cb_book_source.isChecked = bookSource.enabled cb_book_source.isChecked = item.enabled
cb_book_source.setOnClickListener { cb_book_source.setOnClickListener {
bookSource.enabled = cb_book_source.isChecked item.enabled = cb_book_source.isChecked
callBack.update(bookSource) callBack.update(item)
} }
iv_edit_source.onClick { callBack.edit(bookSource) } iv_edit_source.onClick { callBack.edit(item) }
iv_top_source.onClick { callBack.topSource(bookSource) } iv_top_source.onClick { callBack.topSource(item) }
iv_del_source.onClick { callBack.del(bookSource) } iv_del_source.onClick { callBack.del(item) }
} }
} }
interface CallBack { interface CallBack {
fun upCount(count: Int)
fun del(bookSource: BookSource) fun del(bookSource: BookSource)
fun edit(bookSource: BookSource) fun edit(bookSource: BookSource)
fun update(vararg bookSource: BookSource) fun update(vararg bookSource: BookSource)
fun topSource(bookSource: BookSource) fun topSource(bookSource: BookSource)
fun upOrder()
} }
} }

@ -26,6 +26,16 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
execute { App.db.bookSourceDao().update(*bookSource) } execute { App.db.bookSourceDao().update(*bookSource) }
} }
fun upOrder() {
execute {
val sources = App.db.bookSourceDao().all
for ((index: Int, source: BookSource) in sources.withIndex()) {
source.customOrder = index + 1
}
App.db.bookSourceDao().update(*sources.toTypedArray())
}
}
fun addGroup(group: String) { fun addGroup(group: String) {
execute { execute {
val sources = App.db.bookSourceDao().noGroup val sources = App.db.bookSourceDao().noGroup

@ -0,0 +1,43 @@
package io.legado.app.ui.booksource
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.BookSource
class DiffCallBack(
private val oldItems: List<BookSource>,
private val newItems: List<BookSource>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.bookSourceUrl == newItem.bookSourceUrl
}
override fun getOldListSize(): Int {
return oldItems.size
}
override fun getNewListSize(): Int {
return newItems.size
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.bookSourceName == newItem.bookSourceName
&& oldItem.bookSourceGroup == newItem.bookSourceGroup
&& oldItem.enabled == newItem.enabled
}
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return when {
oldItem.bookSourceName == newItem.bookSourceName
&& oldItem.bookSourceGroup == newItem.bookSourceGroup
&& oldItem.enabled != newItem.enabled -> 2
else -> null
}
}
}
Loading…
Cancel
Save