Last active
May 13, 2018 04:24
-
-
Save adrianhall/eaeb1add8fa2bb14de5072af6aa3f937 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.os.Bundle | |
import android.support.v4.app.Fragment | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
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.fragment_note_detail.view.* | |
class NoteDetailFragment : Fragment() { | |
companion object { | |
private val TAG = this::class.java.simpleName | |
const val ARG_NOTE = "noteId" | |
} | |
private lateinit var viewModel: NoteDetailViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
viewModel = ViewModelProviders.of(this).get(NoteDetailViewModel::class.java) | |
arguments?.let { | |
if (it.containsKey(ARG_NOTE)) viewModel.setNoteId(it.getString(ARG_NOTE)) | |
} | |
} | |
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 = view?.detail_title_editor?.text.toString() | |
content = view?.detail_content_editor?.text.toString() | |
} | |
viewModel.saveNote(note) | |
} | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
val rootView = inflater.inflate(R.layout.fragment_note_detail, container, false) | |
viewModel.currentNote.observe(this, Observer { | |
it?.let { | |
rootView.detail_id_field.text = it.noteId | |
rootView.detail_title_editor.text.set(it.title) | |
rootView.detail_content_editor.text.set(it.content) | |
} | |
}) | |
return rootView | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment