content: /*
  Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this
  software and associated documentation files (the "Software"), to deal in the Software
  without restriction, including without limitation the rights to use, copy, modify,
  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   */
  package com.amazonaws.mobile.samples.mynotes.ui;
  
  import android.arch.lifecycle.ViewModelProviders;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v4.app.Fragment;
  import android.text.Editable;
  import android.text.TextWatcher;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  
  import com.amazonaws.mobile.samples.mynotes.NotesApp;
  import com.amazonaws.mobile.samples.mynotes.R;
  import com.amazonaws.mobile.samples.mynotes.models.Note;
  import com.amazonaws.mobile.samples.mynotes.viewmodels.NoteDetailViewModel;
  
  public class NoteDetailFragment extends Fragment {
      NoteDetailViewModel viewModel;
      String noteId;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          Bundle arguments = getArguments();
          if (arguments != null) {
              String noteId = arguments.getString(NotesApp.ITEM_ID);
              this.noteId = (noteId.equals("new")) ? null : noteId;
          }
      }
  
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.note_detail, container, false);
  
          final EditText titleField = view.findViewById(R.id.edit_title);
          titleField.setEnabled(false);   // Disable the field by default
  
          final EditText contentField = view.findViewById(R.id.edit_content);
          contentField.setEnabled(false);
  
          viewModel = ViewModelProviders.of(this).get(NoteDetailViewModel.class);
          // Observe the view model values.  Once we receive the value, enable the field.
          viewModel.getTitle().observe(this, (String title) -> {
              titleField.setText(title);
              titleField.setEnabled(true);
          });
          viewModel.getContent().observe(this, (String content) -> {
              contentField.setText(content);
              contentField.setEnabled(true);
          });
  
          // If this is a new note, create the note, then enable the fields.  Otherwise just load the fields
          // - the fields are received via observables
          if (noteId == null) {
              viewModel.create("", "", (Note result) -> {
                  titleField.setEnabled(true);
                  contentField.setEnabled(true);
              });
          } else {
              viewModel.setNoteId(noteId);
          }
  
          TextWatcher saveHandler = new TextWatcher() {
              @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
              @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
              @Override public void afterTextChanged(Editable s) {
                  String title = titleField.getText().toString();
                  String content = contentField.getText().toString();
                  viewModel.update(title, content);
              }
          };
  
          titleField.addTextChangedListener(saveHandler);
          contentField.addTextChangedListener(saveHandler);
  
          return view;
      }
  }
  