dkunzler / esperandro

Easy SharedPreference Engine foR ANDROid
Other
180 stars 14 forks source link

Support functions or non constant values for default values #49

Closed MFlisar closed 6 years ago

MFlisar commented 6 years ago

Not sure if this is possible, but following (or something functional equivalent) would be nice:

@Default(ofInt = Color.argb(100, 0, 0, 0))
int handleColor();
boolean handleColor(int handleColor);

Any ideas on this?

dkunzler commented 6 years ago

Unfortunately it is very limited on what is possible inside an Annotation.

Since it must be static in any case would somethin like that work for you:

@Default(ofFunction = "android.graphics.Color.argb(100, 0, 0, 0)")
int handleColor();
boolean handleColor(int handleColor);

Notice that the value here would be a string to keep it constant. I would just put this string as is into the place where the default is defined. Means that it would always need to be fully-qualified.

MFlisar commented 6 years ago

That would be ok.

Something like following should work as well then, shouldn't it?

int defHandleColor = Color.argb(100, 0, 0, 0);
@Default(ofFunction = "defHandleColor")
int handleColor();
boolean handleColor(int handleColor);
dkunzler commented 6 years ago

This should work now in 2.7.0, please open a new issue if you find anything that doesn't work as expected.

MFlisar commented 6 years ago

This means I can now initialise List and similar as well in the file which was not possible before.

Following is not working though:

List<String> defHiddenInfos = new ArrayList<>();
@Default(ofStatement = "defHiddenInfos")
List<String> hiddenInfos();
boolean hiddenInfos(List<String> hiddenInfos);

Nor is following working:

@Default(ofStatement = "new ArrayList<>()")
List<String> hiddenInfos();
boolean hiddenInfos(List<String> hiddenInfos);

Here the problem is, that I need to set a serialised default value, which I can't as I can't create a constant serialised value (only by running the serialiser, logging the serialised result and copying it as a const string). Solution would be to in case of a serialised field to use null for the default string value and set the default statement as the default deserialised value:

@Override
public List<String> hiddenInfos() {
List<String> __result = (List<String>) cache.get("hiddenInfos");
if (__result == null) {
  Serializer __serializer = Esperandro.getSerializer();
  // Change this to: String __prefValue = preferences.getString("hiddenInfos", null);
  String __prefValue = preferences.getString("hiddenInfos", new ArrayList<String>());
  // Change this to: java.util.List<java.lang.String> __value = new ArrayList<String>();
  java.util.List<java.lang.String> __value = null;
  HiddenInfos __container = __serializer.deserialize(__prefValue, HiddenInfos.class);
  if (__container != null) {
    __value = __container.value;
  }
  __result = __value;
  if (__result != null) {
    cache.put("hiddenInfos", __result);
  }
}
return __result;
}