|
|
@@ -36,7 +36,7 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
- // 查找回调方法
|
|
|
+ // 查找回调方法(保证只添加一次)
|
|
|
if (mDataMethodList.size() == 0) {
|
|
|
Method[] methods = null;
|
|
|
try {
|
|
|
@@ -57,20 +57,29 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
DataMethod dataMethod = method.getAnnotation(DataMethod.class);
|
|
|
if (dataMethod != null) {
|
|
|
int length = method.getParameterTypes().length;
|
|
|
- if (length != 3) {
|
|
|
- throw new AssertionError("DataMethod参数数量必须为3个");
|
|
|
+ if (length != 4) {
|
|
|
+ throw new AssertionError("DataMethod参数数量必须为4个");
|
|
|
}
|
|
|
|
|
|
+ // 响应结果code
|
|
|
String firstType = method.getParameterTypes()[0].getSimpleName();
|
|
|
- String firstCorrectType = int.class.getSimpleName();
|
|
|
- if (!TextUtils.equals(firstCorrectType, firstType)) {
|
|
|
- throw new AssertionError("DataMethod第1个参数必须为" + firstCorrectType);
|
|
|
+ String resultCodeType = int.class.getSimpleName();
|
|
|
+ if (!TextUtils.equals(resultCodeType, firstType)) {
|
|
|
+ throw new AssertionError("DataMethod第1个参数必须为" + resultCodeType);
|
|
|
}
|
|
|
|
|
|
+ // 响应消息
|
|
|
String secondType = method.getParameterTypes()[1].getSimpleName();
|
|
|
- String secondCorrectType = String.class.getSimpleName();
|
|
|
- if (!TextUtils.equals(secondCorrectType, secondType)) {
|
|
|
- throw new AssertionError("DataMethod第2个参数必须为" + secondCorrectType);
|
|
|
+ String messageType = String.class.getSimpleName();
|
|
|
+ if (!TextUtils.equals(messageType, secondType)) {
|
|
|
+ throw new AssertionError("DataMethod第2个参数必须为" + messageType);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据来源
|
|
|
+ String fourthType = method.getParameterTypes()[3].getSimpleName();
|
|
|
+ String sourceCodeType = int.class.getSimpleName();
|
|
|
+ if (!TextUtils.equals(fourthType, sourceCodeType)) {
|
|
|
+ throw new AssertionError("DataMethod第4个参数必须为" + sourceCodeType);
|
|
|
}
|
|
|
|
|
|
method.setAccessible(true);
|
|
|
@@ -84,34 +93,36 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
mInjectPresenters = new ArrayList<>();
|
|
|
Field[] fields = this.getClass().getDeclaredFields();
|
|
|
for (Field field : fields) {
|
|
|
- //获取变量上面的注解类型
|
|
|
+ // 获取变量上面的注解类型
|
|
|
InjectViewModel injectViewModel = field.getAnnotation(InjectViewModel.class);
|
|
|
- if (injectViewModel != null) {
|
|
|
- try {
|
|
|
- field.setAccessible(true);
|
|
|
-
|
|
|
- Class<? extends BaseViewModel> type = (Class<? extends BaseViewModel>) field.getType();
|
|
|
- //BaseViewModel presenter = type.newInstance();
|
|
|
+ if (injectViewModel == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- //创建一个观察者去更新UI
|
|
|
- final Observer<DataResult> observer = new Observer<DataResult>() {
|
|
|
- @Override
|
|
|
- public void onChanged(final DataResult result) {
|
|
|
- onDataResult(result);
|
|
|
- }
|
|
|
- };
|
|
|
- // 父类引用指向子类对象
|
|
|
- BaseViewModel presenter = new ViewModelProvider(this).get(type);
|
|
|
- presenter.getLiveData().observe(this, observer);
|
|
|
-
|
|
|
- field.set(this, presenter);
|
|
|
- mInjectPresenters.add(presenter);
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (ClassCastException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException("SubClass must extends Class:BaseViewModel");
|
|
|
- }
|
|
|
+ try {
|
|
|
+ field.setAccessible(true);
|
|
|
+
|
|
|
+ // 父类引用指向子类对象
|
|
|
+ // BaseViewModel presenter = type.newInstance();
|
|
|
+ Class<? extends BaseViewModel> type = (Class<? extends BaseViewModel>) field.getType();
|
|
|
+ BaseViewModel presenter = new ViewModelProvider(this).get(type);
|
|
|
+
|
|
|
+ // 创建一个观察者去更新UI
|
|
|
+ final Observer<DataResult> observer = new Observer<DataResult>() {
|
|
|
+ @Override
|
|
|
+ public void onChanged(final DataResult result) {
|
|
|
+ onDataResult(result);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ presenter.getLiveData().observe(this, observer);
|
|
|
+
|
|
|
+ field.set(this, presenter);
|
|
|
+ mInjectPresenters.add(presenter);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClassCastException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException("SubClass must extends Class:BaseViewModel");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,12 +144,16 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
mDataMethodList.clear();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 回调
|
|
|
+ * @param result
|
|
|
+ */
|
|
|
private void onDataResult(DataResult result) {
|
|
|
dismissLoading();
|
|
|
Class objParamClass = result.getBean().getClass();
|
|
|
for (Method method : mDataMethodList) {
|
|
|
Class methodParamClass = method.getParameterTypes()[2];
|
|
|
+ // 判断方法的第二个参数类型是否是回调对象的子类
|
|
|
if (methodParamClass.isAssignableFrom(objParamClass)) {
|
|
|
try {
|
|
|
method.invoke(this, result.getCode(), result.getMessage(), result.getBean());
|