From 0acc3cea8dd35ec0b5c5203361b2acb888984e17 Mon Sep 17 00:00:00 2001 From: sds100 Date: Wed, 28 Oct 2020 16:52:24 +0000 Subject: [PATCH] #476 reordering actions works --- .../data/viewmodel/ConfigKeymapViewModel.kt | 16 ++++ .../keymapper/ui/fragment/ActionsFragment.kt | 89 +++++++++++++++---- 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/github/sds100/keymapper/data/viewmodel/ConfigKeymapViewModel.kt b/app/src/main/java/io/github/sds100/keymapper/data/viewmodel/ConfigKeymapViewModel.kt index f7c75f4240..61a9e5ad33 100644 --- a/app/src/main/java/io/github/sds100/keymapper/data/viewmodel/ConfigKeymapViewModel.kt +++ b/app/src/main/java/io/github/sds100/keymapper/data/viewmodel/ConfigKeymapViewModel.kt @@ -563,6 +563,22 @@ class ConfigKeymapViewModel internal constructor( invalidateOptions() } + fun moveAction(fromIndex: Int, toIndex: Int) { + actionList.value = actionList.value?.toMutableList()?.apply { + if (fromIndex < toIndex) { + for (i in fromIndex until toIndex) { + Collections.swap(this, i, i + 1) + } + } else { + for (i in fromIndex downTo toIndex + 1) { + Collections.swap(this, i, i - 1) + } + } + } + + invalidateOptions() + } + fun setActionBehavior(actionBehavior: ActionBehavior) { actionList.value = actionList.value?.map { diff --git a/app/src/main/java/io/github/sds100/keymapper/ui/fragment/ActionsFragment.kt b/app/src/main/java/io/github/sds100/keymapper/ui/fragment/ActionsFragment.kt index 5189f8abe8..e7a461d208 100644 --- a/app/src/main/java/io/github/sds100/keymapper/ui/fragment/ActionsFragment.kt +++ b/app/src/main/java/io/github/sds100/keymapper/ui/fragment/ActionsFragment.kt @@ -9,8 +9,14 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.map import androidx.navigation.fragment.findNavController import androidx.navigation.navGraphViewModels +import androidx.recyclerview.widget.ItemTouchHelper +import com.airbnb.epoxy.EpoxyController +import com.airbnb.epoxy.EpoxyTouchHelper +import com.google.android.material.card.MaterialCardView +import io.github.sds100.keymapper.ActionBindingModel_ import io.github.sds100.keymapper.R import io.github.sds100.keymapper.action +import io.github.sds100.keymapper.data.model.ActionModel import io.github.sds100.keymapper.data.viewmodel.ConfigKeymapViewModel import io.github.sds100.keymapper.databinding.FragmentActionsBinding import io.github.sds100.keymapper.service.MyAccessibilityService @@ -35,6 +41,8 @@ class ActionsFragment(private val mKeymapId: Long) : Fragment() { } } + private val mActionListController = ActionListController() + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { FragmentActionsBinding.inflate(inflater, container, false).apply { viewModel = mViewModel @@ -46,6 +54,7 @@ class ActionsFragment(private val mKeymapId: Long) : Fragment() { }) subscribeActionList() + epoxyRecyclerViewActions.adapter = mActionListController.adapter mViewModel.testAction.observe(viewLifecycleOwner, EventObserver { if (AccessibilityUtils.isServiceEnabled(requireContext())) { @@ -75,31 +84,75 @@ class ActionsFragment(private val mKeymapId: Long) : Fragment() { private fun FragmentActionsBinding.subscribeActionList() { mActionModelList.observe(viewLifecycleOwner, { actionList -> - epoxyRecyclerViewActions.withModels { + enableActionDragging(mActionListController) + mActionListController.modelList = actionList + }) + } - actionList.forEachIndexed { _, model -> - action { - id(model.id) - model(model) - icon(model.icon) - actionCount(actionList.size) + private fun FragmentActionsBinding.enableActionDragging(controller: EpoxyController): ItemTouchHelper { + return EpoxyTouchHelper.initDragging(controller) + .withRecyclerView(epoxyRecyclerViewActions) + .forVerticalList() + .withTarget(ActionBindingModel_::class.java) + .andCallbacks(object : EpoxyTouchHelper.DragCallbacks() { - onRemoveClick { _ -> - mViewModel.removeAction(model.id) - } + override fun isDragEnabledForModel(model: ActionBindingModel_?): Boolean { + return mViewModel.actionList.value?.size!! > 1 + } - onMoreClick { _ -> - mViewModel.chooseActionBehavior(model.id) - } + override fun onModelMoved( + fromPosition: Int, + toPosition: Int, + modelBeingMoved: ActionBindingModel_?, + itemView: View? + ) { + mViewModel.moveAction(fromPosition, toPosition) + } + + override fun onDragStarted( + model: ActionBindingModel_?, + itemView: View?, + adapterPosition: Int + ) { + itemView?.findViewById(R.id.cardView)?.isDragged = true + } + + override fun onDragReleased(model: ActionBindingModel_?, itemView: View?) { + itemView?.findViewById(R.id.cardView)?.isDragged = false + } + }) + } + + private inner class ActionListController : EpoxyController() { + var modelList: List = listOf() + set(value) { + requestModelBuild() + field = value + } + + override fun buildModels() { + modelList.forEachIndexed { _, model -> + action { + id(model.id) + model(model) + icon(model.icon) + actionCount(modelList.size) - onClick { _ -> - mActionModelList.value?.single { it.id == model.id }?.let { - mViewModel.onActionModelClick(it) - } + onRemoveClick { _ -> + mViewModel.removeAction(model.id) + } + + onMoreClick { _ -> + mViewModel.chooseActionBehavior(model.id) + } + + onClick { _ -> + mActionModelList.value?.single { it.id == model.id }?.let { + mViewModel.onActionModelClick(it) } } } } - }) + } } } \ No newline at end of file