Skip to content

Commit

Permalink
Merge pull request #4 from Ne1c/dev
Browse files Browse the repository at this point in the history
Release 1.2.3
  • Loading branch information
Nikolay Kucheriaviy authored Jan 21, 2018
2 parents 9cc004a + 2762704 commit bc3fa37
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 18 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:3.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ org.gradle.jvmargs=-Xmx1536m
# 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
# org.gradle.parallel=true

-Dorg.gradle.debug=true
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Aug 02 23:16:00 EEST 2016
#Thu Jan 04 22:33:23 EET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
1 change: 1 addition & 0 deletions rainbowmvp-annotations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
8 changes: 8 additions & 0 deletions rainbowmvp-annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apply plugin: 'java'

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ne1c.rainbowmvp.annotaions;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface PresenterTag {
String value();
}
1 change: 1 addition & 0 deletions rainbowmvp-processor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
12 changes: 12 additions & 0 deletions rainbowmvp-processor/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import org.gradle.internal.jvm.Jvm

apply plugin: 'java'

dependencies {
implementation files(Jvm.current().getToolsJar())
implementation project(':rainbowmvp-annotations')
implementation 'com.squareup:javapoet:1.9.0'
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.ne1c.rainbowmvp.processor;

import com.ne1c.rainbowmvp.annotaions.PresenterTag;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;
import com.sun.source.util.Trees;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Names;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;

// Run for debug: ./gradlew --no-daemon -Dorg.gradle.debug=true :sample:clean :sample:compileDebugJavaWithJavac

public class PresenterTagProcessor extends AbstractProcessor {
private Messager messager;
private Filer filer;
private TreeMaker treeMaker;
private Trees trees;
private Names names;

@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);

trees = Trees.instance(env);
messager = env.getMessager();
filer = env.getFiler();

JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) env;
treeMaker = TreeMaker.instance(javacEnv.getContext());
names = Names.instance(javacEnv.getContext());
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element e : roundEnv.getElementsAnnotatedWith(PresenterTag.class)) {
if (e.getKind() != ElementKind.CLASS) {
messager.printMessage(Diagnostic.Kind.ERROR, "Can be applied only to class.");
return true;
}

String presenterTag = e.getAnnotation(PresenterTag.class).value();

MethodSpec getPresenterTagMethod = MethodSpec.methodBuilder("getPresenterTag")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(String.class)
.addStatement("return $S", presenterTag)
.build();

TypeMirror baseActivity = ((TypeElement) e).getSuperclass();
String baseActivityPath = "com.ne1c.rainbowmvp.base.BaseActivity";

if (!((Type.ClassType) baseActivity).asElement().toString().equals(baseActivityPath)) {
messager.printMessage(Diagnostic.Kind.ERROR,
"Can be annotated only classes that inherit com.ne1c.rainbowmvp.base.BaseActivity");
}

TypeSpec typeSpec = TypeSpec.classBuilder(e.getSimpleName().toString() + "$$PresenterTagProxy")
.addModifiers(Modifier.PUBLIC)
.addMethod(getPresenterTagMethod)
.superclass(ParameterizedTypeName.get(baseActivity))
.build();

JavaFile javaFile = JavaFile.builder(e.getEnclosingElement().toString(), typeSpec)
.addFileComment("Generated by RainbowMVP processor, don't modify")
.build();

try {
javaFile.writeTo(filer);

JCTree.JCExpression selector = treeMaker.Ident(names.fromString(javaFile.packageName));
selector = treeMaker.Select(selector, names.fromString(typeSpec.name));
((JCTree.JCClassDecl) trees.getTree(e)).extending = selector;
} catch (IOException ex) {
ex.printStackTrace();
}
}

return true;
}

@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet<>();
annotations.add(PresenterTag.class.getCanonicalName());
return annotations;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.ne1c.rainbowmvp.processor.PresenterTagProcessor
5 changes: 3 additions & 2 deletions rainbowmvp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
buildToolsVersion '26.0.2'

defaultConfig {
minSdkVersion 9
Expand All @@ -21,6 +21,7 @@ android {
group = 'com.github.ne1c'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.2.1'
compile project(':rainbowmvp-annotations')
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ protected P getPresenter() {
return mPresenter;
}

protected abstract String getPresenterTag();
protected String getPresenterTag() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.ne1c.rainbowmvp.ViewState;
import com.ne1c.rainbowmvp.ViewStateListener;

public abstract class BasePresenter<V> {
public abstract class BasePresenter<V extends BaseView> {
private V mView;

private ViewState mLastState = ViewState.NOTHING;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ne1c.rainbowmvp.base;

public interface BaseView {
}
3 changes: 2 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.ne1c.rainbowmvpexample"
minSdkVersion 9
Expand Down Expand Up @@ -34,4 +34,5 @@ dependencies {

testCompile 'junit:junit:4.12'
compile project(':rainbowmvp')
annotationProcessor project(':rainbowmvp-processor')
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import rx.schedulers.Schedulers;

public class MainPresenter extends BasePresenter<MainView> implements ViewStateListener {
public static final String TAG = MainPresenter.class.getName();
public static final String TAG = "main_presenter";

private ReposApi mApi;

Expand Down Expand Up @@ -63,7 +63,7 @@ public ArrayList<RepoModel> call(RepoResponse response) {
.subscribe(new Action1<ArrayList<RepoModel>>() {
@Override
public void call(ArrayList<RepoModel> repoModels) {
setViewState(ViewState.FINISH, false);
setViewState(ViewState.FINISH);

if (getView() != null) {
getView().showRepos(repoModels);
Expand All @@ -74,7 +74,7 @@ public void call(ArrayList<RepoModel> repoModels) {
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
setViewState(ViewState.ERROR, false);
setViewState(ViewState.ERROR);

if (getView() != null) {
getView().showError(R.string.something_happened);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import com.ne1c.rainbowmvp.annotaions.PresenterTag;
import com.ne1c.rainbowmvp.base.BaseActivity;
import com.ne1c.rainbowmvpexample.R;
import com.ne1c.rainbowmvpexample.api.RepoModel;
Expand All @@ -15,6 +16,7 @@

import java.util.ArrayList;

@PresenterTag(MainPresenter.TAG)
public class MainActivity extends BaseActivity<MainPresenter> implements MainView {
private ListView mReposListView;
private ProgressBar mProgressBar;
Expand Down Expand Up @@ -48,11 +50,6 @@ protected void onStop() {
getPresenter().unbindView();
}

@Override
protected String getPresenterTag() {
return MainPresenter.TAG;
}

@Override
public void showRepos(ArrayList<RepoModel> repos) {
mRepos.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import android.support.annotation.StringRes;

import com.ne1c.rainbowmvp.base.BaseView;
import com.ne1c.rainbowmvpexample.api.RepoModel;

import java.util.ArrayList;

public interface MainView {
public interface MainView extends BaseView {
void showRepos(ArrayList<RepoModel> repos);

void showError(@StringRes int resId);
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':sample', ':rainbowmvp'
include ':sample', ':rainbowmvp', ':rainbowmvp-annotations', ':rainbowmvp-processor'

0 comments on commit bc3fa37

Please sign in to comment.