* Cheeses are stored in a database, so swipes and additions edit the database directly, and the UI * is updated automatically using paging components. */ class MainActivity : AppCompatActivity() { private val viewModel by lazy(LazyThreadSafetyMode.NONE) { ViewModelProviders.of(this).get(CheeseViewModel::class.java) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Create adapter for the RecyclerView val adapter = CheeseAdapter() cheeseList.adapter = adapter // Subscribe the adapter to the ViewModel, so the items in the adapter are refreshed // when the list changes viewModel.allCheeses.observe(this, Observer(adapter::setList)) initAddButtonListener() initSwipeToDelete() } private fun initSwipeToDelete() { ItemTouchHelper(object : ItemTouchHelper.Callback() { // enable the items to swipe to the left or right override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int = makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean = false // When an item is swiped, remove the item via the view model. The list item will be // automatically removed in response, because the adapter is observing the live list. override fun onSwiped(viewHolder: RecyclerView.ViewHolder?, direction: Int) { (viewHolder as? CheeseViewHolder)?.cheese?.let { viewModel.remove(it) } } }).attachToRecyclerView(cheeseList) } private fun addCheese() { val newCheese = inputText.text.trim() if (newCheese.isNotEmpty()) { viewModel.insert(newCheese) inputText.setText("") } } private fun initAddButtonListener() { addButton.setOnClickListener { addCheese() } // when the user taps the "Done" button in the on screen keyboard, save the item. inputText.setOnEditorActionListener({ _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_DONE) { addCheese() return@setOnEditorActionListener true } false // action that isn't DONE occurred - ignore }) // When the user clicks on the button, or presses enter, save the item. inputText.setOnKeyListener({ _, keyCode, event -> if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) { addCheese() return@setOnKeyListener true } false // event that isn't DOWN or ENTER occurred - ignore }) } }