BasePresenter.java
2.0 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
package com.xdy.commonlibrary.mvp;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
/**
* @author jianghongbo
* @version 1.0
* @file BasePresenter.java
* @brief
* @date 2017/6/4
* Copyright (c) 2017, 学点云
* All rights reserved.
*/
public class BasePresenter<M extends IModel, V extends BaseView> implements presenter {
protected final String TAG = this.getClass().getSimpleName();
protected CompositeSubscription mCompositeSubscription;
protected M mModel;
protected V mRootView;
public BasePresenter(M model, V rootView) {
this.mModel = model;
this.mRootView = rootView;
onStart();
}
public BasePresenter(V rootView) {
this.mRootView = rootView;
onStart();
}
public BasePresenter() {
onStart();
}
/**
* 控制器被初始化的时候自动调用
*/
@Override
public void onStart() {
}
@Override
public void onDestroy() {
unSubscribe();//解除订阅
if (mModel != null) {
mModel.onDestory();
this.mModel = null;
}
if (mRootView != null)
mRootView.hideLoading();
this.mRootView = null;
this.mCompositeSubscription = null;
}
protected void addSubscrebe(Subscription subscription) {
if (mCompositeSubscription == null) {
mCompositeSubscription = new CompositeSubscription();
}
mCompositeSubscription.add(subscription);//将所有subscription放入,集中处理
}
protected void unSubscribe() {
if (mCompositeSubscription != null) {
mCompositeSubscription.unsubscribe();//保证activity结束时取消所有正在执行的订阅
}
}
@Override
public void unSubscribe(Subscription subscription) {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();//保证activity结束时取消所有正在执行的订阅
}
}
}