Skip to content

[DEPRECATED] Annotation processing wrapper around RxPreferences

Notifications You must be signed in to change notification settings

IVIanuu/auto-rx-preferences

Repository files navigation

AutoRxPreferences

Annotation processing for RxPreferences

Introduction

RxPreferences by f2prateek is great library but it has a few downsides

  1. You have to mess around with the pref keys instead of access your preferences by name which feels more natural
  2. If you want to use your own default values you have to add them to every method you write
  3. Saving custom objects is to complicated

AutoRxPreferences solves those issues by using annotation processing.

Download

// in your root gradle
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
// in your module
dependencies {
	 compile 'com.github.IVIanuu.AutoRxPreferences:autorxpreferences:LATEST-VERSION'
         annotationProcessor 'com.github.IVIanuu.AutoRxPreferences:autorxpreferences-processor:LATEST-VERSION'
}

Usage

  1. Create a class and annotate it with the @Preferences annotation.
  2. Create some fields and annotate them with the @Key annotation.

The class should look like this

@Preferences
class MyPreferences {
    @Key String accessToken;
    @Key UserData userData;
    @Key Boolean loggedIn = false; // this will be the default value
}

This is the class which will be generated by AutoRxPreferences

public final class MyPreferences_ extends MyPreferences {
  private final SharedPreferences sharedPreferences;

  private final RxSharedPreferences rxSharedPreferences;

  private final Converter<UserData> userDataConverter;

  private MyPreferences_(Context context, Gson gson) {
    this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    this.rxSharedPreferences = RxSharedPreferences.create(sharedPreferences);
    this.userDataConverter = new UserDataConverter(gson);
  }

  public static MyPreferences_ create(@NonNull Context context) {
    return create(context, new Gson());
  }

  public static MyPreferences_ create(@NonNull Context context, @NonNull Gson gson) {
    return new MyPreferences_(context, gson);
  }

  public void clear() {
    sharedPreferences.edit().clear().apply();
  }

  @NonNull
  public Preference<String> getAccessToken() {
    if (accessToken != null) {
      return getAccessToken(accessToken);
    } else {
      return rxSharedPreferences.getString("access_token");
    }
  }

  @NonNull
  public Preference<String> getAccessToken(@NonNull String defaultValue) {
    return rxSharedPreferences.getString("access_token", defaultValue);
  }

  @NonNull
  public Preference<UserData> getUserData() {
    if(userData == null) {
      throw new IllegalStateException("userData has no default value");
    }
    return getUserData(userData);
  }

  @NonNull
  public Preference<UserData> getUserData(@NonNull UserData defaultValue) {
    return getUserData(defaultValue, userDataConverter);
  }

  @NonNull
  public Preference<UserData> getUserData(@NonNull UserData defaultValue,
      @NonNull Converter<UserData> converter) {
    return rxSharedPreferences.getObject("user_data", defaultValue, converter);
  }

  @NonNull
  public Preference<Boolean> getLoggedIn() {
    if (loggedIn != null) {
      return getLoggedIn(loggedIn);
    } else {
      return rxSharedPreferences.getBoolean("logged_in");
    }
  }

  @NonNull
  public Preference<Boolean> getLoggedIn(@NonNull Boolean defaultValue) {
    return rxSharedPreferences.getBoolean("logged_in", defaultValue);
  }

  private static final class UserDataConverter implements Converter<UserData> {
    private final Gson gson;

    private UserDataConverter(Gson gson) {
      this.gson = gson;
    }

    @NonNull
    @Override
    public UserData deserialize(String serialized) {
      return gson.fromJson(serialized, UserData.class);
    }

    @NonNull
    @Override
    public String serialize(UserData value) {
      return gson.toJson(value);
    }
  }
}

This saves you tones of boilerplate code. Let's see what you got for free here

  1. You can access your preferences by name not by key
  2. Default values will be applied to every call if they are not null (You can do it anyway if you want)
  3. A converter powered by gson is automatically created for you (You can pass your own Gson instance in the create method)

Annotations

@Preferences

Creates a auto preference class

parameter description default vale
preferenceName SharedPreferences name empty (Uses default preferences)
classNameSuffix The suffix at the of the generated class _
expose whether to make generated classes public or not true

@key

Declare annotated variable as SharedPreferences key.

parameter description default value
name preference's key name empty (variable name is converted to lower_snake_case and used as key)

Credits

yshrsmz: For the idea https://github.com/yshrsmz/simple-preferences

f2prateek: For the RxPreferences library https://github.com/f2prateek

License

Copyright 2017 Manuel Wrage

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

[DEPRECATED] Annotation processing wrapper around RxPreferences

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages