Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Add support for ANDROIDX_PREFERENCE*
  • Loading branch information
dodgex committed Jul 1, 2018
commit 1eddc93ef65af93d1a1b75836ba11e1bf678f9ae
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class ValidatorHelper {

private static final List<String> VALID_PREFERENCE_CLASSES = asList(CanonicalNameConstants.PREFERENCE_ACTIVITY, CanonicalNameConstants.PREFERENCE_FRAGMENT,
CanonicalNameConstants.SUPPORT_V4_PREFERENCE_FRAGMENT, CanonicalNameConstants.MACHINARIUS_V4_PREFERENCE_FRAGMENT, CanonicalNameConstants.SUPPORT_V7_PREFERENCE_FRAGMENTCOMPAT,
CanonicalNameConstants.SUPPORT_V14_PREFERENCE_FRAGMENT);
CanonicalNameConstants.SUPPORT_V14_PREFERENCE_FRAGMENT, CanonicalNameConstants.ANDROIDX_PREFERENCE_FRAGMENT, CanonicalNameConstants.ANDROIDX_PREFERENCE_FRAGMENTCOMPAT);

protected final TargetAnnotationHelper annotationHelper;
private final ParcelerHelper parcelerHelper;
Expand Down Expand Up @@ -500,7 +500,7 @@ public void extendsListOfView(Element element, ElementValidation valid) {
}

public void extendsPreference(Element element, ElementValidation validation) {
extendsOneOfTypes(element, asList(CanonicalNameConstants.PREFERENCE, CanonicalNameConstants.SUPPORT_V7_PREFERENCE), validation);
extendsOneOfTypes(element, asList(CanonicalNameConstants.PREFERENCE, CanonicalNameConstants.SUPPORT_V7_PREFERENCE, CanonicalNameConstants.ANDROIDX_PREFERENCE), validation);
}

