package io.github.droidkaigi.confsched2017.view.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.github.droidkaigi.confsched2017.R;
import io.github.droidkaigi.confsched2017.repository.sessions.MySessionsRepository;
import io.github.droidkaigi.confsched2017.repository.sessions.SessionsRepository;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivity {

    private static final String TAG = SplashActivity.class.getSimpleName();

    private static final int MINIMUM_LOADING_TIME = 1500;

    @Inject
    CompositeDisposable compositeDisposable;

    @Inject
    SessionsRepository sessionsRepository;

    @Inject
    MySessionsRepository mySessionsRepository;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        DataBindingUtil.setContentView(this, R.layout.activity_splash);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            findViewById(android.R.id.content).setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadSessionsForCache();
    }

    @Override
    protected void onStop() {
        super.onStop();
        compositeDisposable.dispose();
    }

    private void loadSessionsForCache() {
        Disposable disposable = Single.zip(sessionsRepository.findAll(Locale.getDefault()),
                mySessionsRepository.findAll(),
                Single.timer(MINIMUM_LOADING_TIME, TimeUnit.MILLISECONDS),
                (sessions, mySessions, obj) -> Observable.empty())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (isFinishing()) return;
                    startActivity(MainActivity.createIntent(SplashActivity.this));
                })
                .subscribe();
        compositeDisposable.add(disposable);
    }

}

@@ -56,71 +56,8 @@
 y;%0A%0A
-import android.content.Context;%0Aimport android.content.Intent;%0A
 impo
@@ -151,35 +151,8 @@
 le;%0A
-import android.os.Handler;%0A
 impo
@@ -735,16 +735,42 @@
 edulers;
+%0Aimport timber.log.Timber;
 %0A%0Apublic
@@ -2465,16 +2465,182 @@
 bscribe(
+objectObservable -%3E Timber.tag(TAG).d(%22Succeeded in loading sessions.%22),%0A                        throwable -%3E Timber.tag(TAG).e(throwable, %22Failed to load sessions.%22)
 );%0A     

Fix crash in splash when offline