package com.nhpatt.asde.mvp.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.View;

import com.nhpatt.asde.mvp.activities.PersistedObject;
import com.nhpatt.asde.mvp.presenters.Presenter;


public abstract class AbstractFragment<P extends Presenter> extends Fragment {

    private P presenter;

    protected P getPresenter() {
        return presenter;
    }

    protected abstract P createPresenter();

    public void showError(String message) {
        View content = getView().findViewById(android.R.id.content);
        Snackbar.make(content, message, Snackbar.LENGTH_LONG).show();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        this.presenter = createPresenter();
        presenter.onCreate(savedInstanceState);

        presenter.setPersistedObject((PersistedObject) getActivity().getLastCustomNonConfigurationInstance());
    }

    @Override
    public void onStart() {
        super.onStart();
        presenter.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        presenter.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        presenter.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }


}

@@ -221,63 +221,8 @@
 w;%0A%0A
-import com.nhpatt.asde.mvp.activities.PersistedObject;%0A
 impo
@@ -358,25 +358,167 @@
   pr
-ivate P presenter
+otected boolean destroyedBySystem;%0A    private P presenter;%0A    private RetainedFragment retainFragment;%0A    private String tag = getClass().getCanonicalName()
 ;%0A%0A 
@@ -849,32 +849,39 @@
  void on
-ActivityCreated(
+ViewCreated(View view, 
 @Nullabl
@@ -931,24 +931,20 @@
 r.on
-Activity
+View
 Created(
 save
@@ -939,16 +939,22 @@
 Created(
+view, 
 savedIns
@@ -979,13 +979,8 @@
     
-this.
 pres
@@ -1002,24 +1002,61 @@
 resenter();%0A
+        presenter.setFragment(true);%0A
         pres
@@ -1104,106 +1104,153 @@
     
-p
 re
-senter.setPersistedObject((PersistedObject) getActivity().getLastCustomNonConfigurationInstance
+tainFragment = RetainedFragment.findOrCreate(getFragmentManager(), tag);%0A        presenter.setPersistedObject(retainFragment.getPersistedObject
 ());
@@ -1424,24 +1424,60 @@
 .onResume();
+%0A%0A        destroyedBySystem = false;
 %0A        pre
@@ -1494,32 +1494,187 @@
 esume();%0A    %7D%0A%0A
+    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        destroyedBySystem = true;%0A    %7D%0A%0A
     @Override%0A  
@@ -1900,24 +1900,308 @@
 Destroy() %7B%0A
+        if (destroyedBySystem) %7B%0A            retainFragment.setPersistedObject(presenter.getPersistedObject());%0A        %7D else %7B%0A            retainFragment.remove(getFragmentManager());%0A            retainFragment.setPersistedObject(null);%0A            retainFragment = null;%0A        %7D%0A
         supe
@@ -2257,7 +2257,5 @@
  %7D%0A%0A
-%0A%7D%0A
+%7D

use retained fragments