From 2e54c2cd0490ef733b35fee3edf9913c681706b1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 1 Sep 2019 22:36:27 +0800 Subject: [PATCH] up --- .../app/ui/booksource/BookSourceActivity.kt | 5 +-- .../app/ui/booksource/BookSourceAdapter.kt | 45 +++++++++---------- .../app/ui/replacerule/ReplaceRuleActivity.kt | 36 +++------------ .../app/ui/replacerule/ReplaceRuleAdapter.kt | 15 ++++--- 4 files changed, 38 insertions(+), 63 deletions(-) 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 334bc1bcc..14a947dd3 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 @@ -101,11 +101,10 @@ class BookSourceActivity : VMBaseActivity(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) } diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt index f9314073b..0d15be516 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt @@ -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(DIFF_CALLBACK) { +class BookSourceAdapter(val callBack: CallBack) : + PagedListAdapter(DIFF_CALLBACK), + OnItemTouchCallbackListener { companion object { @@ -32,27 +33,23 @@ class BookSourceAdapter : } } - var callBack: CallBack? = null + override fun onSwiped(adapterPosition: Int) { - val itemTouchCallbackListener = object : OnItemTouchCallbackListener { - override fun onSwiped(adapterPosition: Int) { - - } + } - override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { - currentList?.let { - val srcSource = it[srcPosition] - val targetSource = it[targetPosition] - srcSource?.let { a -> - targetSource?.let { b -> - a.customOrder = targetPosition - b.customOrder = srcPosition - callBack?.update(a, b) - } + override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { + currentList?.let { + val srcSource = it[srcPosition] + val targetSource = it[targetPosition] + srcSource?.let { a -> + targetSource?.let { b -> + a.customOrder = targetPosition + b.customOrder = srcPosition + callBack.update(a, b) } } - return true } + return true } override fun onCurrentListChanged( @@ -60,7 +57,7 @@ class BookSourceAdapter : currentList: PagedList? ) { 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) } } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index fda735181..a1854810c 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -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(R.layout.activity_replace_rule), @@ -35,7 +35,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initDataObservers() - initSwipeToDelete() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -61,6 +60,10 @@ class ReplaceRuleActivity : VMBaseActivity(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(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(R.layout.activi } override fun edit(rule: ReplaceRule) { - doAsync { - App.db.replaceRuleDao().enableAll(!allEnabled) - allEnabled = !allEnabled - } - toast("Edit function not implemented!") } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index 78587b1a5..b34f6202f 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -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(DIFF_CALLBACK) { + PagedListAdapter(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)