pull/32/head
kunfei 5 years ago
parent 0a8fc314b2
commit 2e54c2cd04
  1. 5
      app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt
  2. 23
      app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt
  3. 36
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  4. 15
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt

@ -101,11 +101,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
LinearLayoutManager.VERTICAL
)
)
adapter = BookSourceAdapter()
adapter.callBack = this
adapter = BookSourceAdapter(this)
recycler_view.adapter = adapter
val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter.itemTouchCallbackListener
itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
}

@ -14,8 +14,9 @@ import io.legado.app.lib.theme.ThemeStore
import kotlinx.android.synthetic.main.item_book_source.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class BookSourceAdapter :
PagedListAdapter<BookSource, BookSourceAdapter.MyViewHolder>(DIFF_CALLBACK) {
class BookSourceAdapter(val callBack: CallBack) :
PagedListAdapter<BookSource, BookSourceAdapter.MyViewHolder>(DIFF_CALLBACK),
OnItemTouchCallbackListener {
companion object {
@ -32,9 +33,6 @@ class BookSourceAdapter :
}
}
var callBack: CallBack? = null
val itemTouchCallbackListener = object : OnItemTouchCallbackListener {
override fun onSwiped(adapterPosition: Int) {
}
@ -47,20 +45,19 @@ class BookSourceAdapter :
targetSource?.let { b ->
a.customOrder = targetPosition
b.customOrder = srcPosition
callBack?.update(a, b)
callBack.update(a, b)
}
}
}
return true
}
}
override fun onCurrentListChanged(
previousList: PagedList<BookSource>?,
currentList: PagedList<BookSource>?
) {
super.onCurrentListChanged(previousList, currentList)
callBack?.upCount(itemCount)
callBack.upCount(itemCount)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
@ -85,7 +82,7 @@ class BookSourceAdapter :
itemView.setBackgroundColor(ThemeStore.backgroundColor(itemView.context))
}
fun bind(bookSource: BookSource, callBack: CallBack?) = with(itemView) {
fun bind(bookSource: BookSource, callBack: CallBack) = with(itemView) {
if (bookSource.bookSourceGroup.isNullOrEmpty()) {
cb_book_source.text = bookSource.bookSourceName
} else {
@ -95,11 +92,11 @@ class BookSourceAdapter :
cb_book_source.isChecked = bookSource.enabled
cb_book_source.setOnClickListener {
bookSource.enabled = cb_book_source.isChecked
callBack?.update(bookSource)
callBack.update(bookSource)
}
iv_edit_source.onClick { callBack?.edit(bookSource) }
iv_top_source.onClick { callBack?.topSource(bookSource) }
iv_del_source.onClick { callBack?.del(bookSource) }
iv_edit_source.onClick { callBack.edit(bookSource) }
iv_top_source.onClick { callBack.topSource(bookSource) }
iv_del_source.onClick { callBack.del(bookSource) }
}
}

@ -11,16 +11,16 @@ import androidx.paging.PagedList
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.ATH
import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_book_source.*
import kotlinx.android.synthetic.main.activity_replace_rule.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast
class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activity_replace_rule),
@ -35,7 +35,6 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
initDataObservers()
initSwipeToDelete()
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -61,6 +60,10 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
this.setDrawable(it)
}
})
val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
}
@ -78,28 +81,6 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
}
}
private fun initSwipeToDelete() {
ItemTouchHelper(object : ItemTouchHelper.Callback() {
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
return makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
}
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
toast("You swiped the item!")
}
}).attachToRecyclerView(rv_replace_rule)
}
override fun update(rule: ReplaceRule) {
viewModel.update(rule)
}
@ -109,11 +90,6 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
}
override fun edit(rule: ReplaceRule) {
doAsync {
App.db.replaceRuleDao().enableAll(!allEnabled)
allEnabled = !allEnabled
}
toast("Edit function not implemented!")
}
}

@ -10,12 +10,14 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.ItemTouchCallback
import kotlinx.android.synthetic.main.item_relace_rule.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
PagedListAdapter<ReplaceRule, ReplaceRuleAdapter.MyViewHolder>(DIFF_CALLBACK) {
PagedListAdapter<ReplaceRule, ReplaceRuleAdapter.MyViewHolder>(DIFF_CALLBACK),
ItemTouchCallback.OnItemTouchCallbackListener {
companion object {
@ -34,8 +36,12 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
}
}
init {
notifyDataSetChanged()
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
return true
}
override fun onSwiped(adapterPosition: Int) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
@ -50,11 +56,8 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
fun bind(rule: ReplaceRule, listener: CallBack?) = with(itemView) {
tv_name.text = rule.name
swt_enabled.isChecked = rule.isEnabled
// divider.isGone = hideDivider
iv_delete.isGone = true
iv_edit.isGone = true
// iv_delete.onClick { listener?.delete(rule) }
// iv_edit.onClick { listener?.edit(rule) }
swt_enabled.onClick {
rule.isEnabled = swt_enabled.isChecked
listener?.update(rule)

Loading…
Cancel
Save