Treasure
Treasure
???Android????? SharePreferences
?????????????????????????? Serializable
? Parcelable
????0????????????????????????
????
1?????
Gradle
compile 'com.baoyz.treasure:treasure:0.7.4' annotationProcessor 'com.baoyz.treasure:treasure-compiler:0.7.4'
Gradle Plugin
???? Module ????? Treasure???? Multiple dex files define Lcom/baoyz/treasure/PreferencesFinder
????? Treasure Gradle Plugin ?????????
? buildscript
?????
dependencies {
classpath 'com.baoyz.treasure:treasure-gradle:0.7.4'
}
?????? Application Module ? apply ???Library Module ????
apply plugin: 'com.baoyz.treasure'
????? Module ???? Treasure ??????????????
2?????
@Preferences public interface SimplePreferences {
String getUsername();
void setUsername(String username);
}
??????? interface
????? @Preferences
????????????????? get
? set
????????????????????? key
??? getUsername()
? setUsername()
? key
?? username
?????? setUsername()
??? value
???? getUsername()
????????? key
?????
3????
SimplePreferences preferences = Treasure.get(context, SimplePreferences.class);
preferences.setUsername("Hello Treasure!");
preferences.getUsername();
// return "Hello Treasure!"
?? Treasure.get()
????????? Preferences
????????? set
??????????? get
??????
????
???
????? Preferences
?????????????????????? Preferences
?
Treasure.get(context, SimplePreferences.class, "id_one");
Treasure.get(context, SimplePreferences.class, "id_two");
Treasure
???????? get
????????? String
???ID???ID??? Preferences
???????????????????? zzz40500??????
???
@Default
?????? get
???????
@Default("Hello Treasure!") String getUsername();
@Default("false") boolean isLogin();
// default is 1 hour @Default("1000 * 60 * 60") long getTimeout();
@Default({
"hello", "world", "!"
}
) Set<String> getStringSet();
?????? @Default
?????????
????? | ??? |
---|---|
int | 0 |
float | 0f |
long | 0l |
boolean | false |
String | null |
Set<String> | null |
????
??????????? edit().apply()
?????? edit().commit()
??????
1???
@Preferences(edit = Preferences.Edit.COMMIT) public interface SimplePreferences
?????????????? set
????? commit()
???????
2???????
@Commit void setUsername(String username);
?? @Commit
???????????
boolean
3??????? boolean setUsername(String username);
???????????????? set
??????? boolean
?????????? commit()
????????? commit()
????
????
@Remove void removeUsername();
@Remove void deleteTimeout();
?? @Remove
??????????????? key
????
????
@Clear void clear();
??????????? @Clear
??????????????????? Preferences
????
???
???? @Expired
???????????????????? set
???? @Expired
????? getter
? setter
?? ??????
// 10????? @Expired(value = 10, unit = Expired.UNIT_SECONDS) String getTestExpired();
@Expired
?????????????????????????? setter
?????????
// second?????????????UNIT_MINUTES???? void setTestExpired(String value, @Expired(unit = Expired.UNIT_MINUTES) int second);
@Expired
? unit
?????? UNIT_MILLISECONDS
???
?????
?? interface
?????????? SharePreferences
???????????? Treasure
???? Serializable
? Parcelable
????
// Serializable or Parcelable class User implements Serializable {
...
}
// Preferences Interface void setUser(User user);
User getUser();
????????????? Gson
???? JSON
??????
public class GsonConverterFactory implements Converter.Factory {
@Override
public <F> Converter<F, String> fromType(Type fromType) {
return new Converter<F, String>() {
@Override
public String convert(F value) {
return new Gson().toJson(value);
}
}
;
}
@Override
public <T> Converter<String, T> toType(final Type toType) {
return new Converter<String, T>() {
@Override
public T convert(String value) {
return new Gson().fromJson(value, toType);
}
}
;
}
}
?????????? Treasure.setConverterFactory()
?????????????
Treasure.setConverterFactory(new GsonConverterFactory());
?? SharedPreferences ??
@Prototype SharedPreferences getSharedPreferences();
?? @Prototype
??????? SharedPreferences
??????????????? SharedPreferences
???
??? Key
@Key("custom_key") String getValue();
@Key("custom_key") void setValue(String value);
?????
?????? get
? set
? put
? is
? remove
? delete
???????????????????? key
??????????????????????? key
?
Proguard
Treasure
???0???????? Proguard
???
??
Favor??????
License
The MIT License (MIT) Copyright (c) 2015 baoyongzhang <[email protected]> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.