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.

SharedPreferencesHelpers, specify key for preference #826

Description

@WonderCsabo

If you declare a preference helper like this:

@SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT)
public interface Preferences {

    @DefaultInt(0)
    int searchChoiceDisplayMode();

}

The generated helper class will use the method name (searchChoiceDisplayMode) in this case as the key of the preference.
But if the application already uses a PreferenceActivity, these keys are declared as IDs and are in the R.id class. If the @DefaultXX annotations would accept a second optional argument which defines the pref key, we could safely use the IDs and we did not have to change the method name if the ID changes.

Activity

  1. WonderCsabo commented on Jan 4, 2014

    @WonderCsabo
    MemberAuthor

    I'm writing down the details about the feature i proposed.

    I would append an int resKey () method to the @DefaultXXX annotations, but at least for @DefaultString:

    public @interface DefaultString {
        String value() default ResId.DEFAULT_VALUE;
        int resKey () default "";
    }

    Let's suppose there is a string resource named prefKey and the client Preferences interface is the following:

    @SharedPref
    public interface Preferences {
    
        @DefaultString(value = "0", resKey = R.string.prefKey)
        String myPref();
    }

    The generated Preferences_ class would be something like this:

    public final class Preferences_
        extends SharedPreferencesHelper
    {
    
        private Context context_;
    
        public Preferences_(Context context) {
            super(PreferenceManager.getDefaultSharedPreferences(context));
            this.context_ = context;
        }
    
        public Preferences_.PreferencesEditor_ edit() {
            return new Preferences_.PreferencesEditor_(getSharedPreferences());
        }
    
        public StringPrefField myPref() {
            return stringField(context.getString(R.string.prefKey), "0");
        }
    
        public final static class PreferencesEditor_
            extends EditorHelper<Preferences_.PreferencesEditor_>
        {
            PreferencesEditor_(SharedPreferences sharedPreferences) {
                super(sharedPreferences);
            }
    
            public StringPrefEditorField<Preferences_.PreferencesEditor_> myPref() {
                return stringField(context.getString(R.string.prefKey));
            }
        }
    }

    @DayS, what do you think?

  2. added this to the 3.1 milestone on Apr 6, 2014
  3. DayS commented on Apr 6, 2014

    @DayS
    Contributor

    Merged :)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions