Skip to content

Commit

Permalink
Merge pull request #7 from zhuozp/v1.0.0
Browse files Browse the repository at this point in the history
feat: view的注入使用apt动态生成类文件的形式
  • Loading branch information
zhuozp authored Mar 14, 2020
2 parents 90d5498 + 1d82b3f commit f8e2ad9
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.gibbon.etc.annotation.Background;
import com.gibbon.etc.annotation.UiThread;
import com.gibbon.etc.apt.annotation.ViewById;
import com.gibbon.etc.apt.annotation.DownloadMoreListener;
Expand Down Expand Up @@ -80,20 +81,15 @@ void recyclerView() {
}

@RefreshListner()
@Background
void refreshView() {
// TODO, in other thread, such as downloading res
new Thread(new Runnable() {
@Override
public void run() {
//
try {
Thread.sleep(3000);
stopRefresh();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopRefresh();
}

@UiThread
Expand Down
4 changes: 4 additions & 0 deletions apt/src/main/java/com/gibbon/etc/apt/AptProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gibbon.etc.apt.annotation.DownloadMoreListener;
import com.gibbon.etc.apt.annotation.RefreshListner;
import com.gibbon.etc.apt.annotation.ViewById;
import com.gibbon.etc.apt.factory.ViewInjectStateFactory;
import com.gibbon.etc.apt.uitls.Utils;
import com.google.auto.service.AutoService;

Expand Down Expand Up @@ -80,6 +81,9 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
return false;
}

ViewInjectStateFactory.generateViewInject(filer);
ViewInjectStateFactory.generateViewInjector(filer);

//生成类
for (String key : proxyInfoHashMap.keySet()) {
ProxyInfo proxyInfo = proxyInfoHashMap.get(key);
Expand Down
8 changes: 8 additions & 0 deletions apt/src/main/java/com/gibbon/etc/apt/ProxyInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public ProxyInfo(Elements elements, TypeElement typeElement) {
this.proxyClassName = className + "$" + PROXY;
}

/**
* 获取包名
*
* */
public String getPackageName() {
return packageName;
}

/**
* 获取全名
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.gibbon.etc.apt.factory;

import java.io.IOException;
import java.io.Writer;

import javax.annotation.processing.Filer;
import javax.tools.JavaFileObject;

/**
* @author zhipeng.zhuo
* @date 2020-03-14
*/
public class ViewInjectStateFactory {

public static final String VIEW_INJECT_NAME = "com.gibbon.etc.inject.ViewInject";
public static final String VIEW_INJECTOR_NAME = "com.gibbon.etc.inject.ViewInjector";

public static void generateViewInject(Filer filer) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("package com.gibbon.etc.inject;\n" +
"\n" +
"public interface ViewInject<A> {\n" +
" void inject(A a, Object source);\n" +
"}");

try {
JavaFileObject jfo = filer.createSourceFile(VIEW_INJECT_NAME, null);
Writer writer = jfo.openWriter();
writer.write(stringBuilder.toString());
writer.flush();
writer.close();
} catch (IOException e) {

}
}

public static void generateViewInjector(Filer filer) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("package com.gibbon.etc.inject;\n" +
"\n" +
"import android.app.Activity;\n" +
"import android.view.View;\n" +
"\n" +

"public class ViewInjector {\n" +
"\n" +
" private static final String SUFFIX = \"$ViewInject\";\n" +
"\n" +
" public static void inject(Activity activity) {\n" +
" ViewInject viewInject = getProxyInject(activity);\n" +
" if (viewInject != null) {\n" +
" viewInject.inject(activity, activity);\n" +
" }\n" +
" }\n" +
"\n" +
" public static void inject(Object object, View view) {\n" +
" ViewInject viewInject = getProxyInject(object);\n" +
" if (viewInject != null) {\n" +
" viewInject.inject(object, view);\n" +
" }\n" +
" }\n" +
"\n" +
" private static ViewInject getProxyInject(Object object) {\n" +
" try {\n" +
" Class cls = object.getClass();\n" +
" Class injectorCls = Class.forName(cls.getName() + SUFFIX);\n" +
" return (ViewInject) injectorCls.newInstance();\n" +
" } catch (Exception e) {\n" +
"\n" +
" }\n" +
"\n" +
" return null;\n" +
" }\n" +
"}");

try {
JavaFileObject jfo = filer.createSourceFile(VIEW_INJECTOR_NAME , null);
Writer writer = jfo.openWriter();
writer.write(stringBuilder.toString());
writer.flush();
writer.close();
} catch (IOException e) {

}
}
}
9 changes: 0 additions & 9 deletions etc/src/main/java/com/gibbon/etc/inject/ViewInject.java

This file was deleted.

39 changes: 0 additions & 39 deletions etc/src/main/java/com/gibbon/etc/inject/ViewInjector.java

This file was deleted.

6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
#org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
Expand All @@ -18,5 +18,5 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true

#org.gradle.daemon=true
#org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

0 comments on commit f8e2ad9

Please sign in to comment.