/*
* Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
* 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
*
* https://mindorks.com/license/apache-v2
*
* 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.mindorks.framework.mvp.ui.splash;
import com.mindorks.framework.mvp.R;
import com.mindorks.framework.mvp.data.DataManager;
import com.mindorks.framework.mvp.ui.base.BasePresenter;
import com.mindorks.framework.mvp.utils.rx.SchedulerProvider;
import javax.inject.Inject;
import io.reactivex.ObservableSource;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
/**
* Created by janisharali on 27/01/17.
*/
public class SplashPresenter<V extends SplashMvpView> extends BasePresenter<V>
implements SplashMvpPresenter<V> {
@Inject
public SplashPresenter(DataManager dataManager,
SchedulerProvider schedulerProvider,
CompositeDisposable compositeDisposable) {
super(dataManager, schedulerProvider, compositeDisposable);
}
@Override
public void onAttach(V mvpView) {
super.onAttach(mvpView);
getMvpView().startSyncService();
getCompositeDisposable().add(getDataManager()
.seedDatabaseQuestions()
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.concatMap(new Function<Boolean, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(Boolean aBoolean) throws Exception {
return getDataManager().seedDatabaseOptions();
}
})
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (!isViewAttached()) {
return;
}
decideNextActivity();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (!isViewAttached()) {
return;
}
getMvpView().onError(R.string.some_error);
decideNextActivity();
}
}));
}
private void decideNextActivity() {
if (getDataManager().getCurrentUserLoggedInMode()
== DataManager.LoggedInMode.LOGGED_IN_MODE_LOGGED_OUT.getType()) {
getMvpView().openLoginActivity();
} else {
getMvpView().openMainActivity();
}
}
}