Skip to content

Commit

Permalink
fix IntentParam bug & add Fragement Param init method && add Navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
foolishchow committed Apr 23, 2021
1 parent 543ae79 commit f2016a5
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
@Target(ElementType.FIELD)
public @interface FragmentParam {
boolean originName() default false;
boolean cacheId() default false;

/**
* use this argument to fragment tag
* @return
*/
boolean cacheToTag() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.foolishchow.android.annotation;

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

/**
* Description:
* Author: foolishchow
* Date: 23/4/2021 9:50 PM
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface Navigation {
NavigationAction[] actions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.foolishchow.android.annotation;

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

/**
* Description:
* Author: foolishchow
* Date: 23/4/2021 9:50 PM
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface NavigationAction {
String name();
String description() default "";
int actionId() default -1;
}
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
buildToolsVersion "30.0.0"

defaultConfig {
applicationId "me.foolishchow.androidplugins"
applicationId "me.foolishchow.autoparamdemo"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
Expand Down Expand Up @@ -40,14 +40,14 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//implementation "me.foolishchow.android:utils:0.0.8"
//implementation "me.foolishchow.android:annotation:0.0.8"

api 'androidx.navigation:navigation-fragment:2.3.4'
api 'androidx.navigation:navigation-ui:2.3.4'
api 'androidx.navigation:navigation-fragment:2.3.5'
api 'androidx.navigation:navigation-ui:2.3.5'


api project(":utils")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

import android.os.Bundle;

import java.util.List;

import me.foolishchow.android.annotation.IntentParam;


public class MainActivity extends AppCompatActivity {

@IntentParam
List<Integer> ints;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.foolishchow.androidplugins.fragment;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

/**
* Description:
* Author: foolishchow
* Date: 23/4/2021 8:22 PM
*/
public class BaseDialogFragment extends DialogFragment {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.foolishchow.androidplugins.fragment;

import androidx.fragment.app.Fragment;

/**
* Description:
* Author: foolishchow
* Date: 23/4/2021 8:22 PM
*/
public class BaseFragment extends Fragment {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import me.foolishchow.android.annotation.FragmentParam;
import me.foolishchow.android.annotation.IntentParam;
import me.foolishchow.androidplugins.R;

/**
* Description:
* Author: foolishchow
* Date: 13/3/2021 12:06 PM
*/
public class ChildFragment extends Fragment {
public class ChildFragment extends SecondBaseDialogFragment {

@FragmentParam(cacheToTag = true)
int param;

@FragmentParam(cacheToTag = true)
String param1;

@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
import java.util.List;

import me.foolishchow.android.annotation.FragmentParam;
import me.foolishchow.android.annotation.Navigation;
import me.foolishchow.android.annotation.NavigationAction;
import me.foolishchow.androidplugins.R;

/**
* Description:
* Author: foolishchow
* Date: 13/3/2021 12:06 PM
*/
public class MainFragment extends Fragment {

@FragmentParam
@Navigation(actions = {
@NavigationAction(
name = "fromMain",
actionId = R.id.action_main_to_child,
description = "从xx跳转"
)
})
public class MainFragment extends BaseFragment {

@FragmentParam(cacheToTag = true)
int in;

@FragmentParam
Expand Down Expand Up @@ -66,6 +75,7 @@ public class MainFragment extends Fragment {
boolean[] bools;
@FragmentParam
Text text;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -76,7 +86,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main,container,false);
return inflater.inflate(R.layout.fragment_main, container, false);
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.foolishchow.androidplugins.fragment;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

/**
* Description:
* Author: foolishchow
* Date: 23/4/2021 8:22 PM
*/
public class SecondBaseDialogFragment extends BaseDialogFragment {

@Override
public int getTheme() {
return super.getTheme();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import me.foolishchow.android.annotation.Constant;
import me.foolishchow.android.annotation.FragmentParam;
import me.foolishchow.android.annotation.IntentParam;
import me.foolishchow.android.annotation.Navigation;
import me.foolishchow.android.annotation.NavigationAction;
import me.foolishchow.anrdoid.processor.base.BaseAnnotationProcessor;

/**
Expand Down Expand Up @@ -74,11 +75,24 @@ public void process(
builder.addField(field.build());


if (!isDialog(elements)) {
Navigation annotation = mOriginClass.getAnnotation(Navigation.class);
if (annotation != null) {
NavigationAction[] actions = annotation.actions();
if (actions != null) {
for (NavigationAction action : actions) {
addNavigate(builder, action);
}

}
}
}
MethodSpec.Builder getBundle = MethodSpec
.methodBuilder("getBundle")
.addModifiers(Modifier.PUBLIC)
.returns(TypeNames.BUNDLE)
.addStatement("return mBundle");;
.addStatement("return mBundle");
;
builder.addMethod(getBundle.build());

MethodSpec.Builder withContext = MethodSpec
Expand Down Expand Up @@ -107,12 +121,18 @@ public void process(
String fieldName = element.getSimpleName().toString();
String keyName = camel2snake(fieldName);

TypeMirror typeMirror = element.asType();
TypeName typeName = ParameterizedTypeName.get(typeMirror);


FragmentParam annotation = element.getAnnotation(FragmentParam.class);
boolean originName = annotation.originName();
addFieldKey(builder, fieldName, keyName, originName);
boolean cacheToTag = annotation.cacheToTag();
if (cacheToTag) {
createField(builder, fieldName, typeName);
}

TypeMirror typeMirror = element.asType();
TypeName typeName = ParameterizedTypeName.get(typeMirror);

//System.out.println(String.format(
// "addElement=>name=%s,type=%s",
Expand All @@ -128,14 +148,99 @@ public void process(

//method.addComment(format("field %s type %s ", fieldName, typeName.toString()));
addParamStatement(elements, keyName, typeName, method);
if (cacheToTag) {
method.addStatement("this." + fieldName + " = param");
}
method.addStatement("return this");
builder.addMethod(method.build());

addParse(parse, elements, element, fieldName, keyName, typeName);

}

addNormalFragment(builder, originClassType);
addTag(builder, originClassType);
builder.addMethod(parse.build());

}

private void addNavigate(TypeSpec.Builder builder, NavigationAction action) {
//noinspection ConstantConditions
if (action.name() == null || action.name().length() == 0) return;

MethodSpec.Builder navigate = MethodSpec
.methodBuilder(action.name())
.addModifiers(Modifier.PUBLIC)
.addParameter(TypeNames.View, "view")
.returns(void.class);

navigate.addJavadoc(action.description() + "\n");

//Navigation
navigate.addStatement("$T.findNavController(view).navigate(" + action.actionId() + "," +
"mBundle) ", TypeNames.Navigation);
builder.addMethod(navigate.build());

}


private boolean isDialog(Elements elements) {
TypeMirror superclass = mOriginClass.getSuperclass();
if (superclass == null) {
return false;
}
boolean isDialog = false;
while (superclass != null) {
if (superclass.toString().equals("androidx.fragment.app.DialogFragment")) {
isDialog = true;
superclass = null;
} else {
TypeElement typeElement = elements.getTypeElement(superclass.toString());
superclass = typeElement == null ? null : typeElement.getSuperclass();
}
}
return isDialog;
}

private void addTag(
TypeSpec.Builder builder,
ClassName originClassType
) {
StringBuilder tagName = new StringBuilder("\"" + originClassType.simpleName() + "\"");
for (Element element : mElements) {
FragmentParam annotation = element.getAnnotation(FragmentParam.class);
boolean cacheToTag = annotation.cacheToTag();
if (cacheToTag) {
tagName.append(" + \"-\" + ").append(element.getSimpleName().toString());
}
}

MethodSpec.Builder getBundle = MethodSpec
.methodBuilder("getTag")
.addModifiers(Modifier.PUBLIC)
.returns(String.class);
getBundle.addStatement("return " + tagName.toString() + "");
builder.addMethod(getBundle.build());
}

private void addNormalFragment(
TypeSpec.Builder builder,
ClassName originClassType
) {
MethodSpec.Builder getBundle = MethodSpec
.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.returns(originClassType);
getBundle.addStatement("$T fragment = null", originClassType)
.beginControlFlow("try")
.addStatement("fragment = $T.class.newInstance()", originClassType)
.addStatement("fragment.setArguments(mBundle)")
.nextControlFlow("catch ($T e)", TypeNames.Throwable)
.addStatement("e.printStackTrace()")
.endControlFlow()
.addStatement("assert fragment != null");
getBundle.addStatement("return fragment");
builder.addMethod(getBundle.build());
}

private void addParamStatement(
Expand Down Expand Up @@ -295,6 +400,15 @@ private void addParse(
parse.endControlFlow();
}

private void createField(TypeSpec.Builder builder, String fieldName, TypeName typeName) {

FieldSpec.Builder field = FieldSpec.builder(
typeName,
fieldName,
Modifier.PRIVATE
);
builder.addField(field.build());
}

private void addFieldKey(TypeSpec.Builder builder, String fieldName, String keyName, boolean originName) {
FieldSpec.Builder field = FieldSpec.builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void addParamStatement(Elements elements, String keyName, TypeName typeN
} else if (TypeUtils.isCharSequenceArrayList(elements, typeName)) {
method.addStatement("mIntent.putCharSequenceArrayListExtra(" + keyName + ",new $T<$T>(param))", TypeNames.ARRAY_LIST, TypeNames.CharSequence);
} else if (TypeUtils.isIntegerArrayList(typeName)) {
method.addStatement("mIntent.putCharSequenceArrayListExtra(" + keyName + ",new $T<$T>(param))", TypeNames.ARRAY_LIST, TypeNames.INTEGER);
method.addStatement("mIntent.putIntegerArrayListExtra(" + keyName + ",new $T<$T>(param))", TypeNames.ARRAY_LIST, TypeNames.INTEGER);
} else if (TypeUtils.isParcelableArrayList(elements, typeName)) {
method.addStatement("mIntent.putParcelableArrayListExtra(" + keyName + ",new $T<$T>(param))", TypeNames.ARRAY_LIST, TypeNames.PARCELABLE);
} else if (TypeUtils.isList(typeName)) {
Expand Down
Loading

0 comments on commit f2016a5

Please sign in to comment.