public void extendsOneOfTypes(Element element, List<String> typeQualifiedNames, ElementValidation valid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,11 @@ public boolean usingSupportV7Preference() {
return preferencesHolder.usingSupportV7Preference();
}

@Override
public boolean usingAndroidxPreference() {
return preferencesHolder.usingAndroidxPreference();
}

public AbstractJClass getBasePreferenceClass() {
return preferencesHolder.getBasePreferenceClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ public boolean usingSupportV7Preference() {
return preferencesDelegate.usingSupportV7Preference();
}

@Override
public boolean usingAndroidxPreference() {
return preferencesDelegate.usingAndroidxPreference();
}

public AbstractJClass getBasePreferenceClass() {
return preferencesDelegate.getBasePreferenceClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public interface HasPreferences extends GeneratedClassHolder {

boolean usingSupportV7Preference();

boolean usingAndroidxPreference();

AbstractJClass getBasePreferenceClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
import com.helger.jcodemodel.JMethod;
import com.helger.jcodemodel.JVar;

public class PreferencesDelegate extends GeneratedClassHolderDelegate<EComponentWithViewSupportHolder>implements HasPreferences {
public class PreferencesDelegate extends GeneratedClassHolderDelegate<EComponentWithViewSupportHolder> implements HasPreferences {

protected JBlock addPreferencesFromResourceInjectionBlock;
protected JBlock addPreferencesFromResourceAfterInjectionBlock;

private boolean usingSupportV7Preference = false;
private boolean usingAndroidxPreference = false;
private AbstractJClass basePreferenceClass;

public PreferencesDelegate(EComponentWithViewSupportHolder holder) {
Expand All @@ -51,12 +52,17 @@ public PreferencesDelegate(EComponentWithViewSupportHolder holder) {
Types typeUtils = holder.getEnvironment().getProcessingEnvironment().getTypeUtils();

TypeElement supportV7PreferenceFragmentCompat = elementUtils.getTypeElement(CanonicalNameConstants.SUPPORT_V7_PREFERENCE_FRAGMENTCOMPAT);

TypeElement andoridxPreferenceFragmentCompat = elementUtils.getTypeElement(CanonicalNameConstants.ANDROIDX_PREFERENCE_FRAGMENTCOMPAT);
TypeElement supportV14PreferenceFragment = elementUtils.getTypeElement(CanonicalNameConstants.SUPPORT_V14_PREFERENCE_FRAGMENT);
TypeElement andoridxPreferenceFragment = elementUtils.getTypeElement(CanonicalNameConstants.ANDROIDX_PREFERENCE_FRAGMENT);

TypeMirror annotatedType = holder.getAnnotatedElement().asType();

if (supportV7PreferenceFragmentCompat != null && typeUtils.isSubtype(annotatedType, supportV7PreferenceFragmentCompat.asType())
if (andoridxPreferenceFragmentCompat != null && typeUtils.isSubtype(annotatedType, andoridxPreferenceFragmentCompat.asType())
|| andoridxPreferenceFragment != null && typeUtils.isSubtype(annotatedType, andoridxPreferenceFragment.asType())) {
usingAndroidxPreference = true;
basePreferenceClass = getClasses().ANDROIDX_PREFERENCE;
} else if (supportV7PreferenceFragmentCompat != null && typeUtils.isSubtype(annotatedType, supportV7PreferenceFragmentCompat.asType())
|| supportV14PreferenceFragment != null && typeUtils.isSubtype(annotatedType, supportV14PreferenceFragment.asType())) {
usingSupportV7Preference = true;
basePreferenceClass = getClasses().SUPPORT_V7_PREFERENCE;
Expand Down Expand Up @@ -117,6 +123,11 @@ public boolean usingSupportV7Preference() {
return usingSupportV7Preference;
}

@Override
public boolean usingAndroidxPreference() {
return usingAndroidxPreference;
}

@Override
public AbstractJClass getBasePreferenceClass() {
return basePreferenceClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void validate(Element element, ElementValidation valid) {
validatorHelper.returnTypeIsVoidOrBoolean(executableElement, valid);

validatorHelper.param.anyOrder() //
.extendsAnyOfTypes(CanonicalNameConstants.PREFERENCE, CanonicalNameConstants.SUPPORT_V7_PREFERENCE).optional() //
.extendsAnyOfTypes(CanonicalNameConstants.PREFERENCE, CanonicalNameConstants.SUPPORT_V7_PREFERENCE, CanonicalNameConstants.ANDROIDX_PREFERENCE).optional() //
.anyOfTypes(CanonicalNameConstants.OBJECT, CanonicalNameConstants.STRING_SET, CanonicalNameConstants.STRING, //
CanonicalNameConstants.BOOLEAN, boolean.class.getName(), //
CanonicalNameConstants.INTEGER, int.class.getName(), //
Expand All @@ -78,15 +78,17 @@ protected void makeCall(JBlock listenerMethodBody, JInvocation call, TypeMirror
@Override
protected void processParameters(HasPreferences holder, JMethod listenerMethod, JInvocation call, List<? extends VariableElement> userParameters) {
JVar preferenceParam = listenerMethod.param(holder.getBasePreferenceClass(), "preference");

JVar newValueParam = listenerMethod.param(getClasses().OBJECT, "newValue");

for (VariableElement variableElement : userParameters) {
String type = variableElement.asType().toString();
if (isTypeOrSubclass(CanonicalNameConstants.PREFERENCE, variableElement)) {
call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.PREFERENCE, preferenceParam, variableElement));
} else if (isTypeOrSubclass(CanonicalNameConstants.SUPPORT_V7_PREFERENCE, variableElement)) {
} else if (isTypeOrSubclass(CanonicalNameConstants.SUPPORT_V7_PREFERENCE, variableElement)) {
call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.SUPPORT_V7_PREFERENCE, preferenceParam, variableElement));
} else if (isTypeOrSubclass(CanonicalNameConstants.ANDROIDX_PREFERENCE, variableElement)) {
call.arg(castArgumentIfNecessary(holder, CanonicalNameConstants.ANDROIDX_PREFERENCE, preferenceParam, variableElement));
} else if (type.equals(CanonicalNameConstants.OBJECT)) {
call.arg(newValueParam);
} else if (type.equals(CanonicalNameConstants.INTEGER) || type.equals(int.class.getName()) || //
Expand Down Expand Up @@ -117,6 +119,7 @@ protected String getSetterName() {

@Override
protected AbstractJClass getListenerClass(HasPreferences holder) {
return holder.usingSupportV7Preference() ? getClasses().SUPPORT_V7_PREFERENCE_CHANGE_LISTENER : getClasses().PREFERENCE_CHANGE_LISTENER;
return holder.usingSupportV7Preference() ? getClasses().SUPPORT_V7_PREFERENCE_CHANGE_LISTENER
: holder.usingAndroidxPreference() ? getClasses().ANDROIDX_PREFERENCE_CHANGE_LISTENER : getClasses().PREFERENCE_CHANGE_LISTENER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void validate(Element element, ElementValidation valid) {
validatorHelper.returnTypeIsVoidOrBoolean(executableElement, valid);

validatorHelper.param //
.extendsAnyOfTypes(CanonicalNameConstants.PREFERENCE, CanonicalNameConstants.SUPPORT_V7_PREFERENCE).optional() //
.extendsAnyOfTypes(CanonicalNameConstants.PREFERENCE, CanonicalNameConstants.SUPPORT_V7_PREFERENCE, CanonicalNameConstants.ANDROIDX_PREFERENCE).optional() //
.validate(executableElement, valid);
}

Expand Down Expand Up @@ -92,7 +92,8 @@ protected String getSetterName() {

@Override
protected AbstractJClass getListenerClass(HasPreferences holder) {
return holder.usingSupportV7Preference() ? getClasses().SUPPORT_V7_PREFERENCE_CLICK_LISTENER : getClasses().PREFERENCE_CLICK_LISTENER;
return holder.usingSupportV7Preference() ? getClasses().SUPPORT_V7_PREFERENCE_CLICK_LISTENER
: holder.usingAndroidxPreference() ? getClasses().ANDROIDX_PREFERENCE_CLICK_LISTENER : getClasses().PREFERENCE_CLICK_LISTENER;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public class Classes {
public final AbstractJClass ANDROIDX_FRAGMENT_ACTIVITY = refClass(CanonicalNameConstants.ANDROIDX_FRAGMENT_ACTIVITY);
public final AbstractJClass ANDROIDX_LOCAL_BROADCAST_MANAGER = refClass(CanonicalNameConstants.ANDROIDX_LOCAL_BROADCAST_MANAGER);
public final AbstractJClass ANDROIDX_CONTEXT_COMPAT = refClass(CanonicalNameConstants.ANDROIDX_CONTEXT_COMPAT);
public final AbstractJClass ANDROIDX_PREFERENCE = refClass(CanonicalNameConstants.ANDROIDX_PREFERENCE);
public final AbstractJClass ANDROIDX_PREFERENCE_CLICK_LISTENER = refClass(CanonicalNameConstants.ANDROIDX_PREFERENCE_CLICK_LISTENER);
public final AbstractJClass ANDROIDX_PREFERENCE_CHANGE_LISTENER = refClass(CanonicalNameConstants.ANDROIDX_PREFERENCE_CHANGE_LISTENER);

/*
* HttpClient
Expand Down