Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

SharedPreferences should allow a default value specified from a resource #536

Description

Currently, the @DefaultX annotations support fixed values, however it is a common use case where this default may vary based on language/other resource qualifiers. The @DefaultX annotations should have an argument valueRes that takes a resource of the appropriate type.

Activity

  1. DayS commented on May 4, 2013

    @DayS
    Contributor

    I think this is a good idea and it should be quit simple to implement.

    The following example is an improvement of the wiki example :

    @SharedPref
    public interface MyPrefs {
        @DefaultString(R.string.defaultPrefName)
        String resourceName();
    
        @DefaultString("John")
        String name();
    }

    The generated code should be :

    public final class MyPrefs_ extends SharedPreferencesHelper {
        private Context context;
    
        public MyPrefs_(Context context) {
            super(context.getSharedPreferences((getLocalClassName(context)+"_MyPrefs"), 0));
            this.context = context;
        }
    
        public StringPrefField resourceName() {
            return stringField("resourceName", context.getResources().getString(R.string.app_name));
        }
    
        public StringPrefField name() {
            return stringField("name", "John");
        }
    }
  2. DayS commented on May 4, 2013

    @DayS
    Contributor

    The new format of the annotations should be something like that :

    public @interface DefaultString {
        String value() default "";
        String resName() default "";
        int resValue() default ResId.DEFAULT_VALUE;
    }
  3. DayS commented on May 5, 2013

    @DayS
    Contributor

    Ok. I just looked further into the code dealing with Android's resources injection and everything is tied up to the value and resName paired attributes. So, we can't just modify the existing annotations without breaking the API.
    We have to create new a set of annotations @DefaultResString, @DefaultResInteger, etc.. And the implementation should be like this :

    public @interface DefaultResString {
        String value() default ResId.DEFAULT_VALUE;
        String resName() default "";
    }
  4. yDelouis commented on Jun 19, 2013

    @yDelouis
    Contributor

    This should be documented and closed.

  5. DayS commented on Jun 20, 2013

    @DayS
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions