/* * Copyright (c) 2015 PocketHub * * 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 com.github.pockethub.android.ui.roboactivities; import android.app.Activity; import android.os.Bundle; import android.support.annotation.CallSuper; import android.support.v4.app.Fragment; import android.view.View; import com.trello.rxlifecycle2.LifecycleProvider; import com.trello.rxlifecycle2.LifecycleTransformer; import com.trello.rxlifecycle2.RxLifecycle; import com.trello.rxlifecycle2.android.FragmentEvent; import com.trello.rxlifecycle2.android.RxLifecycleAndroid; import io.reactivex.Observable; import io.reactivex.subjects.BehaviorSubject; import roboguice.RoboGuice; public abstract class RoboSupportFragment extends Fragment implements LifecycleProvider<FragmentEvent> { private final BehaviorSubject<FragmentEvent> lifecycleSubject = BehaviorSubject.create(); @Override public final Observable<FragmentEvent> lifecycle() { return lifecycleSubject; } @Override public final <T> LifecycleTransformer<T> bindUntilEvent(FragmentEvent event) { return RxLifecycle.bindUntilEvent(lifecycleSubject, event); } @Override public final <T> LifecycleTransformer<T> bindToLifecycle() { return RxLifecycleAndroid.bindFragment(lifecycleSubject); } @Override @CallSuper public void onAttach(Activity activity) { super.onAttach(activity); lifecycleSubject.onNext(FragmentEvent.ATTACH); } @Override @CallSuper public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RoboGuice.getInjector(getActivity()).injectMembersWithoutViews(this); lifecycleSubject.onNext(FragmentEvent.CREATE); } @Override @CallSuper public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); RoboGuice.getInjector(getActivity()).injectViewMembers(this); lifecycleSubject.onNext(FragmentEvent.CREATE_VIEW); } @Override @CallSuper public void onStart() { super.onStart(); lifecycleSubject.onNext(FragmentEvent.START); } @Override @CallSuper public void onResume() { super.onResume(); lifecycleSubject.onNext(FragmentEvent.RESUME); } @Override @CallSuper public void onPause() { lifecycleSubject.onNext(FragmentEvent.PAUSE); super.onPause(); } @Override @CallSuper public void onStop() { lifecycleSubject.onNext(FragmentEvent.STOP); super.onStop(); } @Override @CallSuper public void onDestroyView() { lifecycleSubject.onNext(FragmentEvent.DESTROY_VIEW); super.onDestroyView(); } @Override @CallSuper public void onDestroy() { lifecycleSubject.onNext(FragmentEvent.DESTROY); super.onDestroy(); } @Override @CallSuper public void onDetach() { lifecycleSubject.onNext(FragmentEvent.DETACH); super.onDetach(); } }