Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 @@ -31,6 +31,36 @@
* The annotation value must be one of R.* fields. If the value is not set, the
* method name will be used as the R.* field name.
* </p>
* <table summary="Mapping of return types to thier resource types">
* <tr>
* <th>Return Type</th>
* <th>Resource Type</th>
* </tr>
* <tr>
* <td>String</td>
* <td>R.string.*</td>
* </tr>
* <tr>
* <td>int</td>
* <td>R.integer.*</td>
* </tr>
* <tr>
* <td>long</td>
* <td>R.integer.*</td>
* </tr>
* <tr>
* <td>float</td>
* <td>R.integer.*</td>
* </tr>
* <tr>
* <td>boolean</td>
* <td>R.bool.*</td>
* </tr>
* <tr>
* <td>Set&lt;String&gt;</td>
* <td>R.array.*</td>
* </tr>
* </table>
* <p>
* The key of the preference will be the method name by default. This can be
* overridden by specifying a string resource with the {@link #keyRes()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.androidannotations.annotations.sharedpreferences.DefaultFloat;
import org.androidannotations.annotations.sharedpreferences.DefaultInt;
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
import org.androidannotations.annotations.sharedpreferences.DefaultRes;
import org.androidannotations.annotations.sharedpreferences.DefaultString;
import org.androidannotations.annotations.sharedpreferences.DefaultStringSet;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
Expand Down Expand Up @@ -60,4 +61,7 @@ public interface SomePrefs {

@DefaultStringSet({})
Set<String> emtpyStringSet();

@DefaultRes(R.array.planets_array)
Set<String> planetsStringSet();
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,19 @@ public void getStringSetEmptySetDefaultValue() {

assertThat(somePrefs.types().get()).isEmpty();
}

@Test
public void getStringSetEmptyWithDefaultValue() {
sharedPref.edit().clear().commit();

assertThat(somePrefs.emtpyStringSet().get()).isEmpty();
}

@Test
public void getStringSetWithDefaultRes() {
sharedPref.edit().clear().commit();

assertThat(somePrefs.planetsStringSet().get().size()).isEqualTo(8);
assertThat(somePrefs.planetsStringSet().get()).contains("Earth");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static final class DefaultPrefInfo<T> {
put("int", new DefaultPrefInfo<>(DefaultInt.class, IntPrefField.class, IRClass.Res.INTEGER, 0, "intField"));
put("long", new DefaultPrefInfo<>(DefaultLong.class, LongPrefField.class, IRClass.Res.INTEGER, 0L, "longField"));
put(CanonicalNameConstants.STRING, new DefaultPrefInfo<>(DefaultString.class, StringPrefField.class, IRClass.Res.STRING, "", "stringField"));
put(CanonicalNameConstants.STRING_SET, new DefaultPrefInfo<Set<String>>(DefaultStringSet.class, StringSetPrefField.class, null, null, "stringSetField"));
put(CanonicalNameConstants.STRING_SET, new DefaultPrefInfo<Set<String>>(DefaultStringSet.class, StringSetPrefField.class, IRClass.Res.ARRAY, null, "stringSetField"));
}
};

Expand Down Expand Up @@ -322,10 +322,21 @@ private IJExpression extractResValue(SharedPrefHolder holder, Element method, IR
case STRING:
resourceGetMethodName = "getString";
break;
case ARRAY:
resourceGetMethodName = "getStringArray";
break;
default:
break;
}
return holder.getContextField().invoke("getResources").invoke(resourceGetMethodName).arg(idRef);

JInvocation resourceInvocation = holder.getContextField().invoke("getResources").invoke(resourceGetMethodName).arg(idRef);

if (IRClass.Res.ARRAY.equals(res)) {
JInvocation asList = getClasses().ARRAYS.staticInvoke("asList");
JInvocation newHashMap = JExpr._new(getClasses().HASH_SET.narrow(getClasses().STRING));
resourceInvocation = newHashMap.arg(asList.arg(resourceInvocation));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should handle the empty array here as well (I think it is possible to define an empty string array resource or not?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emtpy string array resource should not be a prolem. this code generates new HashSet<>(Arrays.asList(res.getStringArray(R.array.xxx))); if the resource is empty, the array is emtpy, the list is empty and the set is empty. also i think we have no way to access the number of items of a resource on compile time as the value might be diffrent for e.g. locales or screen sizes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

}
return resourceInvocation;
}

private IJExpression newEmptyStringHashSet() {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.