// Path: mvvm-life-cycle/src/main/java/me/henrytao/mvvmlifecycle/rx/SubscriptionManager.java
// public class SubscriptionManager implements ISubscriptionManager {
// 
//   private Map<UnsubscribeLifeCycle, Map<String, Subscription>> mSubscriptions;
// 
//   public SubscriptionManager() {
//     mSubscriptions = new HashMap<>();
//   }
// 
//   @Override
//   public void manageSubscription(Subscription subscription, UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     manageSubscription(UUID.randomUUID().toString(), subscription, unsubscribeLifeCycle);
//   }
// 
//   @Override
//   public void manageSubscription(String id, Subscription subscription, UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     Map<String, Subscription> subscriptions = getSubscriptions(unsubscribeLifeCycle);
//     if (subscriptions.containsKey(id)) {
//       unsubscribe(id);
//     }
//     subscriptions.put(id, subscription);
//   }
// 
//   @Override
//   public void unsubscribe(UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     unsubscribe(getSubscriptions(unsubscribeLifeCycle));
//   }
// 
//   @Override
//   public void unsubscribe() {
//     for (Map.Entry<UnsubscribeLifeCycle, Map<String, Subscription>> entry : mSubscriptions.entrySet()) {
//       unsubscribe(entry.getValue());
//     }
//     mSubscriptions.clear();
//   }
// 
//   @Override
//   public void unsubscribe(String id) {
//     for (Map.Entry<UnsubscribeLifeCycle, Map<String, Subscription>> entry : mSubscriptions.entrySet()) {
//       unsubscribe(entry.getValue().get(id));
//     }
//   }
// 
//   private Map<String, Subscription> getSubscriptions(UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     Map<String, Subscription> subscriptions = mSubscriptions.get(unsubscribeLifeCycle);
//     if (subscriptions == null) {
//       subscriptions = new HashMap<>();
//       mSubscriptions.put(unsubscribeLifeCycle, subscriptions);
//     }
//     return subscriptions;
//   }
// 
//   private void unsubscribe(Map<String, Subscription> subscriptions) {
//     for (Map.Entry<String, Subscription> entry : subscriptions.entrySet()) {
//       unsubscribe(entry.getValue());
//     }
//     subscriptions.clear();
//   }
// 
//   private void unsubscribe(Subscription subscription) {
//     if (subscription != null && !subscription.isUnsubscribed()) {
//       subscription.unsubscribe();
//     }
//   }
// }
// 
// Path: mvvm-life-cycle/src/main/java/me/henrytao/mvvmlifecycle/rx/UnsubscribeLifeCycle.java
// public enum UnsubscribeLifeCycle {
//   PAUSE,
//   STOP,
//   DESTROY_VIEW,
//   DESTROY
// }


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import me.henrytao.mvvmlifecycle.rx.SubscriptionManager;
import me.henrytao.mvvmlifecycle.rx.UnsubscribeLifeCycle;
import rx.Subscription;
/*
 * Copyright 2016 "Henry Tao <hi@henrytao.me>"
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.henrytao.mvvmlifecycle;




/**
 * Created by henrytao on 11/10/15.
 * Reference: http://developer.android.com/guide/components/fragments.html
 */
public abstract class MVVMFragment extends android.support.v4.app.Fragment implements MVVMLifeCycle, MVVMObserver {

  public abstract View onInflateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

  protected List<MVVMViewModel> mViewModels = new ArrayList<>();

  private List<MVVMViewModel> mAdapterViewModels = new ArrayList<>();

  private boolean mIsDestroy;

  private boolean mIsDestroyView;

  private boolean mIsPause;

  private boolean mIsStop;

  private Bundle mSavedInstanceState;

  private boolean mShouldRestoreInstanceState;

  private Constants.State mState;

  private SubscriptionManager mSubscriptionManager = new SubscriptionManager();

  @Override
  public void addAdapterViewModel(MVVMViewModel viewModel) throws IllegalAccessException {
    addViewModel(viewModel);
    if (!mAdapterViewModels.contains(viewModel)) {
      mAdapterViewModels.add(viewModel);
    }
    throw new IllegalAccessException("This method should be used with care");
  }

  @Override
  public void addViewModel(MVVMViewModel viewModel) {
    if (!mViewModels.contains(viewModel)) {
      mViewModels.add(viewModel);
      propagateLifeCycle(viewModel);
    }
  }

  @Override
// Path: mvvm-life-cycle/src/main/java/me/henrytao/mvvmlifecycle/rx/SubscriptionManager.java
// public class SubscriptionManager implements ISubscriptionManager {
// 
//   private Map<UnsubscribeLifeCycle, Map<String, Subscription>> mSubscriptions;
// 
//   public SubscriptionManager() {
//     mSubscriptions = new HashMap<>();
//   }
// 
//   @Override
//   public void manageSubscription(Subscription subscription, UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     manageSubscription(UUID.randomUUID().toString(), subscription, unsubscribeLifeCycle);
//   }
// 
//   @Override
//   public void manageSubscription(String id, Subscription subscription, UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     Map<String, Subscription> subscriptions = getSubscriptions(unsubscribeLifeCycle);
//     if (subscriptions.containsKey(id)) {
//       unsubscribe(id);
//     }
//     subscriptions.put(id, subscription);
//   }
// 
//   @Override
//   public void unsubscribe(UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     unsubscribe(getSubscriptions(unsubscribeLifeCycle));
//   }
// 
//   @Override
//   public void unsubscribe() {
//     for (Map.Entry<UnsubscribeLifeCycle, Map<String, Subscription>> entry : mSubscriptions.entrySet()) {
//       unsubscribe(entry.getValue());
//     }
//     mSubscriptions.clear();
//   }
// 
//   @Override
//   public void unsubscribe(String id) {
//     for (Map.Entry<UnsubscribeLifeCycle, Map<String, Subscription>> entry : mSubscriptions.entrySet()) {
//       unsubscribe(entry.getValue().get(id));
//     }
//   }
// 
//   private Map<String, Subscription> getSubscriptions(UnsubscribeLifeCycle unsubscribeLifeCycle) {
//     Map<String, Subscription> subscriptions = mSubscriptions.get(unsubscribeLifeCycle);
//     if (subscriptions == null) {
//       subscriptions = new HashMap<>();
//       mSubscriptions.put(unsubscribeLifeCycle, subscriptions);
//     }
//     return subscriptions;
//   }
// 
//   private void unsubscribe(Map<String, Subscription> subscriptions) {
//     for (Map.Entry<String, Subscription> entry : subscriptions.entrySet()) {
//       unsubscribe(entry.getValue());
//     }
//     subscriptions.clear();
//   }
// 
//   private void unsubscribe(Subscription subscription) {
//     if (subscription != null && !subscription.isUnsubscribed()) {
//       subscription.unsubscribe();
//     }
//   }
// }
// 
// Path: mvvm-life-cycle/src/main/java/me/henrytao/mvvmlifecycle/rx/UnsubscribeLifeCycle.java
// public enum UnsubscribeLifeCycle {
//   PAUSE,
//   STOP,
//   DESTROY_VIEW,
//   DESTROY
// }


// Path: mvvm-life-cycle/src/main/java/me/henrytao/mvvmlifecycle/MVVMFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import me.henrytao.mvvmlifecycle.rx.SubscriptionManager;
import me.henrytao.mvvmlifecycle.rx.UnsubscribeLifeCycle;
import rx.Subscription;

/*
 * Copyright 2016 "Henry Tao <hi@henrytao.me>"
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.henrytao.mvvmlifecycle;




/**
 * Created by henrytao on 11/10/15.
 * Reference: http://developer.android.com/guide/components/fragments.html
 */
public abstract class MVVMFragment extends android.support.v4.app.Fragment implements MVVMLifeCycle, MVVMObserver {

  public abstract View onInflateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

  protected List<MVVMViewModel> mViewModels = new ArrayList<>();

  private List<MVVMViewModel> mAdapterViewModels = new ArrayList<>();

  private boolean mIsDestroy;

  private boolean mIsDestroyView;

  private boolean mIsPause;

  private boolean mIsStop;

  private Bundle mSavedInstanceState;

  private boolean mShouldRestoreInstanceState;

  private Constants.State mState;

  private SubscriptionManager mSubscriptionManager = new SubscriptionManager();

  @Override
  public void addAdapterViewModel(MVVMViewModel viewModel) throws IllegalAccessException {
    addViewModel(viewModel);
    if (!mAdapterViewModels.contains(viewModel)) {
      mAdapterViewModels.add(viewModel);
    }
    throw new IllegalAccessException("This method should be used with care");
  }

  @Override
  public void addViewModel(MVVMViewModel viewModel) {
    if (!mViewModels.contains(viewModel)) {
      mViewModels.add(viewModel);
      propagateLifeCycle(viewModel);
    }
  }

  @Override
  public void manageSubscription(UnsubscribeLifeCycle unsubscribeLifeCycle, Subscription... subscriptions) {