Created
May 13, 2018 03:38
-
-
Save adrianhall/750298de45af042685298941567867de 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
| package com.shellmonger.apps.mynotes.ui | |
| import android.arch.lifecycle.Observer | |
| import android.arch.lifecycle.ViewModelProviders | |
| import android.support.v7.app.AppCompatActivity | |
| import android.os.Bundle | |
| import android.util.Log | |
| import com.shellmonger.apps.mynotes.R | |
| import com.shellmonger.apps.mynotes.extensions.set | |
| import com.shellmonger.apps.mynotes.models.Note | |
| import com.shellmonger.apps.mynotes.viewmodels.NoteDetailViewModel | |
| import kotlinx.android.synthetic.main.activity_note_detail.* | |
| class NoteDetailActivity : AppCompatActivity() { | |
| companion object { | |
| private val TAG = this::class.java.simpleName | |
| } | |
| private lateinit var viewModel: NoteDetailViewModel | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_note_detail) | |
| viewModel = ViewModelProviders.of(this).get(NoteDetailViewModel::class.java) | |
| // Set the note that we want to edit | |
| viewModel.setNoteId("temp-note") | |
| // Observe the viewModel for changes to the note | |
| viewModel.currentNote.observe(this, Observer { | |
| Log.d(TAG,"Loading note via observer") | |
| it?.let { | |
| detail_id_field.text = it.noteId | |
| detail_title_editor.text.set(it.title) | |
| detail_content_editor.text.set(it.content) | |
| } | |
| }) | |
| } | |
| override fun onPause() { | |
| super.onPause() | |
| saveNote() | |
| } | |
| private fun saveNote() { | |
| Log.d(TAG, "Note is being saved") | |
| with (viewModel.currentNote.value!!) { | |
| val note = Note(noteId).apply { | |
| title = detail_title_editor.text.toString() | |
| content = detail_content_editor.text.toString() | |
| } | |
| viewModel.saveNote(note) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment