|
| 1 | +# PreferenceHelper |
| 2 | +A more painless way to set/get with SharedPreferences. |
| 3 | +# Getting Started |
| 4 | +Add it in your root build.gradle at the end of repositories: |
| 5 | +```groovy |
| 6 | +allprojects { |
| 7 | + repositories { |
| 8 | + maven { url 'https://jitpack.io' } |
| 9 | + } |
| 10 | +} |
| 11 | +``` |
| 12 | +Add the following to the dependencies section of app/build.gradle (check Releases for the latest version): |
| 13 | +``` |
| 14 | +implementation 'com.github.simplymadeapps:PreferenceHelper:1.1.1' |
| 15 | +``` |
| 16 | + |
| 17 | +# Usage |
| 18 | +Init the library in the `Application.onCreate()` method. You can also put this at that top of your start activity's `onCreate()`. All that matters is `init()` is called before any other calls to the PreferenceHelper. |
| 19 | +```java |
| 20 | +public class MyApplication extends Application { |
| 21 | + @Override |
| 22 | + public void onCreate() { |
| 23 | + super.onCreate(); |
| 24 | + PreferenceHelper.init(this); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | +Now that the library has been initialized, you can easily use `put()` and `get()`. |
| 29 | + |
| 30 | +Storing an object: |
| 31 | +``` |
| 32 | +PreferenceHelper.put("current_user_name", "John Doe"); |
| 33 | +``` |
| 34 | + |
| 35 | +Getting an object: |
| 36 | +``` |
| 37 | +String username = PreferenceHelper.get("current_user_name", "No Name"); |
| 38 | +``` |
| 39 | + |
| 40 | +The library is using the object type of the input or fallback to determine what SharedPreference method to use (ex, putString, putLong, putInt, etc). |
| 41 | +This can cause confusion when you pass in a null object as one of these parameters. If you need to store a null object or retrieve an object with a null fallback you should pass in that object type. |
| 42 | + |
| 43 | +Storing a null string: |
| 44 | +``` |
| 45 | +PreferenceHelper.put("current_user_name", null, String.class); |
| 46 | +``` |
| 47 | + |
| 48 | +Getting a set with a null fallback: |
| 49 | +``` |
| 50 | +Set<String> usernames = PreferenceHelper.get("all_names", null, Set.class); |
| 51 | +``` |
| 52 | +Note that primitive types (int, boolean, float, long) cannot be null. |
| 53 | + |
| 54 | + |
| 55 | +You can also check if a value has been stored with: |
| 56 | +``` |
| 57 | +boolean key_exists = PreferenceHelper.contains("some_key"); |
| 58 | +``` |
| 59 | + |
| 60 | +The library can only store what the default SharedPreferences stores (String, int, boolean, float, long, Set<String>). It currently does not support storing of a custom objects. This may be expanded upon in the future. |
| 61 | + |
| 62 | +# Contributing |
| 63 | +1. Fork it |
| 64 | +2. Create your feature branch (`git checkout -b my-new-feature`) |
| 65 | +3. Commit your changes (`git commit -am 'Add some feature'`) |
| 66 | +4. Push to the branch (`git push origin my-new-feature`) |
| 67 | +5. Create new Pull Request |
| 68 | + |
| 69 | +# License |
| 70 | +The library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
0 commit comments