|
|
@@ -36,60 +36,15 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
- // 查找回调方法(保证只添加一次)
|
|
|
- if (mDataMethodList.size() == 0) {
|
|
|
- Method[] methods = null;
|
|
|
- try {
|
|
|
- // This is faster than getMethods, especially when subscribers are fat classes like Activities
|
|
|
- methods = getClass().getDeclaredMethods();
|
|
|
- } catch (Throwable th) {
|
|
|
- // Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
|
|
|
- try {
|
|
|
- methods = getClass().getDeclaredMethods();
|
|
|
- } catch (LinkageError error) { // super class of NoClassDefFoundError to be a bit more broad...
|
|
|
- error.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 参数校验
|
|
|
- if (methods != null && methods.length > 0) {
|
|
|
- for (Method method : methods) {
|
|
|
- DataMethod dataMethod = method.getAnnotation(DataMethod.class);
|
|
|
- if (dataMethod != null) {
|
|
|
-
|
|
|
- int length = method.getParameterTypes().length;
|
|
|
- if (length != 4) {
|
|
|
- throw new AssertionError("DataMethod参数数量必须为4个");
|
|
|
- }
|
|
|
-
|
|
|
- // 响应结果code
|
|
|
- String firstType = method.getParameterTypes()[0].getSimpleName();
|
|
|
- String resultCodeType = int.class.getSimpleName();
|
|
|
- if (!TextUtils.equals(resultCodeType, firstType)) {
|
|
|
- throw new AssertionError("DataMethod第1个参数必须为" + resultCodeType);
|
|
|
- }
|
|
|
-
|
|
|
- // 响应消息
|
|
|
- String secondType = method.getParameterTypes()[1].getSimpleName();
|
|
|
- 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);
|
|
|
- mDataMethodList.add(method);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ initVariables();
|
|
|
+ initCallbacks();
|
|
|
+ initViews(savedInstanceState);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 初始化添加注解的变量
|
|
|
+ */
|
|
|
+ private void initVariables() {
|
|
|
// 这里可以获取到子类的成员变量
|
|
|
mInjectPresenters = new ArrayList<>();
|
|
|
Field[] fields = this.getClass().getDeclaredFields();
|
|
|
@@ -104,7 +59,7 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
field.setAccessible(true);
|
|
|
|
|
|
// 父类引用指向子类对象
|
|
|
- // BaseViewModel presenter = type.newInstance();
|
|
|
+ //BaseViewModel presenter = type.newInstance();
|
|
|
Class<? extends BaseViewModel> type = (Class<? extends BaseViewModel>) field.getType();
|
|
|
BaseViewModel presenter = new ViewModelProvider(this).get(type);
|
|
|
|
|
|
@@ -113,7 +68,7 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
@Override
|
|
|
public void onChanged(final DataResult result) {
|
|
|
// 回调是在主线程
|
|
|
- //Log.i("Thread", String.valueOf(Thread.currentThread() == Looper.getMainLooper().getThread()));
|
|
|
+ // Log.i("Thread", String.valueOf(Thread.currentThread() == Looper.getMainLooper().getThread()));
|
|
|
onDataResult(result);
|
|
|
}
|
|
|
};
|
|
|
@@ -125,11 +80,74 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
e.printStackTrace();
|
|
|
} catch (ClassCastException e) {
|
|
|
e.printStackTrace();
|
|
|
- throw new RuntimeException("SubClass must extends Class:BaseViewModel");
|
|
|
+ throw new RuntimeException(InjectViewModel.class.getName() + "注解修饰的类必须继承自:" + BaseViewModel.class.getName());
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- initViews(savedInstanceState);
|
|
|
+ /**
|
|
|
+ * 初始化获取需要回调的注解方法
|
|
|
+ */
|
|
|
+ private void initCallbacks() {
|
|
|
+ // 查找回调方法(保证只添加一次)
|
|
|
+ if (mDataMethodList.size() != 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Method[] methods = null;
|
|
|
+ try {
|
|
|
+ // This is faster than getMethods, especially when subscribers are fat classes like Activities
|
|
|
+ methods = getClass().getDeclaredMethods();
|
|
|
+ } catch (Throwable th) {
|
|
|
+ // Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
|
|
|
+ try {
|
|
|
+ methods = getClass().getMethods();
|
|
|
+ } catch (LinkageError error) {
|
|
|
+ error.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 参数校验
|
|
|
+ if (methods == null || methods.length == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String methodAnnotationName = DataMethod.class.getName();
|
|
|
+ for (Method method : methods) {
|
|
|
+ DataMethod dataMethod = method.getAnnotation(DataMethod.class);
|
|
|
+ if (dataMethod == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ int length = method.getParameterTypes().length;
|
|
|
+ if (length != 4) {
|
|
|
+ throw new AssertionError(methodAnnotationName + "修饰的方法参数数量必须为4个");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 响应结果code
|
|
|
+ String firstType = method.getParameterTypes()[0].getSimpleName();
|
|
|
+ String resultCodeType = int.class.getSimpleName();
|
|
|
+ if (!TextUtils.equals(resultCodeType, firstType)) {
|
|
|
+ throw new AssertionError(methodAnnotationName + "修饰的方法第1个参数必须为" + resultCodeType);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 响应消息
|
|
|
+ String secondType = method.getParameterTypes()[1].getSimpleName();
|
|
|
+ String messageType = String.class.getSimpleName();
|
|
|
+ if (!TextUtils.equals(messageType, secondType)) {
|
|
|
+ throw new AssertionError(methodAnnotationName + "修饰的方法第2个参数必须为" + messageType);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据来源
|
|
|
+ String fourthType = method.getParameterTypes()[3].getSimpleName();
|
|
|
+ String sourceCodeType = int.class.getSimpleName();
|
|
|
+ if (!TextUtils.equals(fourthType, sourceCodeType)) {
|
|
|
+ throw new AssertionError(methodAnnotationName + "修饰的方法第4个参数必须为" + sourceCodeType);
|
|
|
+ }
|
|
|
+
|
|
|
+ method.setAccessible(true);
|
|
|
+ mDataMethodList.add(method);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -157,12 +175,14 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
|
|
|
for (Method method : mDataMethodList) {
|
|
|
Class methodParamClass = method.getParameterTypes()[2];
|
|
|
// 判断方法的第二个参数类型是否是回调对象的父类
|
|
|
- if (methodParamClass.isAssignableFrom(objParamClass)) {
|
|
|
- try {
|
|
|
- method.invoke(this, result.getResponseCode(), result.getMessage(), result.getBean(), result.getSourceCode());
|
|
|
- } catch (Exception e) {
|
|
|
- //e.printStackTrace();
|
|
|
- }
|
|
|
+ if (!methodParamClass.isAssignableFrom(objParamClass)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ method.invoke(this, result.getResponseCode(), result.getMessage(), result.getBean(), result.getSourceCode());
|
|
|
+ } catch (Exception e) {
|
|
|
+ //e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|