Easy to use SharedPreferences
Use Annotations and Compile-time Processing to generate the code , so you can save value just with methods.
| Module | easypreferences | easypreferences-compiler |
|---|---|---|
| Latest Version | |
|
depend via Maven:
<dependency>
<groupId>com.kkdandan</groupId>
<artifactId>easypreferences</artifactId>
<version>*.*.*</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.kkdandan</groupId>
<artifactId>easypreferences-compiler</artifactId>
<version>*.*.*</version>
<type>pom</type>
</dependency>
or Gradle
compile 'com.kkdandan:easypreferences:*.*.*'
compile 'com.kkdandan:easypreferences-compiler:*.*.*'
There are eight annotations in the easy preferences-annotations artifact:
@Preferences - This annotation can be used on the interface which define methods to set , get ,remove or clear value. You can set the value to define the name of SharedPreferences, if not will use the name of interface.
@Key - This annotation can be used on the methods of setter, getter and remove to define the value of key.
@Default - This annotation can be used on the methods of getter and its value is the default value of getter.
@All - This annotation can be used on the method to get the all values of SharedPreferences. The return type of method must be Map<String,?> to match the result of origin method.
@Apply - The method of setter uses 'commit()' api to submit the value. This annotation is used to switch to 'apply()' api.
@Clear - This annotation can be used on the method which clears all values.
@Remove - This annotation can be used on the method which remove a key.
@Converter - This annotation can be used on the method which to save custom class. The parameter must implement the interface of IConvert.
There is only one interface IConvert that support methods to transform between class and string so We can save all types of object. For example , we can define a class to transform json.
public class UserConverter implements IConvert<User> {
@Override
public String convertToString(User value) {
return new Gson().toJson(value);
}
@Override
public User convertFromString(String value) {
return new Gson().fromJson(value, User.class);
}
}then we can use this on setter method of User
@Preferences
public interface SimplePreferences {
@Converter(UserConverter.class)
void setUser(User user);
@Converter(UserConverter.class)
User getUser();
}Define an interface , and then Use class EasyPreferences to get the instance of interface.
@Preferences
public interface SimplePreferences {
void setLong(long value);
Long getLong();
void setFloat(Float value);
float getFloat();
@Apply
void setSetString(Set<String> stringSet);
Set<String> getSetString();
@Default("hello")
String getWelcome();
@Default("0")
int getDafaultInt();
@Default({"hh", "gg"})
Set<String> getDefaultSetString();
@Converter(UserConverter.class)
void setUser(User user);
@Converter(UserConverter.class)
User getUser();
@Remove("string")
void removeString();
@Clear
void clear();
@All
Map<String,?> getAll();
}public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimplePreferences simplePreferences = EasyPreferences.getSharedPreferences(this, SimplePreferences.class);
simplePreferences.setInt(120);
simplePreferences.removeString();
User user = simplePreferences.getUser();
Map<String,?> allValues = simplePreferences.getAll();
simplePreferences.clear();
}
}- get and set types of int long boolean float
- check the setter and getter's type
- support String
- remove key
- clear all keys
- define the name of SharedPreferences
- support setting default value
- support using method of Apply to submit
- support type of Set
- support all types
- support compile
- support boolean when commit the value
- support customize key
- buffer instance of Preference
- multi-thread concurrency
- WeChat: helloworld12
- Email: rjjdick@gmail.com
Copyright (C) 2018 King
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.