Last active
June 12, 2021 13:21
-
-
Save AbGhost-cyber/36320c2571a608689b19c53bf6ad89c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val itemTouchHelperCallback = object : SimpleCallback( | |
0, LEFT or RIGHT | |
) { | |
override fun onMove( | |
recyclerView: RecyclerView, | |
viewHolder: ViewHolder, | |
target: ViewHolder | |
): Boolean { | |
return false | |
} | |
override fun onSwiped(viewHolder: ViewHolder, direction: Int) { | |
val position = viewHolder.layoutPosition | |
val semester = semesterAdapter.differ.currentList[position] | |
if (direction == LEFT) { | |
homeViewModel.deleteSemester(semester.id) | |
Snackbar.make( | |
requireView(), "semester deleted", | |
Snackbar.LENGTH_LONG | |
).apply { | |
setAction("Undo") { | |
homeViewModel.insertSemester(semester) | |
homeViewModel.deleteLocallyDeletedSemesterId(semester.id) | |
} | |
show() | |
} | |
} else if (direction == RIGHT) { | |
homeViewModel.observeSemesterById(semester.id).observe(viewLifecycleOwner, | |
Observer { | |
it?.let { semester -> | |
currentSemester = semester | |
} | |
}) | |
showAddOwnerToSemesterDialog() | |
} | |
} | |
override fun onChildDraw( | |
c: Canvas, | |
recyclerView: RecyclerView, | |
viewHolder: ViewHolder, | |
dX: Float, | |
dY: Float, | |
actionState: Int, | |
isCurrentlyActive: Boolean | |
) { | |
if (actionState == ACTION_STATE_SWIPE) { | |
swipingItem.postValue(isCurrentlyActive) | |
} | |
setupDecorator( | |
c, recyclerView, viewHolder, | |
dX, dY, actionState, | |
isCurrentlyActive | |
) | |
super.onChildDraw( | |
c, | |
recyclerView, | |
viewHolder, | |
dX / 4, | |
dY, | |
actionState, | |
isCurrentlyActive | |
) | |
} | |
} | |
fun setupDecorator( | |
c: Canvas, recyclerView: RecyclerView, | |
viewHolder: RecyclerView.ViewHolder, | |
dX: Float, dY: Float, actionState: Int, | |
isCurrentlyActive: Boolean | |
) { | |
RecyclerViewSwipeDecorator.Builder( | |
c, | |
recyclerView, | |
viewHolder, | |
dX, | |
dY, | |
actionState, | |
isCurrentlyActive | |
).addSwipeRightActionIcon(R.drawable.ic_email) | |
.addSwipeLeftActionIcon(R.drawable.ic_baseline_delete_24) | |
.setSwipeLeftLabelColor(Color.WHITE) | |
.setSwipeRightLabelColor(Color.WHITE) | |
.addSwipeLeftBackgroundColor(Color.RED) | |
.addSwipeRightBackgroundColor(Color.GREEN) | |
.create() | |
.decorate() | |
} | |
/*swipe decoration library | |
implementation 'it.xabaras.android:recyclerview-swipedecorator:1.2.2' | |
*remember to attach it to the recycleview with | |
*ItemTouchHelper(itemTouchHelperCallback) .attachToRecyclerView(this) | |
*dx/4 reduces the the width of the backgroun drawn, that is, it won't show the fully drawn background | |
*ignore my viewmodel code :) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment