-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSplashPresenter.java
More file actions
126 lines (108 loc) · 4.85 KB
/
SplashPresenter.java
File metadata and controls
126 lines (108 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.riseapps.thebeauties.ui.splash;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.text.format.DateUtils;
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork;
import com.riseapps.thebeauties.R;
import com.riseapps.thebeauties.TheBeautiesApplication;
import com.riseapps.thebeauties.network.FetchDataInteractor;
import com.riseapps.thebeauties.network.TheBeautiesApi;
import com.riseapps.thebeauties.storage.preferences.Preferences;
import com.riseapps.thebeauties.ui.base.BasePresenter;
import com.riseapps.thebeauties.ui.base.PresenterEvent;
import com.riseapps.thebeauties.util.ErrorMessagesUtil;
import com.riseapps.thebeauties.util.InternetConnectionManager;
import javax.inject.Inject;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.HttpException;
public class SplashPresenter extends BasePresenter<SplashView> {
private static final long SPLASH_DURATION = 2 * DateUtils.SECOND_IN_MILLIS;
@Inject
TheBeautiesApplication theBeautiesApplication;
@Inject
Preferences preferences;
@Inject
InternetConnectionManager internetConnectionManager;
@Inject
FetchDataInteractor fetchDataInteractor;
private Handler handler;
private Disposable networkConnectivityDisposable;
public SplashPresenter() {
TheBeautiesApplication.getInstance().getApplicationComponent().inject(this);
}
@Override
protected void onViewAttached() {
super.onViewAttached();
startHandler();
}
@Override
protected void onViewDetached() {
super.onViewDetached();
destroyHandler();
disposeNetworkConnectivity();
}
private void getToken() {
if (preferences.getToken() != null && getView() != null) {
getView().startMainActivity();
} else {
fetchDataInteractor.getToken()
.doOnSuccess(tokenResponseModel -> preferences.saveToken(tokenResponseModel.getTokenDataModel().getToken()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(bindUntilEvent(PresenterEvent.DESTROY))
.subscribe(tokenResponseModel -> {
if (getView() != null) {
getView().startMainActivity();
}
disposeNetworkConnectivity();
}, throwable -> {
throwable.printStackTrace();
if (throwable instanceof HttpException) {
final HttpException httpException = (HttpException) throwable;
String errorMessage;
switch (httpException.code()) {
case TheBeautiesApi.BAD_REQUEST:
errorMessage = ErrorMessagesUtil.toStringError(httpException.response().errorBody().string());
break;
default:
errorMessage = theBeautiesApplication.getString(R.string.unknown_server_error);
break;
}
if (getView() != null) {
getView().showSnackbar(errorMessage, Snackbar.LENGTH_SHORT);
}
}
});
}
}
private void observeNetworkConnectivity() {
networkConnectivityDisposable = ReactiveNetwork.observeNetworkConnectivity(theBeautiesApplication)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(connectivity -> {
if (connectivity != null && connectivity.isAvailable() && getView() != null) {
getView().hideSnackbar();
getToken();
} else if ((connectivity == null || !connectivity.isAvailable()) && getView() != null) {
getView().showSnackbar(R.string.please_check_your_network_connection, Snackbar.LENGTH_INDEFINITE);
}
}, Throwable::printStackTrace);
}
private void disposeNetworkConnectivity() {
if (networkConnectivityDisposable != null && !networkConnectivityDisposable.isDisposed()) {
networkConnectivityDisposable.dispose();
}
}
private void startHandler() {
handler = new Handler();
handler.postDelayed(runnable, SPLASH_DURATION);
}
private void destroyHandler() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
}
private Runnable runnable = this::observeNetworkConnectivity;
}