23 | * @param
24 | */
25 | public abstract class MvpActivity> extends RxActivity implements IMvpView, MvpDelegateCallback {
26 |
27 | protected ActivityMvpDelegate mvpDelegate;
28 |
29 | /**
30 | * 获取 Presenter 数组
31 | */
32 | protected abstract P[] getPresenterArray();
33 |
34 | @Override
35 | public P[] getPresenter() {
36 | return getPresenterArray();
37 | }
38 |
39 | @Override
40 | public V[] getMvpView() {
41 | V[] view = null;
42 | P[] pArray = getPresenter();
43 | if (pArray != null) {
44 | view = (V[]) new IMvpView[pArray.length];
45 | for (int i = 0; i < pArray.length; i++) {
46 | view[i] = (V) this;
47 | }
48 | }
49 | return view;
50 | }
51 |
52 | @NonNull
53 | protected ActivityMvpDelegate getMvpDelegate() {
54 | if (mvpDelegate == null) {
55 | mvpDelegate = new ActivityMvpDelegateImpl(this, this);
56 | }
57 | return mvpDelegate;
58 | }
59 |
60 | @Override
61 | protected void onCreate(Bundle savedInstanceState) {
62 | super.onCreate(savedInstanceState);
63 | getMvpDelegate().onCreate(savedInstanceState);
64 | }
65 |
66 | @Override
67 | protected void onDestroy() {
68 | super.onDestroy();
69 | getMvpDelegate().onDestroy();
70 | }
71 |
72 | @Override
73 | protected void onSaveInstanceState(Bundle outState) {
74 | super.onSaveInstanceState(outState);
75 | getMvpDelegate().onSaveInstanceState(outState);
76 | }
77 |
78 | @Override
79 | protected void onPause() {
80 | super.onPause();
81 | getMvpDelegate().onPause();
82 | }
83 |
84 | @Override
85 | protected void onResume() {
86 | super.onResume();
87 | getMvpDelegate().onResume();
88 | }
89 |
90 | @Override
91 | protected void onStart() {
92 | super.onStart();
93 | getMvpDelegate().onStart();
94 | }
95 |
96 | @Override
97 | protected void onStop() {
98 | super.onStop();
99 | getMvpDelegate().onStop();
100 | }
101 |
102 | @Override
103 | protected void onRestart() {
104 | super.onRestart();
105 | getMvpDelegate().onRestart();
106 | }
107 |
108 | @Override
109 | public void onContentChanged() {
110 | super.onContentChanged();
111 | getMvpDelegate().onContentChanged();
112 | }
113 |
114 | @Override
115 | protected void onPostCreate(Bundle savedInstanceState) {
116 | super.onPostCreate(savedInstanceState);
117 | getMvpDelegate().onPostCreate(savedInstanceState);
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/MvpAppCompatActivity.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.util.Log;
6 |
7 | import com.r.mvp.cn.delegate.ActivityMvpDelegate;
8 | import com.r.mvp.cn.delegate.ActivityMvpDelegateImpl;
9 | import com.r.mvp.cn.delegate.MvpDelegateCallback;
10 | import com.r.mvp.cn.root.IMvpPresenter;
11 | import com.r.mvp.cn.root.IMvpView;
12 | import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
13 |
14 | import java.lang.reflect.ParameterizedType;
15 |
16 |
17 | /**
18 | * MvpAppCompatActivity
19 | * 备注:
20 | * 1.XXActivity 继承 MvpActivity,当页面存在 Presenter 时,具体 Activity 需要调用 setPresenter(P... presenter)
21 | * 2.由于此框架集合了 RxLifecycle 因此本 Activity 继承自 RxActivity (开发者也可以直接继承 Activity)
22 | * 3.支持一个 Activity 存在多个 Presenter
23 | *
24 | * @param
25 | * @param
26 | */
27 | public abstract class MvpAppCompatActivity> extends RxAppCompatActivity implements IMvpView, MvpDelegateCallback {
28 |
29 | protected ActivityMvpDelegate mvpDelegate;
30 |
31 | /**
32 | * 获取 Presenter 数组
33 | */
34 | protected abstract P[] getPresenterArray();
35 |
36 | @Override
37 | public P[] getPresenter() {
38 | return getPresenterArray();
39 | }
40 |
41 | @Override
42 | public V[] getMvpView() {
43 | V[] view = null;
44 | P[] pArray = getPresenter();
45 | if (pArray != null) {
46 | view = (V[]) new IMvpView[pArray.length];
47 | for (int i = 0; i < pArray.length; i++) {
48 | view[i] = (V) this;
49 | }
50 | }
51 | return view;
52 | }
53 |
54 |
55 | @NonNull
56 | protected ActivityMvpDelegate getMvpDelegate() {
57 | if (mvpDelegate == null) {
58 | mvpDelegate = new ActivityMvpDelegateImpl(this, this);
59 | }
60 | return mvpDelegate;
61 | }
62 |
63 | @Override
64 | protected void onCreate(Bundle savedInstanceState) {
65 | super.onCreate(savedInstanceState);
66 | getMvpDelegate().onCreate(savedInstanceState);
67 | }
68 |
69 | @Override
70 | protected void onDestroy() {
71 | super.onDestroy();
72 | getMvpDelegate().onDestroy();
73 | }
74 |
75 | @Override
76 | protected void onSaveInstanceState(Bundle outState) {
77 | super.onSaveInstanceState(outState);
78 | getMvpDelegate().onSaveInstanceState(outState);
79 | }
80 |
81 | @Override
82 | protected void onPause() {
83 | super.onPause();
84 | getMvpDelegate().onPause();
85 | }
86 |
87 | @Override
88 | protected void onResume() {
89 | super.onResume();
90 | getMvpDelegate().onResume();
91 | }
92 |
93 | @Override
94 | protected void onStart() {
95 | super.onStart();
96 | getMvpDelegate().onStart();
97 | }
98 |
99 | @Override
100 | protected void onStop() {
101 | super.onStop();
102 | getMvpDelegate().onStop();
103 | }
104 |
105 | @Override
106 | protected void onRestart() {
107 | super.onRestart();
108 | getMvpDelegate().onRestart();
109 | }
110 |
111 | @Override
112 | public void onContentChanged() {
113 | super.onContentChanged();
114 | getMvpDelegate().onContentChanged();
115 | }
116 |
117 | @Override
118 | protected void onPostCreate(Bundle savedInstanceState) {
119 | super.onPostCreate(savedInstanceState);
120 | getMvpDelegate().onPostCreate(savedInstanceState);
121 | }
122 |
123 | }
124 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/MvpFragment.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.support.annotation.NonNull;
6 | import android.support.annotation.Nullable;
7 | import android.view.View;
8 |
9 | import com.r.mvp.cn.delegate.ActivityMvpDelegate;
10 | import com.r.mvp.cn.delegate.ActivityMvpDelegateImpl;
11 | import com.r.mvp.cn.delegate.FragmentMvpDelegate;
12 | import com.r.mvp.cn.delegate.FragmentMvpDelegateImpl;
13 | import com.r.mvp.cn.delegate.MvpDelegateCallback;
14 | import com.r.mvp.cn.root.IMvpPresenter;
15 | import com.r.mvp.cn.root.IMvpView;
16 | import com.trello.rxlifecycle2.components.support.RxFragment;
17 |
18 |
19 | /**
20 | * MVPFragment
21 | * 备注:
22 | * 1.XXFragment 继承 MvpFragment,当页面存在 Presenter 时,具体 Fragment 需要调用 setPresenter(P... presenter)
23 | * 2.由于此框架集合了 RxLifecycle 因此本 Fragment 继承自 RxFragment (开发者也可以直接继承 Fragment)
24 | * 3.支持一个 Fragment 存在多个 Presenter
25 | *
26 | * @param
27 | * @param
28 | */
29 | public abstract class MvpFragment> extends RxFragment implements IMvpView, MvpDelegateCallback {
30 |
31 | protected FragmentMvpDelegate mvpDelegate;
32 |
33 | /**
34 | * 获取 Presenter 数组
35 | */
36 | protected abstract P[] getPresenterArray();
37 |
38 | @Override
39 | public P[] getPresenter() {
40 | return getPresenterArray();
41 | }
42 |
43 | @Override
44 | public V[] getMvpView() {
45 | V[] view = null;
46 | P[] pArray = getPresenter();
47 | if (pArray != null) {
48 | view = (V[]) new IMvpView[pArray.length];
49 | for (int i = 0; i < pArray.length; i++) {
50 | view[i] = (V) this;
51 | }
52 | }
53 | return view;
54 | }
55 |
56 | @NonNull
57 | protected FragmentMvpDelegate getMvpDelegate() {
58 | if (mvpDelegate == null) {
59 | mvpDelegate = new FragmentMvpDelegateImpl(this, this);
60 | }
61 | return mvpDelegate;
62 | }
63 |
64 | @Override
65 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
66 | super.onViewCreated(view, savedInstanceState);
67 | getMvpDelegate().onViewCreated(view, savedInstanceState);
68 | }
69 |
70 | @Override
71 | public void onDestroyView() {
72 | super.onDestroyView();
73 | getMvpDelegate().onDestroyView();
74 | }
75 |
76 | @Override
77 | public void onCreate(Bundle savedInstanceState) {
78 | super.onCreate(savedInstanceState);
79 | getMvpDelegate().onCreate(savedInstanceState);
80 | }
81 |
82 | @Override
83 | public void onDestroy() {
84 | super.onDestroy();
85 | getMvpDelegate().onDestroy();
86 | }
87 |
88 | @Override
89 | public void onPause() {
90 | super.onPause();
91 | getMvpDelegate().onPause();
92 | }
93 |
94 | @Override
95 | public void onResume() {
96 | super.onResume();
97 | getMvpDelegate().onResume();
98 | }
99 |
100 | @Override
101 | public void onStart() {
102 | super.onStart();
103 | getMvpDelegate().onStart();
104 | }
105 |
106 | @Override
107 | public void onStop() {
108 | super.onStop();
109 | getMvpDelegate().onStop();
110 | }
111 |
112 | @Override
113 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
114 | super.onActivityCreated(savedInstanceState);
115 | getMvpDelegate().onActivityCreated(savedInstanceState);
116 | }
117 |
118 | @Override
119 | public void onAttach(Activity activity) {
120 | super.onAttach(activity);
121 | getMvpDelegate().onAttach(activity);
122 | }
123 |
124 | @Override
125 | public void onDetach() {
126 | super.onDetach();
127 | getMvpDelegate().onDetach();
128 | }
129 |
130 | @Override
131 | public void onSaveInstanceState(Bundle outState) {
132 | super.onSaveInstanceState(outState);
133 | getMvpDelegate().onSaveInstanceState(outState);
134 | }
135 | }
136 |
137 |
138 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/MvpFragmentActivity.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 |
6 | import com.r.mvp.cn.delegate.ActivityMvpDelegate;
7 | import com.r.mvp.cn.delegate.ActivityMvpDelegateImpl;
8 | import com.r.mvp.cn.delegate.MvpDelegateCallback;
9 | import com.r.mvp.cn.root.IMvpPresenter;
10 | import com.r.mvp.cn.root.IMvpView;
11 | import com.trello.rxlifecycle2.components.support.RxFragmentActivity;
12 |
13 |
14 | /**
15 | * MvpFragmentActivity
16 | * 备注:
17 | * 1.XXActivity 继承 MvpActivity,当页面存在 Presenter 时,具体 Activity 需要调用 setPresenter(P... presenter)
18 | * 2.由于此框架集合了 RxLifecycle 因此本 Activity 继承自 RxActivity (开发者也可以直接继承 Activity)
19 | * 3.支持一个 Activity 存在多个 Presenter
20 | *
21 | * @param
22 | * @param
23 | */
24 | public abstract class MvpFragmentActivity> extends RxFragmentActivity implements IMvpView, MvpDelegateCallback {
25 |
26 | protected ActivityMvpDelegate mvpDelegate;
27 |
28 | /**
29 | * 获取 Presenter 数组
30 | */
31 | protected abstract P[] getPresenterArray();
32 |
33 | @Override
34 | public P[] getPresenter() {
35 | return getPresenterArray();
36 | }
37 |
38 | @Override
39 | public V[] getMvpView() {
40 | V[] view = null;
41 | P[] pArray = getPresenter();
42 | if (pArray != null) {
43 | view = (V[]) new IMvpView[pArray.length];
44 | for (int i = 0; i < pArray.length; i++) {
45 | view[i] = (V) this;
46 | }
47 | }
48 | return view;
49 | }
50 |
51 | @NonNull
52 | protected ActivityMvpDelegate getMvpDelegate() {
53 | if (mvpDelegate == null) {
54 | mvpDelegate = new ActivityMvpDelegateImpl(this, this);
55 | }
56 | return mvpDelegate;
57 | }
58 |
59 | @Override
60 | protected void onCreate(Bundle savedInstanceState) {
61 | super.onCreate(savedInstanceState);
62 | getMvpDelegate().onCreate(savedInstanceState);
63 | }
64 |
65 | @Override
66 | protected void onDestroy() {
67 | super.onDestroy();
68 | getMvpDelegate().onDestroy();
69 | }
70 |
71 | @Override
72 | protected void onSaveInstanceState(Bundle outState) {
73 | super.onSaveInstanceState(outState);
74 | getMvpDelegate().onSaveInstanceState(outState);
75 | }
76 |
77 | @Override
78 | protected void onPause() {
79 | super.onPause();
80 | getMvpDelegate().onPause();
81 | }
82 |
83 | @Override
84 | protected void onResume() {
85 | super.onResume();
86 | getMvpDelegate().onResume();
87 | }
88 |
89 | @Override
90 | protected void onStart() {
91 | super.onStart();
92 | getMvpDelegate().onStart();
93 | }
94 |
95 | @Override
96 | protected void onStop() {
97 | super.onStop();
98 | getMvpDelegate().onStop();
99 | }
100 |
101 | @Override
102 | protected void onRestart() {
103 | super.onRestart();
104 | getMvpDelegate().onRestart();
105 | }
106 |
107 | @Override
108 | public void onContentChanged() {
109 | super.onContentChanged();
110 | getMvpDelegate().onContentChanged();
111 | }
112 |
113 | @Override
114 | protected void onPostCreate(Bundle savedInstanceState) {
115 | super.onPostCreate(savedInstanceState);
116 | getMvpDelegate().onPostCreate(savedInstanceState);
117 | }
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/MvpPresenter.java:
--------------------------------------------------------------------------------
1 |
2 | package com.r.mvp.cn;
3 |
4 | import android.support.annotation.UiThread;
5 |
6 | import com.r.mvp.cn.proxy.MvpViewProxy;
7 | import com.r.mvp.cn.root.IMvpPresenter;
8 | import com.r.mvp.cn.root.IMvpView;
9 |
10 | /**
11 | * Presenter基础实现
12 | *
13 | * @param
14 | */
15 | public abstract class MvpPresenter implements IMvpPresenter {
16 |
17 | protected V mView;
18 |
19 | //View代理对象
20 | protected MvpViewProxy mMvpViewProxy;
21 |
22 | /**
23 | * 获取view
24 | *
25 | * @return
26 | */
27 | @UiThread
28 | public V getView() {
29 | return mView;
30 | }
31 |
32 | /**
33 | * 判断View是否已经添加
34 | *
35 | * @return
36 | */
37 | @UiThread
38 | public boolean isViewAttached() {
39 | return mView != null;
40 | }
41 |
42 | /**
43 | * 绑定View
44 | *
45 | * @param view
46 | */
47 | @UiThread
48 | @Override
49 | public void attachView(V view) {
50 | mMvpViewProxy = new MvpViewProxy();
51 | mView = (V) mMvpViewProxy.newProxyInstance(view);
52 | }
53 |
54 | /**
55 | * 移除View
56 | */
57 | @Override
58 | public void detachView() {
59 | if (mMvpViewProxy != null) {
60 | mMvpViewProxy.detachView();
61 | }
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/MvpView.java:
--------------------------------------------------------------------------------
1 |
2 | package com.r.mvp.cn;
3 |
4 | import android.app.Activity;
5 | import android.support.annotation.NonNull;
6 | import android.support.annotation.UiThread;
7 |
8 | import com.r.mvp.cn.root.IMvpView;
9 | import com.trello.rxlifecycle2.LifecycleProvider;
10 |
11 | /**
12 | * 基础View接口
13 | */
14 | public interface MvpView extends IMvpView {
15 |
16 | /**
17 | * RxLifecycle用于绑定组件生命周期
18 | *
19 | * @return
20 | */
21 | LifecycleProvider getRxLifecycle();
22 |
23 | /**
24 | * 获取Activity实例
25 | *
26 | * @return
27 | */
28 | Activity getActivity();
29 |
30 | /**
31 | * 展示吐司
32 | *
33 | * @param msg 吐司文本
34 | */
35 | @UiThread
36 | void showToast(@NonNull String msg);
37 |
38 | /**
39 | * 显示进度View
40 | */
41 | @UiThread
42 | void showProgressView();
43 |
44 | /**
45 | * 隐藏进度View
46 | */
47 | @UiThread
48 | void dismissProgressView();
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/delegate/ActivityMvpDelegate.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.delegate;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 |
6 | import com.r.mvp.cn.root.IMvpPresenter;
7 | import com.r.mvp.cn.root.IMvpView;
8 |
9 | /**
10 | * Activity生命周期
11 | *
12 | * @param
13 | * @param
14 | */
15 | public interface ActivityMvpDelegate> {
16 |
17 | /**
18 | * This method must be called from {@link Activity#onCreate(Bundle)}.
19 | * This method internally creates the presenter and attaches the view to it.
20 | */
21 | void onCreate(Bundle bundle);
22 |
23 | /**
24 | * This method must be called from {@link Activity#onDestroy()}}.
25 | * This method internally detaches the view from presenter
26 | */
27 | void onDestroy();
28 |
29 | /**
30 | * This method must be called from {@link Activity#onPause()}
31 | */
32 | void onPause();
33 |
34 | /**
35 | * This method must be called from {@link Activity#onResume()}
36 | */
37 | void onResume();
38 |
39 | /**
40 | * This method must be called from {@link Activity#onStart()}
41 | */
42 | void onStart();
43 |
44 | /**
45 | * This method must be called from {@link Activity#onStop()}
46 | */
47 | void onStop();
48 |
49 | /**
50 | * This method must be called from {@link Activity#onRestart()}
51 | */
52 | void onRestart();
53 |
54 | /**
55 | * This method must be called from {@link Activity#onContentChanged()}
56 | */
57 | void onContentChanged();
58 |
59 | /**
60 | * This method must be called from {@link Activity#onSaveInstanceState(Bundle)}
61 | */
62 | void onSaveInstanceState(Bundle outState);
63 |
64 | /**
65 | * This method must be called from {@link Activity#onPostCreate(Bundle)}
66 | */
67 | void onPostCreate(Bundle savedInstanceState);
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/delegate/ActivityMvpDelegateImpl.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.delegate;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 |
7 | import com.r.mvp.cn.root.IMvpPresenter;
8 | import com.r.mvp.cn.root.IMvpView;
9 |
10 | import java.lang.reflect.ParameterizedType;
11 |
12 | /**
13 | * Activity媒介
14 | * 备注:主要是连接 Activity 的生命周期与 Presenter 实现特定生命周期绑定与解除 V
15 | *
16 | * @author ZhongDaFeng
17 | */
18 | public class ActivityMvpDelegateImpl> implements ActivityMvpDelegate {
19 |
20 | /**
21 | * Activity
22 | */
23 | protected Activity activity;
24 |
25 | /**
26 | * V & P
27 | */
28 | private MvpDelegateCallback delegateCallback;
29 |
30 | public ActivityMvpDelegateImpl(Activity activity, MvpDelegateCallback delegateCallback) {
31 | if (activity == null) {
32 | throw new NullPointerException("Activity is null!");
33 | }
34 | if (delegateCallback == null) {
35 | throw new NullPointerException("MvpDelegateCallback is null!");
36 | }
37 | this.activity = activity;
38 | this.delegateCallback = delegateCallback;
39 | }
40 |
41 | /**
42 | * 是否保留V&P实例
43 | *
44 | * @return
45 | */
46 | private static boolean retainVPInstance(Activity activity) {
47 | return activity.isChangingConfigurations() || !activity.isFinishing();
48 | }
49 |
50 | @Override
51 | public void onCreate(Bundle bundle) {
52 |
53 | P[] pArray = delegateCallback.getPresenter();
54 | if (pArray != null) {
55 | V[] vArray = delegateCallback.getMvpView();
56 | P presenter;
57 | V view;
58 | for (int i = 0; i < pArray.length; i++) {
59 | presenter = pArray[i];
60 | view = vArray[i];
61 | if (presenter != null && view != null) {
62 | //关联view
63 | presenter.attachView(view);
64 | }
65 | }
66 | }
67 | }
68 |
69 |
70 | @Override
71 | public void onDestroy() {
72 | P[] pArray = delegateCallback.getPresenter();
73 | if (pArray != null) {
74 | P presenter;
75 | for (int i = 0; i < pArray.length; i++) {
76 | presenter = pArray[i];
77 | if (presenter != null) {
78 | //解除View
79 | presenter.detachView();
80 | if (!retainVPInstance(activity)) {
81 | //销毁 V & P 实例
82 | presenter.destroy();
83 | }
84 | }
85 | }
86 | }
87 | }
88 |
89 | @Override
90 | public void onPause() {
91 |
92 | }
93 |
94 | @Override
95 | public void onResume() {
96 |
97 | }
98 |
99 | @Override
100 | public void onStart() {
101 |
102 | }
103 |
104 | @Override
105 | public void onStop() {
106 |
107 | }
108 |
109 | @Override
110 | public void onRestart() {
111 |
112 | }
113 |
114 | @Override
115 | public void onContentChanged() {
116 |
117 | }
118 |
119 | @Override
120 | public void onSaveInstanceState(Bundle outState) {
121 |
122 | }
123 |
124 | @Override
125 | public void onPostCreate(Bundle savedInstanceState) {
126 |
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/delegate/FragmentMvpDelegate.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.delegate;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.view.View;
8 |
9 | import com.r.mvp.cn.root.IMvpPresenter;
10 | import com.r.mvp.cn.root.IMvpView;
11 |
12 | /**
13 | * Fragment生命周期
14 | *
15 | * @param
16 | * @param
17 | */
18 | public interface FragmentMvpDelegate> {
19 |
20 | /**
21 | * Must be called from {@link Fragment#onCreate(Bundle)}
22 | *
23 | * @param saved The bundle
24 | */
25 | void onCreate(Bundle saved);
26 |
27 | /**
28 | * Must be called from {@link Fragment#onDestroy()}
29 | */
30 | void onDestroy();
31 |
32 | /**
33 | * Must be called from {@link Fragment#onViewCreated(View, Bundle)}
34 | *
35 | * @param view The inflated view
36 | * @param savedInstanceState the bundle
37 | */
38 | void onViewCreated(View view, @Nullable Bundle savedInstanceState);
39 |
40 | /**
41 | * Must be called from {@link Fragment#onDestroyView()}
42 | */
43 | void onDestroyView();
44 |
45 | /**
46 | * Must be called from {@link Fragment#onPause()}
47 | */
48 | void onPause();
49 |
50 | /**
51 | * Must be called from {@link Fragment#onResume()}
52 | */
53 | void onResume();
54 |
55 | /**
56 | * Must be called from {@link Fragment#onStart()}
57 | */
58 | void onStart();
59 |
60 | /**
61 | * Must be called from {@link Fragment#onStop()}
62 | */
63 | void onStop();
64 |
65 | /**
66 | * Must be called from {@link Fragment#onActivityCreated(Bundle)}
67 | *
68 | * @param savedInstanceState The saved bundle
69 | */
70 | void onActivityCreated(Bundle savedInstanceState);
71 |
72 | /**
73 | * Must be called from {@link Fragment#onAttach(Activity)}
74 | *
75 | * @param activity The activity the fragment is attached to
76 | */
77 | void onAttach(Activity activity);
78 |
79 | /**
80 | * Must be called from {@link Fragment#onDetach()}
81 | */
82 | void onDetach();
83 |
84 | /**
85 | * Must be called from {@link Fragment#onSaveInstanceState(Bundle)}
86 | */
87 | void onSaveInstanceState(Bundle outState);
88 | }
89 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/delegate/FragmentMvpDelegateImpl.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.delegate;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.view.View;
8 |
9 | import com.r.mvp.cn.root.IMvpPresenter;
10 | import com.r.mvp.cn.root.IMvpView;
11 |
12 | /**
13 | * Fragment 媒介
14 | * 备注:主要是连接 Fragment 的生命周期与 Presenter 实现特定生命周期绑定与解除 V
15 | *
16 | * @author ZhongDaFeng
17 | */
18 | public class FragmentMvpDelegateImpl> implements FragmentMvpDelegate {
19 |
20 | /**
21 | * Fragment
22 | */
23 | protected Fragment fragment;
24 |
25 | /**
26 | * V & P
27 | */
28 | private MvpDelegateCallback delegateCallback;
29 |
30 | public FragmentMvpDelegateImpl(Fragment fragment, MvpDelegateCallback delegateCallback) {
31 | if (fragment == null) {
32 | throw new NullPointerException("Fragment is null!");
33 | }
34 | if (delegateCallback == null) {
35 | throw new NullPointerException("MvpDelegateCallback is null!");
36 | }
37 | this.fragment = fragment;
38 | this.delegateCallback = delegateCallback;
39 | }
40 |
41 | /**
42 | * 是否保留V&P实例
43 | *
44 | * @return
45 | */
46 | private static boolean retainVPInstance(Activity activity, Fragment fragment) {
47 | if (activity.isChangingConfigurations()) {
48 | return false;
49 | }
50 | if (activity.isFinishing()) {
51 | return false;
52 | }
53 | return !fragment.isRemoving();
54 | }
55 |
56 |
57 | /**
58 | * 获取Activity
59 | *
60 | * @return
61 | */
62 | private Activity getActivity() {
63 | Activity activity = fragment.getActivity();
64 | if (activity == null) {
65 | throw new NullPointerException("Activity returned by Fragment.getActivity() is null. Fragment is " + fragment);
66 | }
67 | return activity;
68 | }
69 |
70 | @Override
71 | public void onCreate(Bundle saved) {
72 | P[] pArray = delegateCallback.getPresenter();
73 | if (pArray != null) {
74 | V[] vArray = delegateCallback.getMvpView();
75 | P p;
76 | V v;
77 | for (int i = 0; i < pArray.length; i++) {
78 | p = pArray[i];
79 | v = vArray[i];
80 | if (p != null && v != null) {
81 | //关联view
82 | p.attachView(v);
83 | }
84 | }
85 | }
86 | }
87 |
88 | @Override
89 | public void onDestroy() {
90 | Activity activity = getActivity();
91 | P[] pArray = delegateCallback.getPresenter();
92 | if (pArray != null) {
93 | P presenter;
94 | for (int i = 0; i < pArray.length; i++) {
95 | presenter = pArray[i];
96 | if (presenter != null) {
97 | if (!retainVPInstance(activity, fragment)) {
98 | //销毁 V & P 实例
99 | presenter.destroy();
100 | }
101 | }
102 | }
103 | }
104 | }
105 |
106 | @Override
107 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
108 | }
109 |
110 | @Override
111 | public void onDestroyView() {
112 | P[] pArray = delegateCallback.getPresenter();
113 | if (pArray != null) {
114 | P presenter;
115 | for (int i = 0; i < pArray.length; i++) {
116 | presenter = pArray[i];
117 | if (presenter != null) {
118 | //解除View
119 | presenter.detachView();
120 | }
121 | }
122 | }
123 | }
124 |
125 | @Override
126 | public void onPause() {
127 |
128 | }
129 |
130 | @Override
131 | public void onResume() {
132 |
133 | }
134 |
135 | @Override
136 | public void onStart() {
137 |
138 | }
139 |
140 | @Override
141 | public void onStop() {
142 |
143 | }
144 |
145 | @Override
146 | public void onActivityCreated(Bundle savedInstanceState) {
147 |
148 | }
149 |
150 | @Override
151 | public void onAttach(Activity activity) {
152 |
153 | }
154 |
155 | @Override
156 | public void onDetach() {
157 |
158 | }
159 |
160 | @Override
161 | public void onSaveInstanceState(Bundle outState) {
162 |
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/delegate/MvpDelegateCallback.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.delegate;
2 |
3 | import com.r.mvp.cn.root.IMvpPresenter;
4 | import com.r.mvp.cn.root.IMvpView;
5 |
6 | /**
7 | * V/P 媒介
8 | *
9 | * @param
10 | * @param
11 | */
12 | public interface MvpDelegateCallback> {
13 |
14 | /**
15 | * Gets the presenter.
16 | */
17 | P[] getPresenter();
18 |
19 | /**
20 | * Gets the MvpView for the presenter
21 | *
22 | * @return The view associated with the presenter
23 | */
24 | V[] getMvpView();
25 |
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/model/ModelCallback.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.model;
2 |
3 | /**
4 | * 模块数据回调接口
5 | *
6 | * @author ZhongDaFeng
7 | */
8 | public interface ModelCallback {
9 |
10 | /**
11 | * 网络数据回调,泛指http
12 | *
13 | * @param
14 | */
15 | interface Http {
16 |
17 | public void onSuccess(T object);
18 |
19 | public void onError(int code, String desc);
20 |
21 | public void onCancel();
22 | }
23 |
24 | /**
25 | * 其他数据回调<本地数据,数据库等>
26 | *
27 | * @param
28 | */
29 | interface Data {
30 |
31 | public void onSuccess(T object);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/model/ModelFactory.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.model;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import com.r.mvp.cn.root.IMvpModel;
6 |
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | /**
11 | * Model工厂类
12 | *
13 | * @author ZhongDaFeng
14 | */
15 | public class ModelFactory {
16 |
17 | /**
18 | * 全局存储Model
19 | */
20 | private static Map modelMap = new HashMap<>();
21 |
22 | /**
23 | * 获取model
24 | * 查询Map中是否存在model实例,不存在时动态创建
25 | *
26 | * @param cls 类
27 | * @param model
28 | * @return
29 | */
30 | public static T getModel(@NonNull Class cls) {
31 | String className = cls.getName();//类名
32 | T model = (T) modelMap.get(className);
33 | if (model == null) {//不存在
34 | model = getModelReflex(className);
35 | modelMap.put(className, model);
36 | }
37 | return model;
38 | }
39 |
40 | /**
41 | * 反射获取Model
42 | *
43 | * @param className 包含完整路径的类名称 com.ruffian.cn.User
44 | * @param
45 | * @return
46 | */
47 | private static T getModelReflex(@NonNull String className) {
48 | T result = null;
49 | try {
50 | result = (T) Class.forName(className).newInstance();
51 | } catch (Exception e) {
52 | e.printStackTrace();
53 | }
54 | return result;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/proxy/MvpViewProxy.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.proxy;
2 |
3 | import com.r.mvp.cn.root.IMvpView;
4 |
5 | import java.lang.reflect.InvocationHandler;
6 | import java.lang.reflect.Method;
7 | import java.lang.reflect.Proxy;
8 |
9 | /**
10 | * MvpView 代理
11 | *
12 | * 目的:MvpView 对象需要在 activity/fragment 组件销毁时清空,目的是异步回调时不再处理 MvpView 的方法
13 | * 原理:通过代理,将 MvpView 的方法调用放在代理类中实现,通过判断代理中 MvpView 是否为空判断是否需要回调方法,同时避免 P 中每次调用 MvpView 都要判空的麻烦
14 | *
15 | * @author ZhongDaFeng
16 | */
17 | public class MvpViewProxy implements InvocationHandler {
18 |
19 | private V mView;
20 |
21 | //创建代理(接受委托)
22 | public Object newProxyInstance(V view) {
23 | this.mView = view;
24 | return Proxy.newProxyInstance(view.getClass().getClassLoader(), view.getClass().getInterfaces(), this);
25 | }
26 |
27 | @Override
28 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
29 | // V 为空直接返回 null 不再继续调用函数
30 | if (mView == null) {
31 | return null;
32 | }
33 | //调用目标方法
34 | Object temp = method.invoke(mView, args);
35 | return temp;
36 | }
37 |
38 | /**
39 | * 解绑View
40 | */
41 | public void detachView() {
42 | mView = null;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/root/IMvpModel.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.root;
2 |
3 | /**
4 | * MVP 根Model
5 | * MvpModel创建之后全局静态持有,因此不能持有短生命周期的对象,避免内存泄漏
6 | *
7 | * @author ZhongDaFeng
8 | */
9 | public interface IMvpModel {
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/root/IMvpPresenter.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.root;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.annotation.UiThread;
5 |
6 | /**
7 | * MVP 根Presenter
8 | *
9 | * @author ZhongDaFeng
10 | */
11 | public interface IMvpPresenter {
12 |
13 | /**
14 | * 将 View 添加到当前 Presenter
15 | */
16 | @UiThread
17 | void attachView(@NonNull V view);
18 |
19 | /**
20 | * 将 View 从 Presenter 移除
21 | */
22 | @UiThread
23 | void detachView();
24 |
25 | /**
26 | * 销毁 V 实例
27 | */
28 | @UiThread
29 | void destroy();
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Mvp/src/main/java/com/r/mvp/cn/root/IMvpView.java:
--------------------------------------------------------------------------------
1 | package com.r.mvp.cn.root;
2 |
3 | /**
4 | * MVP 根视图
5 | *
6 | * @author ZhongDaFeng
7 | */
8 | public interface IMvpView {
9 | }
10 |
--------------------------------------------------------------------------------
/Mvp/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/RHttp/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/RHttp/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion '28.0.2'
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | official {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | debug {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24 | }
25 | intranet {
26 | minifyEnabled false
27 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
28 | }
29 | extranet {
30 | minifyEnabled false
31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
32 | }
33 | }
34 |
35 | }
36 |
37 | dependencies {
38 | implementation fileTree(include: ['*.jar'], dir: 'libs')
39 | testImplementation 'junit:junit:4.12'
40 | /*RxLifecycle基础库*/
41 | api 'com.trello.rxlifecycle2:rxlifecycle:2.1.0'
42 | api 'com.trello.rxlifecycle2:rxlifecycle-components:2.1.0'
43 | /*网络请求框架*/
44 | api 'com.squareup.retrofit2:retrofit:2.3.0'
45 | api 'com.squareup.retrofit2:converter-gson:2.3.0'
46 | api 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
47 | api 'com.squareup.okhttp3:logging-interceptor:3.8.0'
48 | /*RxJava&RxAndroid*/
49 | api 'io.reactivex.rxjava2:rxjava:2.1.0'
50 | api 'io.reactivex.rxjava2:rxandroid:2.0.1'
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/RHttp/libs/lite-orm-1.9.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RuffianZhong/Rx-Mvp/6fe60642c3ed1aca8b6990ec5c25abc90e68683d/RHttp/libs/lite-orm-1.9.2.jar
--------------------------------------------------------------------------------
/RHttp/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/RHttp/src/androidTest/java/com/r/http/cn/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.r.http.cn.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/RHttp/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/api/Api.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.api;
2 |
3 | import com.google.gson.JsonElement;
4 |
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import io.reactivex.Observable;
9 | import okhttp3.MultipartBody;
10 | import okhttp3.RequestBody;
11 | import okhttp3.ResponseBody;
12 | import retrofit2.http.Body;
13 | import retrofit2.http.DELETE;
14 | import retrofit2.http.FieldMap;
15 | import retrofit2.http.FormUrlEncoded;
16 | import retrofit2.http.GET;
17 | import retrofit2.http.HTTP;
18 | import retrofit2.http.Header;
19 | import retrofit2.http.HeaderMap;
20 | import retrofit2.http.Multipart;
21 | import retrofit2.http.POST;
22 | import retrofit2.http.PUT;
23 | import retrofit2.http.Part;
24 | import retrofit2.http.PartMap;
25 | import retrofit2.http.QueryMap;
26 | import retrofit2.http.Streaming;
27 | import retrofit2.http.Url;
28 |
29 | /**
30 | * Api接口
31 | *
32 | * @author ZhongDaFeng
33 | */
34 | public interface Api {
35 |
36 | /**
37 | * GET 请求
38 | *
39 | * @param url api接口url
40 | * @param parameter 请求参数map
41 | * @param header 请求头map
42 | * @return
43 | */
44 | @GET
45 | Observable get(@Url String url, @QueryMap Map parameter, @HeaderMap Map header);
46 |
47 |
48 | /**
49 | * POST 请求
50 | *
51 | * @param url api接口url
52 | * @param parameter 请求参数map
53 | * @param header 请求头map
54 | * @return
55 | */
56 | @FormUrlEncoded
57 | @POST
58 | Observable post(@Url String url, @FieldMap Map parameter, @HeaderMap Map header);
59 |
60 |
61 | /**
62 | * @param requestBody 用于String/JSON格式数据
63 | */
64 | @POST
65 | Observable post(@Url String url, @Body RequestBody requestBody, @HeaderMap Map header);
66 |
67 |
68 | /**
69 | * DELETE 请求
70 | *
71 | * @param url api接口url
72 | * @param parameter 请求参数map
73 | * @param header 请求头map
74 | * @return
75 | */
76 | @DELETE
77 | Observable delete(@Url String url, @QueryMap Map parameter, @HeaderMap Map header);
78 |
79 |
80 | /**
81 | * PUT 请求
82 | *
83 | * @param url api接口url
84 | * @param parameter 请求参数map
85 | * @param header 请求头map
86 | * @return
87 | */
88 | @FormUrlEncoded
89 | @PUT
90 | Observable put(@Url String url, @FieldMap Map parameter, @HeaderMap Map header);
91 |
92 |
93 | /**
94 | * 多文件上传
95 | *
96 | * @param url api接口url
97 | * @param parameter 请求接口参数
98 | * @param header 请求头map
99 | * @param fileList 文件列表
100 | * @return
101 | * @Multipart 文件上传注解 multipart/form-data
102 | */
103 | @Multipart
104 | @POST
105 | Observable upload(@Url String url, @PartMap Map parameter, @HeaderMap Map header, @Part List fileList);
106 |
107 |
108 | /**
109 | * 断点续传下载
110 | *
111 | * @param range 断点下载范围 bytes= start - end
112 | * @param url 下载地址
113 | * @param header 请求头map
114 | * @return
115 | * @Streaming 防止内容写入内存, 大文件通过此注解避免OOM
116 | */
117 | @Streaming
118 | @GET
119 | Observable download(@Header("RANGE") String range, @Url String url, @HeaderMap Map header);
120 |
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/callback/BaseCallback.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.callback;
2 |
3 | import com.r.http.cn.RHttp;
4 | import com.r.http.cn.exception.ApiException;
5 | import com.r.http.cn.exception.ExceptionEngine;
6 | import com.r.http.cn.observer.HttpObserver;
7 | import com.r.http.cn.utils.ThreadUtils;
8 |
9 | import java.lang.reflect.ParameterizedType;
10 |
11 | import io.reactivex.annotations.NonNull;
12 |
13 | /**
14 | * Http请求基础回调接口
15 | * 备注:处理基本逻辑
16 | *
17 | * @author ZhongDaFeng
18 | */
19 | public abstract class BaseCallback extends HttpObserver {
20 |
21 | @Override
22 | public void onNext(@NonNull T value) {
23 | super.onNext(value);
24 | inSuccess(value);
25 | }
26 |
27 | @Override
28 | public void onError(Throwable e) {
29 | super.onError(e);
30 | if (e instanceof ApiException) {
31 | ApiException exception = (ApiException) e;
32 | inError(exception.getCode(), exception.getMsg());
33 | } else {
34 | inError(ExceptionEngine.UN_KNOWN_ERROR, "未知错误");
35 | }
36 | }
37 |
38 | @Override
39 | public void onCanceled() {
40 | onCanceledLogic();
41 | }
42 |
43 | /**
44 | * 请求成功
45 | *
46 | * @param t
47 | */
48 | public abstract void inSuccess(T t);
49 |
50 | /**
51 | * 请求出错
52 | *
53 | * @param code
54 | * @param desc
55 | */
56 | public abstract void inError(int code, String desc);
57 |
58 | /**
59 | * 请求取消
60 | */
61 | public abstract void inCancel();
62 |
63 | /**
64 | * Http被取消回调处理逻辑
65 | */
66 | private void onCanceledLogic() {
67 | if (!ThreadUtils.isMainThread()) {
68 | RHttp.Configure.get().getHandler().post(new Runnable() {
69 | @Override
70 | public void run() {
71 | inCancel();
72 | }
73 | });
74 | } else {
75 | inCancel();
76 | }
77 | }
78 |
79 | @Deprecated
80 | private void getTypeClass() {
81 | /**
82 | * 获取当前类泛型(暂时保留)
83 | */
84 | ParameterizedType ptClass = (ParameterizedType) getClass().getGenericSuperclass();
85 | Class mClass;
86 | if (ptClass != null) {
87 | mClass = (Class) ptClass.getActualTypeArguments()[0];
88 | //LogUtils.e("当前类泛型:" + mClass);
89 | }
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/callback/HttpCallback.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.callback;
2 |
3 | import com.r.http.cn.exception.ExceptionEngine;
4 | import com.r.http.cn.helper.ParseHelper;
5 |
6 | /**
7 | * Http请求回调
8 | *
9 | * @author ZhongDaFeng
10 | */
11 | public abstract class HttpCallback extends BaseCallback implements ParseHelper {
12 |
13 | /**
14 | * 是否回调成功函数
15 | */
16 | private boolean callSuccess = true;
17 |
18 | @Override
19 | public T parse(String data) {
20 | T t = null;
21 | try {
22 | t = onConvert(data);
23 | callSuccess = true;
24 | } catch (Exception e) {
25 | callSuccess = false;
26 | e.printStackTrace();
27 | onError(ExceptionEngine.ANALYTIC_CLIENT_DATA_ERROR, "解析数据出错");
28 | }
29 | return t;
30 | }
31 |
32 |
33 | @Override
34 | public void inSuccess(T value) {
35 | T result = parse((String) value);
36 | if (callSuccess && isBusinessOk()) {
37 | onSuccess(result);
38 | }
39 | }
40 |
41 | @Override
42 | public void inError(int code, String desc) {
43 | onError(code, desc);
44 | }
45 |
46 | @Override
47 | public void inCancel() {
48 | onCancel();
49 | }
50 |
51 | /**
52 | * 数据转换/解析数据
53 | *
54 | * @param data
55 | * @return
56 | */
57 | public abstract T onConvert(String data) throws Exception;
58 |
59 | /**
60 | * 成功回调
61 | *
62 | * @param value
63 | */
64 | public abstract void onSuccess(T value);
65 |
66 | /**
67 | * 失败回调
68 | *
69 | * @param code
70 | * @param desc
71 | */
72 | public abstract void onError(int code, String desc);
73 |
74 | /**
75 | * 取消回调
76 | */
77 | public abstract void onCancel();
78 |
79 | /**
80 | * 业务逻辑是否成功
81 | *
82 | * @return
83 | */
84 | public abstract boolean isBusinessOk();
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/callback/UploadCallback.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.callback;
2 |
3 | import com.r.http.cn.load.upload.UploadProgressCallback;
4 |
5 | import java.io.File;
6 |
7 | /**
8 | * 上传回调接口
9 | *
10 | * @author ZhongDaFeng
11 | */
12 | public abstract class UploadCallback extends HttpCallback implements UploadProgressCallback {
13 |
14 |
15 | @Override
16 | public void progress(File file, long currentSize, long totalSize, float progress, int currentIndex, int totalFile) {
17 | onProgress(file, currentSize, totalSize, progress, currentIndex, totalFile);
18 | }
19 |
20 | /**
21 | * 上传回调
22 | *
23 | * @param file
24 | * @param currentSize
25 | * @param totalSize
26 | * @param progress
27 | * @param currentIndex
28 | * @param totalFile
29 | */
30 | public abstract void onProgress(File file, long currentSize, long totalSize, float progress, int currentIndex, int totalFile);
31 |
32 | /**
33 | * 数据转换/解析数据
34 | *
35 | * @param data
36 | * @return
37 | */
38 | public abstract T onConvert(String data);
39 |
40 | /**
41 | * 成功回调
42 | *
43 | * @param value
44 | */
45 | public abstract void onSuccess(T value);
46 |
47 | /**
48 | * 失败回调
49 | *
50 | * @param code
51 | * @param desc
52 | */
53 | public abstract void onError(int code, String desc);
54 |
55 | /**
56 | * 取消回调
57 | */
58 | public abstract void onCancel();
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/cancel/RequestCancel.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.cancel;
2 |
3 | /**
4 | * 请求取消接口
5 | *
6 | * @author ZhongDaFeng
7 | */
8 | public interface RequestCancel {
9 |
10 | /**
11 | * 取消请求
12 | */
13 | void cancel();
14 |
15 | /**
16 | * 请求被取消
17 | */
18 | void onCanceled();
19 | }
20 |
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/cancel/RequestManager.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.cancel;
2 |
3 | import io.reactivex.disposables.Disposable;
4 |
5 | /**
6 | * Http请求管理接口
7 | *
8 | * @author ZhongDaFeng
9 | */
10 | public interface RequestManager {
11 | /**
12 | * 添加
13 | *
14 | * @param tag
15 | * @param disposable
16 | */
17 | void add(T tag, Disposable disposable);
18 |
19 | /**
20 | * 移除
21 | *
22 | * @param tag
23 | */
24 | void remove(T tag);
25 |
26 | /**
27 | * 取消
28 | *
29 | * @param tag
30 | */
31 | void cancel(T tag);
32 |
33 | /**
34 | * 取消全部
35 | */
36 | void cancelAll();
37 |
38 | }
--------------------------------------------------------------------------------
/RHttp/src/main/java/com/r/http/cn/cancel/RequestManagerImpl.java:
--------------------------------------------------------------------------------
1 | package com.r.http.cn.cancel;
2 |
3 | import android.annotation.TargetApi;
4 | import android.os.Build;
5 | import android.support.v4.util.ArrayMap;
6 |
7 | import java.util.Set;
8 |
9 | import io.reactivex.disposables.Disposable;
10 |
11 | /**
12 | * Http请求管理实现类
13 | *
14 | * @author ZhongDaFeng
15 | */
16 | public class RequestManagerImpl implements RequestManager