Search in sources :

Example 1 with RestorableViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState in project mosby by sockeqwe.

the class ActivityMvpViewStateDelegateImpl method onPostCreate.

@Override
public void onPostCreate(Bundle bundle) {
    super.onPostCreate(bundle);
    if (mosbyViewId != null) {
        VS viewState = PresenterManager.getViewState(activity, mosbyViewId);
        if (viewState != null) {
            // 
            // ViewState restored from PresenterManager
            // 
            setViewState(viewState, true, true);
            if (DEBUG) {
                Log.d(DEBUG_TAG, "ViewState reused from Mosby internal cache for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    VS viewState = delegateCallback.createViewState();
    if (viewState == null) {
        throw new NullPointerException("ViewState returned from createViewState() is null! MvpView that has returned null as ViewState is: " + delegateCallback.getMvpView());
    }
    if (bundle != null && viewState instanceof RestorableViewState) {
        // A little bit hacky that we need an instance of the viewstate to restore a view state
        // (may creates another view state object) but I don't know any better way :)
        RestorableViewState restoredViewState = ((RestorableViewState) viewState).restoreInstanceState(bundle);
        if (restoredViewState != null) {
            // 
            // ViewState restored from bundle
            // 
            viewState = (VS) restoredViewState;
            setViewState(viewState, true, false);
            if (keepPresenterInstance) {
                if (mosbyViewId == null) {
                    throw new IllegalStateException("The (internal) Mosby View id is null although bundle is not null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
                }
                PresenterManager.putViewState(activity, mosbyViewId, viewState);
            }
            if (DEBUG) {
                Log.d(DEBUG_TAG, "Recreated ViewState from bundle for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    // 
    if (keepPresenterInstance) {
        if (mosbyViewId == null) {
            throw new IllegalStateException("The (internal) Mosby View id is null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
        }
        PresenterManager.putViewState(activity, mosbyViewId, viewState);
    }
    setViewState(viewState, false, false);
    if (DEBUG) {
        Log.d(DEBUG_TAG, "Created a new ViewState instance for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
    }
    delegateCallback.onNewViewStateInstance();
}
Also used : RestorableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)

Example 2 with RestorableViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState in project mosby by sockeqwe.

the class FragmentMvpViewStateDelegateImpl method restoreViewStateOrRecreateViewStateAfterProcessDeath.

private VS restoreViewStateOrRecreateViewStateAfterProcessDeath(Bundle bundle) {
    if (bundle == null) {
        throw new NullPointerException("Bundle is null. This should never be the case" + "Please report this issue at https://github.com/sockeqwe/mosby/issues");
    }
    if (mosbyViewId == null) {
        throw new NullPointerException("The (internal) Mosby View id is null although bundle is not null. " + "This should never be the case while restoring ViewState instance. " + "Please report this issue at https://github.com/sockeqwe/mosby/issues");
    }
    // 
    // Try to restore ViewState from PresenterManager
    // 
    VS viewState = PresenterManager.getViewState(fragment.getActivity(), mosbyViewId);
    if (viewState != null) {
        applyViewState = true;
        applyViewStateFromMemory = true;
        if (DEBUG) {
            Log.d(DEBUG_TAG, "ViewState reused from Mosby internal cache for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
        }
        return viewState;
    }
    // 
    // Try to restore viewstate from bundle
    // 
    viewState = delegateCallback.createViewState();
    if (viewState == null) {
        throw new NullPointerException("ViewState returned from createViewState() is null! MvpView that has returned null as ViewState is: " + delegateCallback.getMvpView());
    }
    if (viewState instanceof RestorableViewState) {
        // A little bit hacky that we need an instance of the viewstate to restore a view state
        // (may creates another view state object) but I don't know any better way :)
        RestorableViewState restoredViewState = ((RestorableViewState) viewState).restoreInstanceState(bundle);
        if (restoredViewState != null) {
            // 
            // ViewState restored from bundle
            // 
            viewState = (VS) restoredViewState;
            applyViewState = true;
            applyViewStateFromMemory = false;
            if (keepPresenterInstanceDuringScreenOrientationChanges) {
                PresenterManager.putViewState(getActivity(), mosbyViewId, viewState);
            }
            if (DEBUG) {
                Log.d(DEBUG_TAG, "Recreated ViewState from bundle for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return viewState;
        }
    }
    // 
    // Entirely new ViewState has been created, typically because process death and mosby view id points to
    // a  old id but view got a new one because of process death.
    // 
    applyViewState = false;
    applyViewStateFromMemory = false;
    if (keepPresenterInstanceDuringScreenOrientationChanges) {
        PresenterManager.putViewState(getActivity(), mosbyViewId, viewState);
    }
    if (DEBUG) {
        Log.d(DEBUG_TAG, "Created a new ViewState instance for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
    }
    return viewState;
}
Also used : RestorableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)

Example 3 with RestorableViewState

use of com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState in project mosby by sockeqwe.

the class FragmentMvpViewStateDelegateImpl method onViewCreated.

@Override
public void onViewCreated(View view, Bundle bundle) {
    super.onViewCreated(view, bundle);
    if (bundle != null && keepPresenterInstanceDuringScreenOrientationChanges) {
        mosbyViewId = bundle.getString(KEY_MOSBY_VIEW_ID);
        if (DEBUG) {
            Log.d(DEBUG_TAG, "MosbyView ID = " + mosbyViewId + " for MvpView: " + delegateCallback.getMvpView());
        }
    }
    if (mosbyViewId != null) {
        VS viewState = PresenterManager.getViewState(fragment.getActivity(), mosbyViewId);
        if (viewState != null) {
            //
            // ViewState restored from PresenterManager
            //
            delegateCallback.setViewState(viewState);
            applyViewState = true;
            applyViewStateFromMemory = true;
            if (DEBUG) {
                Log.d(DEBUG_TAG, "ViewState reused from Mosby internal cache for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    VS viewState = delegateCallback.createViewState();
    if (viewState == null) {
        throw new NullPointerException("ViewState returned from createViewState() is null! MvpView that has returned null as ViewState is: " + delegateCallback.getMvpView());
    }
    if (bundle != null && viewState instanceof RestorableViewState) {
        // A little bit hacky that we need an instance of the viewstate to restore a view state
        // (may creates another view state object) but I don't know any better way :)
        RestorableViewState restoredViewState = ((RestorableViewState) viewState).restoreInstanceState(bundle);
        if (restoredViewState != null) {
            //
            // ViewState restored from bundle
            //
            viewState = (VS) restoredViewState;
            delegateCallback.setViewState(viewState);
            applyViewState = true;
            applyViewStateFromMemory = false;
            if (keepPresenterInstanceDuringScreenOrientationChanges) {
                if (mosbyViewId == null) {
                    throw new IllegalStateException("The (internal) Mosby View id is null although bundle is not null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
                }
                PresenterManager.putViewState(fragment.getActivity(), mosbyViewId, viewState);
            }
            if (DEBUG) {
                Log.d(DEBUG_TAG, "Recreated ViewState from bundle for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    //
    if (keepPresenterInstanceDuringScreenOrientationChanges) {
        if (mosbyViewId == null) {
            throw new IllegalStateException("The (internal) Mosby View id is null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
        }
        PresenterManager.putViewState(fragment.getActivity(), mosbyViewId, viewState);
    }
    delegateCallback.setViewState(viewState);
    applyViewState = false;
    applyViewStateFromMemory = false;
    if (DEBUG) {
        Log.d(DEBUG_TAG, "Created a new ViewState instance for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
    }
}
Also used : RestorableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)

Aggregations

RestorableViewState (com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)3