package com.sinyuk.yuk.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Sinyuk on 16/7/6.
 */
public abstract class BaseFragment extends Fragment {
    private Unbinder unbinder;
    private CompositeSubscription mCompositeSubscription;
    protected Context mContext = null;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCompositeSubscription = new CompositeSubscription();
        beforeInflate();
    }

    protected abstract void beforeInflate();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getRootViewId(), container, false);
    }

    protected abstract int getRootViewId();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        finishInflate();
    }

    protected abstract void finishInflate();

    protected void addSubscription(Subscription s) {
        mCompositeSubscription.add(s);
    }

    protected void removeSubscription(Subscription s) {
        mCompositeSubscription.remove(s);
    }

    protected void clearSubscription() {
        mCompositeSubscription.clear();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        if (!mCompositeSubscription.isUnsubscribed()) { mCompositeSubscription.unsubscribe(); }

    }


}

@@ -163,16 +163,67 @@
 agment;%0A
+import android.support.v4.app.FragmentTransaction;%0A
 import a
@@ -663,16 +663,94 @@
 = null;%0A
+    private static final String STATE_SAVE_IS_HIDDEN = %22STATE_SAVE_IS_HIDDEN%22;
 %0A    @Ov
@@ -833,16 +833,17 @@
 ntext);%0A
+%0A
         
@@ -1104,84 +1104,695 @@
     
-mCompositeSubscription = new CompositeSubscription();%0A        beforeInflate(
+if (savedInstanceState != null) %7B%0A            boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);%0A%0A            FragmentTransaction ft = getFragmentManager().beginTransaction();%0A            if (isSupportHidden) %7B%0A                ft.hide(this);%0A            %7D else %7B%0A                ft.show(this);%0A            %7D%0A            ft.commit();%0A        %7D%0A        mCompositeSubscription = new CompositeSubscription();%0A        beforeInflate();%0A    %7D%0A%0A    /**%0A     * %E4%BF%9D%E5%AD%98Fragment%E7%9A%84Hidden%E7%8A%B6%E6%80%81%0A     * %E9%81%BF%E5%85%8D%E5%86%85%E5%AD%98%E9%87%8D%E5%90%AF%E5%AF%BC%E8%87%B4%E7%9A%84Fragment%E9%87%8D%E5%8F%A0%0A     * @param outState%0A     */%0A    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden()
 );%0A 

Refactor on base fragment