订阅源和替换规则添加滑动选择

pull/363/head
gedoor 4 years ago
parent 827a64b174
commit 69cb0a5f1e
  1. 8
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  2. 27
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  3. 18
      app/src/main/java/io/legado/app/ui/rss/source/manage/DiffCallBack.kt
  4. 8
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  5. 51
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt

@ -31,6 +31,7 @@ import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.ui.replacerule.edit.ReplaceEditDialog import io.legado.app.ui.replacerule.edit.ReplaceEditDialog
import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.ui.widget.text.AutoCompleteTextView
import io.legado.app.utils.* import io.legado.app.utils.*
@ -86,6 +87,13 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
val itemTouchCallback = ItemTouchCallback() val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true itemTouchCallback.isCanDrag = true
val dragSelectTouchHelper: DragSelectTouchHelper =
DragSelectTouchHelper(adapter.initDragSelectTouchHelperCallback()).setSlideArea(16, 50)
dragSelectTouchHelper.attachToRecyclerView(recycler_view)
// When this page is opened, it is in selection mode
dragSelectTouchHelper.activeSlideSelect()
// Note: need judge selection first, so add ItemTouchHelper after it.
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
} }

@ -12,6 +12,7 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
import kotlinx.android.synthetic.main.item_replace_rule.view.* import kotlinx.android.synthetic.main.item_replace_rule.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.* import java.util.*
@ -155,6 +156,32 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
} }
} }
fun initDragSelectTouchHelperCallback(): DragSelectTouchHelper.Callback {
return object : DragSelectTouchHelper.AdvanceCallback<ReplaceRule>(Mode.ToggleAndReverse) {
override fun currentSelectedId(): MutableSet<ReplaceRule> {
return selected
}
override fun getItemId(position: Int): ReplaceRule {
return getItem(position)!!
}
override fun updateSelectState(position: Int, isSelected: Boolean): Boolean {
getItem(position)?.let {
if (isSelected) {
selected.add(it)
} else {
selected.remove(it)
}
notifyItemChanged(position, bundleOf(Pair("selected", null)))
callBack.upCountView()
return true
}
return false
}
}
}
interface CallBack { interface CallBack {
fun update(vararg rule: ReplaceRule) fun update(vararg rule: ReplaceRule)
fun delete(rule: ReplaceRule) fun delete(rule: ReplaceRule)

@ -1,5 +1,6 @@
package io.legado.app.ui.rss.source.manage package io.legado.app.ui.rss.source.manage
import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
@ -33,10 +34,19 @@ class DiffCallBack(
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val oldItem = oldItems[oldItemPosition] val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition] val newItem = newItems[newItemPosition]
return when { val payload = Bundle()
oldItem.sourceName == newItem.sourceName if (oldItem.sourceName != newItem.sourceName) {
&& oldItem.enabled != newItem.enabled -> 2 payload.putString("name", newItem.sourceName)
else -> null
} }
if (oldItem.sourceGroup != newItem.sourceGroup) {
payload.putString("group", newItem.sourceGroup)
}
if (oldItem.enabled != newItem.enabled) {
payload.putBoolean("enabled", newItem.enabled)
}
if (payload.isEmpty) {
return null
}
return payload
} }
} }

@ -30,6 +30,7 @@ import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.qrcode.QrCodeActivity
import io.legado.app.ui.rss.source.edit.RssSourceEditActivity import io.legado.app.ui.rss.source.edit.RssSourceEditActivity
import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.ui.widget.text.AutoCompleteTextView
import io.legado.app.utils.* import io.legado.app.utils.*
@ -116,6 +117,13 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
val itemTouchCallback = ItemTouchCallback() val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true itemTouchCallback.isCanDrag = true
val dragSelectTouchHelper: DragSelectTouchHelper =
DragSelectTouchHelper(adapter.initDragSelectTouchHelperCallback()).setSlideArea(16, 50)
dragSelectTouchHelper.attachToRecyclerView(recycler_view)
// When this page is opened, it is in selection mode
dragSelectTouchHelper.activeSlideSelect()
// Note: need judge selection first, so add ItemTouchHelper after it.
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
} }

@ -1,8 +1,10 @@
package io.legado.app.ui.rss.source.manage package io.legado.app.ui.rss.source.manage
import android.content.Context import android.content.Context
import android.os.Bundle
import android.view.View import android.view.View
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView 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.ItemViewHolder
@ -10,6 +12,7 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
import kotlinx.android.synthetic.main.item_rss_source.view.* import kotlinx.android.synthetic.main.item_rss_source.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.* import java.util.*
@ -24,7 +27,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
getItems().forEach { getItems().forEach {
selected.add(it) selected.add(it)
} }
notifyItemRangeChanged(0, itemCount, 1) notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null)))
callBack.upCountView() callBack.upCountView()
} }
@ -36,7 +39,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
selected.add(it) selected.add(it)
} }
} }
notifyItemRangeChanged(0, itemCount, 1) notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null)))
callBack.upCountView() callBack.upCountView()
} }
@ -52,7 +55,8 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
if (payloads.isEmpty()) { val bundle = payloads.getOrNull(0) as? Bundle
if (bundle == null) {
this.setBackgroundColor(context.backgroundColor) this.setBackgroundColor(context.backgroundColor)
if (item.sourceGroup.isNullOrEmpty()) { if (item.sourceGroup.isNullOrEmpty()) {
cb_source.text = item.sourceName cb_source.text = item.sourceName
@ -63,9 +67,18 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
swt_enabled.isChecked = item.enabled swt_enabled.isChecked = item.enabled
cb_source.isChecked = selected.contains(item) cb_source.isChecked = selected.contains(item)
} else { } else {
when (payloads[0]) { bundle.keySet().map {
1 -> cb_source.isChecked = selected.contains(item) when (it) {
2 -> swt_enabled.isChecked = item.enabled "name", "group" ->
if (item.sourceGroup.isNullOrEmpty()) {
cb_source.text = item.sourceName
} else {
cb_source.text =
String.format("%s (%s)", item.sourceName, item.sourceGroup)
}
"selected" -> cb_source.isChecked = selected.contains(item)
"enabled" -> swt_enabled.isChecked = item.enabled
}
} }
} }
} }
@ -147,6 +160,32 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
} }
} }
fun initDragSelectTouchHelperCallback(): DragSelectTouchHelper.Callback {
return object : DragSelectTouchHelper.AdvanceCallback<RssSource>(Mode.ToggleAndReverse) {
override fun currentSelectedId(): MutableSet<RssSource> {
return selected
}
override fun getItemId(position: Int): RssSource {
return getItem(position)!!
}
override fun updateSelectState(position: Int, isSelected: Boolean): Boolean {
getItem(position)?.let {
if (isSelected) {
selected.add(it)
} else {
selected.remove(it)
}
notifyItemChanged(position, bundleOf(Pair("selected", null)))
callBack.upCountView()
return true
}
return false
}
}
}
interface CallBack { interface CallBack {
fun del(source: RssSource) fun del(source: RssSource)
fun edit(source: RssSource) fun edit(source: RssSource)

Loading…
Cancel
Save