Skip to content

Commit

Permalink
Merge pull request #79 from AlanCheen/feature/2.0.0
Browse files Browse the repository at this point in the history
Feature/2.0.0
  • Loading branch information
AlanCheen authored Sep 18, 2020
2 parents 3ee287f + 24779e6 commit e9ba528
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 154 deletions.
48 changes: 35 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@

### 集成 Flap

添加 `Flap` 的各个模块的最新版本到你的依赖:
1)添加 `Flap` 的各个模块的最新版本到你的依赖:

```groovy
dependencies {
implementation 'me.yifeiyuan.flap:flap:$lastest_version'
implementation 'me.yifeiyuan.flap:flap-annotations:$lastest_version'
annotationProcessor 'me.yifeiyuan.flap:flap-compiler:$lastest_version'
dependencies {
//recyclerview
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation "me.yifeiyuan.flap:flap:$lastest_version"
implementation "me.yifeiyuan.flap:flap-annotations:$lastest_version"
annotationProcessor "me.yifeiyuan.flap:flap-compiler:$lastest_version"
}
```

Expand All @@ -44,14 +45,34 @@ dependencies {
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'me.yifeiyuan.flap:flap:$lastest_version'
implementation 'me.yifeiyuan.flap:flap-annotations:$lastest_version'
kapt 'me.yifeiyuan.flap:flap-compiler:$lastest_version'
implementation "me.yifeiyuan.flap:flap:$lastest_version"
implementation "me.yifeiyuan.flap:flap-annotations:$lastest_version"
kapt "me.yifeiyuan.flap:flap-compiler:$lastest_version"
}
```

2) 添加 flap-plugin

在你的项目 build.gradle 添加插件的 classpath :
```
buildscript {
ext.kotlin_version = "1.4.0"
repositories {
google()
jcenter()
}
dependencies {
//添加插件
classpath "me.yifeiyuan.flap:plugin:$lastest_version"
}
}
```

在 app/build.gradle 中应用插件:
```
apply plugin: 'me.yifeiyuan.flap.plugin'
```

### Flap 基本使用教程

Expand Down Expand Up @@ -96,6 +117,7 @@ public class SimpleTextComponent extends Component<SimpleTextModel> {
}
```

注意:目前只能在 App 模块下使用,而在 Library 类型的 Module 中不能使用,因为使用了 R 文件,所以需要像 ButterKnife 那样针对 Module 做处理。


#### Step 3 : 创建你的 `FlapAdapter` 并设置 data
Expand Down Expand Up @@ -239,12 +261,12 @@ protected final <V extends View> V findViewById(@IdRes int viewId)

## TODO

