auto-droid
Introduction: AutoValue Extension for Android
Tags:
Android-Auto-An extension for Google's AutoValue that generates the following for for @AutoValue annotated objects:
createFromSharedPreferences(SharedPreferences sharedPreferences)createFromCursor(Cursor cursor)- Parcelable implementations if the class
implements Parcelable
Download
Add the following to your build.gradle:
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
repositories {
maven { url "https://jitpack.io" }
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'com.github.xizzhu.auto-droid:auto-droid-annotations:0.2.1'
apt 'com.github.xizzhu.auto-droid:auto-droid-processors:0.2.1'
}
Usage
From shared preference
// annotate your AutoValue objects as usual
@AutoValue
public abstract class MyPreference {
// indicate that the factory will retrieve the value from shared preferences
@SharedPreference(key = "keyOfMyInt", defaultValue = "8964")
abstract int myIntFromPreference();
// adds the factory method
public static MyPreference create(SharedPreferences sharedPreferences) {
return AutoValue_MyPreference.createFromSharedPreferences(sharedPreferences);
}
}
For objects constructed from shared preferences, the following types are supported:
boolean/Boolean: the default value isfalseif not specifiedint/Integer: the default value is0if not specifiedlong/Long: the default value is0Lif not specifiedfloat/Float: the default value is0.0Fif not specifieddouble/Double: the default value is0.0if not specifiedString: the default value isnullif not specifiedSet<String>: the default value is alwaysnull
From cursor
// annotate your AutoValue objects as usual
@AutoValue
public abstract class MyCursor {
// indicate that the factory will retrieve the value from cursor
@ColumnName("keyOfMyInt")
abstract int myIntFromCursor();
// indicate to use MyColumnAdapterFactory to generate the value
// MyColumnAdapterFactory must provide a `static` method that
// takes a `Cursor` and returns a `MyColumnAdapter`
@ColumnAdapter(MyColumnAdapterFactory.class)
abstract MyColumnAdapter myCustomColumn();
// indicate to implement a method to create a ContentValues object with values put in
abstract ContentValues toContentValues();
// adds the factory method
public static MyCursor create(Cursor cursor) {
return AutoValue_MyCursor.createFromCursor(cursor);
}
}
For objects constructed from cursors, the following types are supported:
int/Integerlong/Longshort/Shortfloat/Floatdouble/Doublebyte[]String
Implements parcelable
// annotate your AutoValue objects as usual
// and just tells that it impements Parcelable
@AutoValue
public abstract class MyCursor implements Parcelable {
// whatever you may have here
}
License
Copyright 2016 Xizhi Zhu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
