Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
@Target(ElementType.METHOD)
public @interface DefaultStringSet {

/**
* The default value of the preference.
*
* @return the default value
*/
String[] value();

/**
* The R.string.* field which refers to the key of the preference.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.androidannotations.annotations.sharedpreferences.DefaultInt;
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
import org.androidannotations.annotations.sharedpreferences.DefaultString;
import org.androidannotations.annotations.sharedpreferences.DefaultStringSet;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
import org.androidannotations.annotations.sharedpreferences.SharedPref.Scope;
import org.androidannotations.test.R;
Expand Down Expand Up @@ -50,4 +51,7 @@ public interface SomePrefs {
long lastUpdated();

Set<String> types();

@DefaultStringSet({"a", "b", "c"})
Set<String> setWithDefault();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.fest.assertions.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

Expand Down Expand Up @@ -87,6 +88,12 @@ public void putStringSet() {
assertThat(sharedPref.getStringSet("types", null)).isEqualTo(values);
}

@Test
public void checkSetDefaultValues() {
Set<String> values = new HashSet<String>(Arrays.asList("a", "b", "c"));
assertThat(somePrefs.setWithDefault().get()).isEqualTo(values);
}

@Test
public void editLong() {
long now = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -64,6 +66,7 @@
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldRef;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JVar;

Expand Down Expand Up @@ -247,14 +250,28 @@ private JExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement
JExpression defaultValueExpr;

Object value = null;
if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
if (annotation != null) {
value = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value");
}

if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
defaultValueExpr = codeModelHelper.litObject(value);
} else if (method.getAnnotation(DefaultRes.class) != null) {
defaultValueExpr = extractResValue(holder, method, resType);
annotationClass = DefaultRes.class;
} else if (method.getAnnotation(DefaultStringSet.class) != null) {
defaultValueExpr = newEmptyStringHashSet();
if (value != null) {
Set<String> arrayValues = new HashSet<>(Arrays.asList((String[]) value));
value = arrayValues;

JInvocation arrayAsList = getClasses().ARRAYS.staticInvoke("asList");
for (String arrayValue : arrayValues) {
arrayAsList.arg(lit(arrayValue));
}
defaultValueExpr = JExpr._new(getClasses().HASH_SET.narrow(getClasses().STRING)).arg(arrayAsList);
} else {
defaultValueExpr = newEmptyStringHashSet();
}
annotationClass = DefaultStringSet.class;
} else {
defaultValueExpr = defaultValue != null ? codeModelHelper.litObject(defaultValue) : newEmptyStringHashSet();
Expand Down