Created
November 15, 2020 14:22
-
-
Save vshkl/c209a23e003cbd7eef96e93ce079ac5a 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
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