Skip to content

Commit

Permalink
#476 reordering actions works
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Oct 28, 2020
1 parent a204ee9 commit 0acc3ce
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())) {
Expand Down Expand Up @@ -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<ActionBindingModel_>() {

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<MaterialCardView>(R.id.cardView)?.isDragged = true
}

override fun onDragReleased(model: ActionBindingModel_?, itemView: View?) {
itemView?.findViewById<MaterialCardView>(R.id.cardView)?.isDragged = false
}
})
}

private inner class ActionListController : EpoxyController() {
var modelList: List<ActionModel> = 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)
}
}
}
}
})
}
}
}

0 comments on commit 0acc3ce

Please sign in to comment.