Skip to content

Instantly share code, notes, and snippets.

@adrianhall
Last active May 14, 2018 04:58
Show Gist options
  • Save adrianhall/793d6aa09fe72d52fb86924a33b4bace to your computer and use it in GitHub Desktop.
Save adrianhall/793d6aa09fe72d52fb86924a33b4bace to your computer and use it in GitHub Desktop.
class NoteListActivity : AppCompatActivity() {
companion object {
private val TAG = this::class.java.simpleName
}
private val viewModel by viewModel<NoteListViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_note_list)
setSupportActionBar(note_list_toolbar)
note_list_add_note.setOnClickListener {
Log.d(TAG, "addNote button pressed")
launchNoteDetailActivity(this)
}
val adapter = NoteListAdapter {
launchNoteDetailActivity(this@NoteListActivity, it.noteId)
}
note_list_recyclerview.layoutManager = LinearLayoutManager(this).apply {
orientation = LinearLayoutManager.VERTICAL
}
note_list_recyclerview.adapter = adapter
viewModel.notes.observe(this, Observer {
adapter.loadItems(it ?: emptyList())
adapter.notifyDataSetChanged()
})
}
private fun launchNoteDetailActivity(context: Context, noteId: String? = null) {
val intent = Intent(context, NoteDetailActivity::class.java)
noteId?.let {
val bundle = Bundle().apply { putString(NoteDetailActivity.ARG_NOTE, noteId) }
intent.putExtras(bundle)
}
context.startActivity(intent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment