package com.noticeditorteam.noticeditorandroid.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.noticeditorteam.noticeditorandroid.NoticeListener;
import com.noticeditorteam.noticeditorandroid.R;
import com.noticeditorteam.noticeditorandroid.model.NoticeItem;

import us.feras.mdv.MarkdownView;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PreviewNoticeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PreviewNoticeFragment extends Fragment implements NoticeListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM_TREE = "tree";
    private static final String SAVE_PARAM_TREE = "tree";

    // TODO: Rename and change types of parameters
    private static NoticeItem notice;

    private MarkdownView mdView;

    public PreviewNoticeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param notice notice.
     * @return A new instance of fragment PreviewNoticeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PreviewNoticeFragment newInstance(NoticeItem notice) {
        PreviewNoticeFragment fragment = new PreviewNoticeFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM_TREE, notice);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            notice = getArguments().getParcelable(ARG_PARAM_TREE);
            assert notice != null;
            notice.addNoticeListener(this);
        }
        if(savedInstanceState != null) {
            notice.removeNoticeListener(this);
            notice = savedInstanceState.getParcelable(SAVE_PARAM_TREE);
            assert notice != null;
            notice.addNoticeListener(this);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_preview_notice, container, false);
        mdView = (MarkdownView) view.findViewById(R.id.markdownView);
        mdView.loadMarkdown(notice.getContent());
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(SAVE_PARAM_TREE, notice);
        notice.removeNoticeListener(this);
    }

    @Override
    public void onTitleChanged(String newTitle) {
    }

    @Override
    public void onContentChanged(String newContent) {
        mdView.loadMarkdown(newContent);
    }
}

@@ -1970,52 +1970,9 @@
 ll;%0A
-            notice.addNoticeListener(this);
 %0A
+
     
@@ -2022,55 +2022,8 @@
 ) %7B%0A
-            notice.removeNoticeListener(this);%0A
     
@@ -2129,52 +2129,8 @@
 ll;%0A
-            notice.addNoticeListener(this);%0A
     
@@ -2547,16 +2547,56 @@
 ent());%0A
+        notice.addNoticeListener(this);%0A
         
@@ -2721,32 +2721,75 @@
 tate(outState);%0A
+        notice.removeNoticeListener(this);%0A
         outState
@@ -2833,51 +2833,8 @@
 e);%0A
-        notice.removeNoticeListener(this);%0A
     

Fix #7.