- [ ] P0 , 针对 Library 类型的 Module 处理,让 Flap 也能工作;
- [ ] 【P2】Kotlin 改造;
- [ ] 【P1】迁移 AndroidX;
- [ ] 【P1】做一个 FlapRecyclerView 封装 FlapAdapter,进一步降低使用成本;
- [x] 支持 gradle plugin 实现组件的自动注册;
- [x] AndroidX 迁移;
- [x] Component 注解支持使用 DataBinding;
- [x] APT 实现组件自动注册;
- [x] 使用 APT 自动生成 Component 相关样板代码;
- [x] 支持组件全局缓存;
- [x] 支持组件监听生命周期事件,Lifecycle 接入;
Expand Down
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ dependencies {

implementation 'androidx.recyclerview:recyclerview:1.1.0'

// implementation project(':flap')
// implementation project(':flap-annotations')
// annotationProcessor project(':flap-compiler')
implementation project(':flap')
implementation project(':flap-annotations')
kapt project(':flap-compiler')

implementation 'me.yifeiyuan.flap:flap-annotations:1.6.1'
implementation 'me.yifeiyuan.flap:flap:1.6.1'
kapt 'me.yifeiyuan.flap:flap-compiler:1.6.1'
// implementation 'me.yifeiyuan.flap:flap-annotations:1.6.1'
// implementation 'me.yifeiyuan.flap:flap:1.6.1'
// kapt 'me.yifeiyuan.flap:flap-compiler:1.6.1'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Created by 程序亦非猿 on 2018/12/4.
*/
@Proxy(layoutId = R.layout.flap_item_simple_text, autoRegister = false)
@Proxy(layoutId = R.layout.flap_item_simple_text)
public class SimpleTextComponent extends Component<SimpleTextModel> {

private static final String TAG = "SimpleTextItem";
Expand Down
2 changes: 1 addition & 1 deletion flap-annotations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ publish {
userOrg = 'alancheen'
groupId = 'me.yifeiyuan.flap'
artifactId = 'flap-annotations'
publishVersion = '1.6.1'
publishVersion = '2.0.0'
desc = 'flap annotations'
website = 'https://github.com/AlanCheen/Flap'
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.lang.annotation.Target;

/**
* 标记一个类为组件,并配置组件的一些基本信息
* 标记一个类为组件,并配置组件的一些基本信息。
* <p>
* 被 @Proxy 标记的组件,会生成一个 Proxy 类,并被自动注册到 Flap 中。
*
* @author 程序亦非猿
* @since 1.1.0
Expand All @@ -16,27 +18,17 @@
public @interface Proxy {

/**
* 组件的布局 ID
* @return the layout res id
* 组件的布局 ID,也会被当做组件的 itemViewType
*
* @return the layout res id
* @since 1.1.0
*/
int layoutId() default -1;

/**
* 标记一个组件是否需要自动注册,如果为 true 会自动注册到 Flap
*
* @return true then Flap will auto register this component.
*
* @since 1.1.0
*/
boolean autoRegister() default true;

/**
* 是否使用 DataBinding,假如使用 DataBinding,那么组件的构造方法需要做一定的修改
*
* @return 如果要使用 DataBinding 则设置 true
*
* @since 1.5.1
*/
boolean useDataBinding() default false;
Expand Down
2 changes: 1 addition & 1 deletion flap-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish {
userOrg = 'alancheen'
groupId = 'me.yifeiyuan.flap'
artifactId = 'flap-compiler'
publishVersion = '1.6.1'
publishVersion = '2.0.0'
desc = 'flap annotations'
website = 'https://github.com/AlanCheen/Flap'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
Expand All @@ -32,33 +30,24 @@
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;

import me.yifeiyuan.flap.annotations.AutoRegister;
import me.yifeiyuan.flap.annotations.Proxy;

@AutoService(Processor.class)
public class FlapProcessor extends AbstractProcessor {

private static final String PKG_NAME_PROXIES = "me.yifeiyuan.flap.apt.proxies";
private static final String PKG_NAME_MANAGER = "me.yifeiyuan.flap.apt.manager";

private static final String NAME_SUFFIX = "Proxy";

private final ClassName CLASS_KEEP = ClassName.bestGuess("androidx.annotation.Keep");
private final ClassName CLASS_FLAP = ClassName.bestGuess("me.yifeiyuan.flap.Flap");
private final ClassName CLASS_COMPONENT_PROXY = ClassName.bestGuess("me.yifeiyuan.flap.internal.ComponentProxy");

private static final String KEY_OPTION_AUTO_REGISTER = "autoRegister";

private Filer filer;
private Elements elements;
private Types types;
private Messager messager;

/**
* 是否自动注册 Factories
*/
private boolean autoRegisterFactories = true;

@Override
public synchronized void init(final ProcessingEnvironment processingEnv) {
super.init(processingEnv);
Expand All @@ -67,11 +56,6 @@ public synchronized void init(final ProcessingEnvironment processingEnv) {
elements = processingEnv.getElementUtils(); // Get class meta.
messager = processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE, "FlapProcessor init");

Map<String, String> options = processingEnv.getOptions();
if (options.containsKey(KEY_OPTION_AUTO_REGISTER)) {
autoRegisterFactories = Boolean.parseBoolean(options.get(KEY_OPTION_AUTO_REGISTER));
}
}

@Override
Expand All @@ -81,9 +65,8 @@ public boolean process(final Set<? extends TypeElement> set, final RoundEnvironm

if (Proxy.class.getCanonicalName().equals(typeElement.getQualifiedName().toString())) {
processComponent(roundEnvironment, typeElement);
} else if (AutoRegister.class.getCanonicalName().equals(typeElement.getQualifiedName().toString())) {
processComponentProxyManager(roundEnvironment, typeElement);
}

}

return true;
Expand Down Expand Up @@ -115,7 +98,6 @@ private void processComponent(final RoundEnvironment roundEnvironment, final Typ
* @param typeElement @Component
* @param flapComponentElement 被 FlapComponent 注解了的那个类
* @param componentProxy 注解了目标类的 注解,可以获取值
*
* @return ComponentProxy TypeSpec
*/
private TypeSpec createComponentProxyTypeSpec(final RoundEnvironment roundEnvironment, final TypeElement typeElement, final TypeElement flapComponentElement, final Proxy componentProxy) {
Expand All @@ -126,9 +108,8 @@ private TypeSpec createComponentProxyTypeSpec(final RoundEnvironment roundEnviro
String targetClassName = flapComponentElement.getSimpleName().toString() + NAME_SUFFIX;

int layoutId = componentProxy.layoutId();
boolean autoRegister = componentProxy.autoRegister();

boolean dataBinding = componentProxy.useDataBinding();
boolean useDataBinding = componentProxy.useDataBinding();

DeclaredType declaredType = flapComponentElement.getSuperclass().accept(new FlapItemModelVisitor(), null);
List<? extends TypeMirror> args = declaredType.getTypeArguments();
Expand All @@ -147,8 +128,8 @@ private TypeSpec createComponentProxyTypeSpec(final RoundEnvironment roundEnviro
.addParameter(TypeName.INT, "layoutId")
.returns(flapItemClass);

if (dataBinding) {
onCreateViewHolderMethodBuilder.addStatement("return new $T(androidx.databinding.DataBindingUtil.inflate(inflater,layoutId,parent,false))",flapItemClass);
if (useDataBinding) {
onCreateViewHolderMethodBuilder.addStatement("return new $T(androidx.databinding.DataBindingUtil.inflate(inflater,layoutId,parent,false))", flapItemClass);
} else {
onCreateViewHolderMethodBuilder.addStatement("return new $T(inflater.inflate(layoutId,parent,false))", flapItemClass);
}
Expand Down Expand Up @@ -181,58 +162,6 @@ private TypeSpec createComponentProxyTypeSpec(final RoundEnvironment roundEnviro
.addMethod(getComponentModelClass)
.addSuperinterface(name);

if (autoRegister) {
builder.addAnnotation(AutoRegister.class);
}
return builder.build();
}

/**
* 处理 AutoRegister 注解,把需要自动注册的组件处理一下。
*
* @param roundEnvironment
* @param typeElement
*/
private void processComponentProxyManager(final RoundEnvironment roundEnvironment, final TypeElement typeElement) {

if (!autoRegisterFactories) {
return;
}
List<ClassName> factories = new ArrayList<>();

Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(AutoRegister.class);

for (final Element element : elements) {
TypeElement flapItemFactory = (TypeElement) element;
ClassName factoryClass = ClassName.get(flapItemFactory);
factories.add(factoryClass);
}

TypeSpec manager = TypeSpec.classBuilder("ComponentAutoRegister")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addAnnotation(CLASS_KEEP)
.addMethod(createInjectMethod(factories))
.build();

try {
JavaFile.builder(PKG_NAME_MANAGER, manager).build().writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}

}

private MethodSpec createInjectMethod(final List<ClassName> factories) {

MethodSpec.Builder builder = MethodSpec.methodBuilder("inject")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.returns(void.class)
.addParameter(CLASS_FLAP, "flap", Modifier.FINAL);

for (final ClassName factory : factories) {
builder.addStatement("flap.register(new $T())", factory);
}

return builder.build();
}

Expand All @@ -245,7 +174,6 @@ public SourceVersion getSupportedSourceVersion() {
public Set<String> getSupportedAnnotationTypes() {
Set<String> annotationTypes = new LinkedHashSet<>();
annotationTypes.add(Proxy.class.getCanonicalName());
annotationTypes.add(AutoRegister.class.getCanonicalName());
return annotationTypes;
}

Expand Down
4 changes: 2 additions & 2 deletions flap-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ apply plugin: 'com.novoda.bintray-release'
publish {
userOrg = 'alancheen'
groupId = 'me.yifeiyuan.flap'
artifactId = 'flap-plugin'
publishVersion = '1.0.0'
artifactId = 'plugin'
publishVersion = '1.0.1'
desc = 'flap plugin'
website = 'https://github.com/AlanCheen/Flap'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ class Log {

private static Logger logger;

private static boolean debug = false;

static void setup(Project project) {
logger = project.getLogger();
}

static void i(String info) {
logger.info("FlapPlugin:" + info);
if (debug) {
logger.info("FlapPlugin:" + info);
}
}

static void println(Object obj) {
System.out.println("FlapPlugin: " + obj);
}

static void println(String msg) {
System.out.println("FlapPlugin: " + msg);
if (debug) {
System.out.println("FlapPlugin: " + msg);
}
}
}
Loading

0 comments on commit e9ba528

Please sign in to comment.