package com.nilhcem.droidcontn.ui.schedule.pager;

import com.nilhcem.droidcontn.data.app.DataProvider;
import com.nilhcem.droidcontn.data.app.model.Schedule;
import com.nilhcem.droidcontn.ui.BaseFragmentPresenter;

import icepick.State;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import timber.log.Timber;

public class SchedulePagerPresenter extends BaseFragmentPresenter<SchedulePagerView> {

    @State Schedule schedule;

    private final DataProvider dataProvider;
    private Subscription scheduleSubscription;

    public SchedulePagerPresenter(SchedulePagerView view, DataProvider dataProvider) {
        super(view);
        this.dataProvider = dataProvider;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (schedule == null) {
            loadData();
        } else {
            this.view.displaySchedule(schedule);
        }
    }

    public void reloadData() {
        loadData();
    }

    @Override
    public void onStop() {
        if (scheduleSubscription != null) {
            scheduleSubscription.unsubscribe();
        }
        super.onStop();
    }

    private void loadData() {
        scheduleSubscription = dataProvider.getSchedule()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(scheduleDays -> {
                    schedule = scheduleDays;
                    view.displaySchedule(schedule);
                }, throwable -> Timber.e(throwable, "Error getting schedule"), () -> {
                    if (schedule == null) {
                        view.displayLoadingError();
                    }
                });
    }
}

@@ -40,24 +40,121 @@
 ule.pager;%0A%0A
+import android.os.Bundle;%0Aimport android.support.annotation.Nullable;%0Aimport android.view.View;%0A%0A
 import com.n
@@ -865,40 +865,122 @@
 d on
-Start() %7B%0A        super.onStart(
+ViewCreated(View view, @Nullable Bundle savedInstanceState) %7B%0A        super.onViewCreated(view, savedInstanceState
 );%0A 

Fix state change issue