-
Notifications
You must be signed in to change notification settings - Fork 137
/
ShotPresenter.java
75 lines (61 loc) · 2.38 KB
/
ShotPresenter.java
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
package com.hitherejoe.bourboncorecommon.ui.shot;
import com.hitherejoe.bourboncorecommon.data.DataManager;
import com.hitherejoe.bourboncorecommon.data.model.Comment;
import com.hitherejoe.bourboncorecommon.ui.base.BasePresenter;
import java.util.List;
import javax.inject.Inject;
import rx.Single;
import rx.SingleSubscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import timber.log.Timber;
public class ShotPresenter extends BasePresenter<ShotMvpView> {
// We'll handle pagination in the future...
public static final int SHOT_COUNT = 10;
public static final int SHOT_PAGE = 0;
private final DataManager mDataManager;
private Subscription mSubscription;
private List<Comment> mComments;
@Inject
public ShotPresenter(DataManager dataManager) {
mDataManager = dataManager;
}
@Override
public void detachView() {
super.detachView();
if (mSubscription != null) mSubscription.unsubscribe();
}
public void getComments(int id, int perPage, int page) {
checkViewAttached();
getMvpView().showProgress();
Single<List<Comment>> single;
if (mComments == null) {
single = mDataManager.getComments(id, perPage, page);
} else {
single = Single.just(mComments);
}
mSubscription = single
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new SingleSubscriber<List<Comment>>() {
@Override
public void onSuccess(List<Comment> comments) {
mComments = comments;
getMvpView().hideProgress();
if (comments.isEmpty()) {
getMvpView().showEmptyComments();
} else {
getMvpView().showComments(comments);
}
getMvpView().showCommentsTitle(!comments.isEmpty());
}
@Override
public void onError(Throwable error) {
Timber.e(error, "There was an error retrieving the comments");
getMvpView().hideProgress();
getMvpView().showError();
}
});
}
}