|
@@ -21,7 +21,12 @@ import java.lang.reflect.Method;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+// 第三个参数的类型
|
|
|
|
|
+//String objTypeName = method.getGenericParameterTypes()[2].toString();
|
|
|
|
|
+//if (objectParamsList.contains(objTypeName)) {
|
|
|
|
|
+// throw new AssertionError("第3个参数为" + objTypeName + "的方法有多个,应该合并为一个方法");
|
|
|
|
|
+//}
|
|
|
|
|
+//objectParamsList.add(objTypeName);
|
|
|
public abstract class BaseMVPFragment extends BaseFragment implements IView {
|
|
public abstract class BaseMVPFragment extends BaseFragment implements IView {
|
|
|
/**
|
|
/**
|
|
|
* 是否被创建了
|
|
* 是否被创建了
|
|
@@ -33,72 +38,22 @@ public abstract class BaseMVPFragment extends BaseFragment implements IView {
|
|
|
protected boolean isUIVisible;
|
|
protected boolean isUIVisible;
|
|
|
|
|
|
|
|
private List<Method> mDataMethodList = new ArrayList<>();
|
|
private List<Method> mDataMethodList = new ArrayList<>();
|
|
|
- //private List<String> objectParamsList = new ArrayList<>();
|
|
|
|
|
private List<BaseViewModel> mInjectPresenters;
|
|
private List<BaseViewModel> mInjectPresenters;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle 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();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ initVariables();
|
|
|
|
|
+ initCallbacks();
|
|
|
|
|
|
|
|
- // 参数校验
|
|
|
|
|
- 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 objTypeName = method.getGenericParameterTypes()[2].toString();
|
|
|
|
|
- if (objectParamsList.contains(objTypeName)) {
|
|
|
|
|
- throw new AssertionError("第3个参数为" + objTypeName + "的方法有多个,应该合并为一个方法");
|
|
|
|
|
- }
|
|
|
|
|
- objectParamsList.add(objTypeName);*/
|
|
|
|
|
-
|
|
|
|
|
- // 数据来源
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ isViewCreated = true;
|
|
|
|
|
+ onViewInit();
|
|
|
|
|
+ loadData();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化添加注解的变量
|
|
|
|
|
+ */
|
|
|
|
|
+ private void initVariables() {
|
|
|
// 这里可以获取到子类的成员变量
|
|
// 这里可以获取到子类的成员变量
|
|
|
mInjectPresenters = new ArrayList<>();
|
|
mInjectPresenters = new ArrayList<>();
|
|
|
Field[] fields = this.getClass().getDeclaredFields();
|
|
Field[] fields = this.getClass().getDeclaredFields();
|
|
@@ -113,7 +68,7 @@ public abstract class BaseMVPFragment extends BaseFragment implements IView {
|
|
|
field.setAccessible(true);
|
|
field.setAccessible(true);
|
|
|
|
|
|
|
|
// 父类引用指向子类对象
|
|
// 父类引用指向子类对象
|
|
|
- // BaseViewModel presenter = type.newInstance();
|
|
|
|
|
|
|
+ //BaseViewModel presenter = type.newInstance();
|
|
|
Class<? extends BaseViewModel> type = (Class<? extends BaseViewModel>) field.getType();
|
|
Class<? extends BaseViewModel> type = (Class<? extends BaseViewModel>) field.getType();
|
|
|
BaseViewModel presenter = new ViewModelProvider(this).get(type);
|
|
BaseViewModel presenter = new ViewModelProvider(this).get(type);
|
|
|
|
|
|
|
@@ -122,7 +77,7 @@ public abstract class BaseMVPFragment extends BaseFragment implements IView {
|
|
|
@Override
|
|
@Override
|
|
|
public void onChanged(final DataResult result) {
|
|
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);
|
|
onDataResult(result);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -134,13 +89,74 @@ public abstract class BaseMVPFragment extends BaseFragment implements IView {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} catch (ClassCastException e) {
|
|
} catch (ClassCastException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
- throw new RuntimeException("SubClass must extends Class:BaseViewModel");
|
|
|
|
|
|
|
+ throw new RuntimeException(InjectViewModel.class.getName() + "注解修饰的类必须继承自:" + BaseViewModel.class.getName());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- isViewCreated = true;
|
|
|
|
|
- onViewInit();
|
|
|
|
|
- loadData();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化获取需要回调的注解方法
|
|
|
|
|
+ */
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|