Search in sources :

Example 1 with PrefsTransform

use of com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateWriteMethod.

/**
 * @param entity
 */
private static void generateWriteMethod(PrefsEntity entity) {
    // write method
    MethodSpec.Builder method = MethodSpec.methodBuilder("write").addJavadoc("write bean entirely\n\n").addJavadoc("@param bean bean to entirely write\n").addModifiers(Modifier.PUBLIC).addParameter(typeName(entity.getName()), "bean").returns(Void.TYPE);
    method.addStatement("$T editor=prefs.edit()", SharedPreferences.Editor.class);
    PrefsTransform transform;
    for (PrefsProperty item : entity.getCollection()) {
        if (item.hasTypeAdapter()) {
            transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
        } else {
            transform = PrefsTransformer.lookup(item);
        }
        transform.generateWriteProperty(method, "editor", typeName(entity.getElement()), "bean", item);
        method.addCode("\n");
    }
    method.addCode("\n");
    method.addStatement("editor.commit()");
    builder.addMethod(method.build());
}
Also used : PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) BindSharedPreferences(com.abubusoft.kripton.android.annotation.BindSharedPreferences) SharedPreferences(android.content.SharedPreferences) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Example 2 with PrefsTransform

use of com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateReadMethod.

private static void generateReadMethod(PrefsEntity entity) {
    // read method
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("read").addModifiers(Modifier.PUBLIC).addJavadoc("read bean entirely\n\n").addJavadoc("@return read bean\n").returns(typeName(entity.getName()));
    methodBuilder.addStatement("$T bean=new $T()", typeName(entity.getName()), typeName(entity.getName()));
    PrefsTransform transform;
    for (PrefsProperty item : entity.getCollection()) {
        if (item.hasTypeAdapter()) {
            transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
        } else {
            transform = PrefsTransformer.lookup(item);
        }
        transform.generateReadProperty(methodBuilder, "prefs", typeName(item.getElement().asType()), "bean", item, true);
        methodBuilder.addCode("\n");
    }
    methodBuilder.addCode("\n");
    methodBuilder.addStatement("return bean");
    builder.addMethod(methodBuilder.build());
}
Also used : PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Example 3 with PrefsTransform

use of com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateSingleReadMethod.

private static void generateSingleReadMethod(PrefsEntity entity) {
    // read method
    PrefsTransform transform;
    for (PrefsProperty item : entity.getCollection()) {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(item.getName()).addModifiers(Modifier.PUBLIC).addJavadoc("read property $L\n\n", item.getName()).addJavadoc("@return property $L value\n", item.getName()).returns(item.getPropertyType().getTypeName());
        if (item.hasTypeAdapter()) {
            transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
        } else {
            transform = PrefsTransformer.lookup(item);
        }
        transform.generateReadProperty(methodBuilder, "prefs", typeName(item.getElement().asType()), "defaultBean", item, false);
        methodBuilder.addCode("\n");
        builder.addMethod(methodBuilder.build());
    }
}
Also used : PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Example 4 with PrefsTransform

use of com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform in project kripton by xcesco.

the class BindSharedPreferencesBuilder method generateEditor.

/**
 * create editor
 *
 * @param entity
 */
private static void generateEditor(PrefsEntity entity) {
    com.abubusoft.kripton.common.Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
    Builder innerClassBuilder = TypeSpec.classBuilder("BindEditor").addModifiers(Modifier.PUBLIC).addJavadoc("editor class for shared preferences\n").superclass(typeName("AbstractEditor"));
    innerClassBuilder.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build());
    PrefsTransform transform;
    // write method
    for (PrefsProperty item : entity.getCollection()) {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("put" + converter.convert(item.getName())).addModifiers(Modifier.PUBLIC).addParameter(typeName(item.getElement()), "value").addJavadoc("modifier for property $L\n", item.getName()).returns(typeName("BindEditor"));
        TypeName type;
        if (item.hasTypeAdapter()) {
            type = typeName(item.typeAdapter.dataType);
        } else {
            type = TypeUtility.typeName(item.getElement());
        }
        transform = PrefsTransformer.lookup(type);
        transform.generateWriteProperty(builder, "editor", null, "value", item);
        builder.addCode("\n");
        builder.addStatement("return this");
        innerClassBuilder.addMethod(builder.build());
    }
    builder.addType(innerClassBuilder.build());
}
Also used : TypeName(com.squareup.javapoet.TypeName) PrefsProperty(com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) PrefsTransform(com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)

Aggregations

PrefsProperty (com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty)4 PrefsTransform (com.abubusoft.kripton.processor.sharedprefs.transform.PrefsTransform)4 MethodSpec (com.squareup.javapoet.MethodSpec)4 SharedPreferences (android.content.SharedPreferences)1 BindSharedPreferences (com.abubusoft.kripton.android.annotation.BindSharedPreferences)1 TypeName (com.squareup.javapoet.TypeName)1 Builder (com.squareup.javapoet.TypeSpec.Builder)1