Skip to content

Instantly share code, notes, and snippets.

@vshkl
Created November 15, 2020 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vshkl/c209a23e003cbd7eef96e93ce079ac5a to your computer and use it in GitHub Desktop.
Save vshkl/c209a23e003cbd7eef96e93ce079ac5a to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity(R.layout.activity_main),
MainCallbackAdapter, OnInterceptTouchEventListener, OnRowMoveListener {
private val viewModel by viewModels<MainViewModel>()
private val controller = MainController(this)
private var touchHelper: ItemTouchHelper? = null
private var touchedPosition = -1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupList()
observeRowsData()
}
/** @see MainCallbackAdapter */
override fun onDragStart() {
if (touchedPosition >= 0) {
val touchedModel = controller.adapter.getModelAtPosition(touchedPosition)
if (touchedModel is RowEpoxyModel_) {
val touchedViewHolder = controller.adapter.boundViewHolders.getHolderForModel(touchedModel)
if (touchedViewHolder != null) {
touchHelper?.startDrag(touchedViewHolder)
}
}
}
}
/** @see OnInterceptTouchEventListener */
override fun onInterceptTouchEvent(touchedPosition: Int) {
this.touchedPosition = touchedPosition
}
/** @see OnRowMoveListener */
override fun onMoved(movingRowId: String, shiftingRowId: String) {
viewModel.moveRow(movingRowId, shiftingRowId)
}
private fun setupList() {
val layoutManager = LinearLayoutManager(this)
erv_list.layoutManager = layoutManager
erv_list.setControllerAndBuildModels(controller)
erv_list.addOnItemTouchListener(MainSimpleOnItemTouchListener(this))
erv_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
setupTouchHelper(erv_list)
}
private fun setupTouchHelper(recyclerView: RecyclerView) {
touchHelper = ItemTouchHelper(MainEpoxyTouchCallback(controller, this))
.apply { attachToRecyclerView(recyclerView) }
}
private fun observeRowsData() {
viewModel.firstRowsData.observe(this, { controller.setFirstRowsData(it) })
viewModel.secondRowsData.observe(this, { controller.setSecondRowsData(it) })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment