ShPref


Source link: https://github.com/Pulimet/ShPref-Library

ShPref-Library

Simplifies the interaction with Shared Preferences on Android. (Bonus: Logger)

Installation

  • Add the dependency from jCenter to your app's (not project) build.gradle file:
repositories {

  jcenter() 
}
  dependencies {

  compile 'net.alexandroid.utils:shpref:1.6' 
}
  • Add init line in your application class as shown below:
public class MyApplication extends Application {

  @Override
  public void onCreate() {

super.onCreate();

ShPref.init(this, ShPref.APPLY);
  
}
 
}
  • Also don't forget to add: android:name=".MyApplication" at your application tag in AndroidManifest.xml
 <application

android:name=".MyApplication"

...>

Release notes

  • 1.6 - Logger separated ( https://github.com/Pulimet/MyLogLibrary), appcompat dependency removed
  • 1.5 - Logger methods for visual customization
  • 1.4 - Added remove method to Editor
  • 1.3 - Tests and bug fixes
  • 1.2 - Save doubles support. Put null as a value bugfix. Logs are removed. Added contains and clear methods. New bonus Contextor and Logger classes. Chainning by using Editor. Ability to get values without default value.
  • 1.1 - JavaDoc, readme file, bug fixes

How to use it

On initialization (at yours application class) you should pass default saving mode as a second argument. As you know, there are two options to save: APPLY (faster and asynchronous) or COMMIT.

With ShPref library you can use following types: String, int, boolean, float, double, long and lists with that types.

  • Put:

// Save using a resource as a key ShPref.put(R.string.some_key, "Sample text");  // Save using a hard coded string ShPref.put("HardCodedKey", 25);  // Option to chain using Editor class new ShPref.Editor()
  .put("key1", "key1value")
  .put("key2", "key2value")
  .put("key3", "key3value")
  .commit();
  // Saving a List ArrayList<Integer> list = new ArrayList<>();
 for (int i = 0; i < 10; i++) {

  list.add(i); 
}
 ShPref.putList("myListKey", list);
 



  • Get:

// Get a value and if it not exist get a default value that was passed as a second argument String myString = ShPref.getString(R.string.some_key, "Default value"); int myInt= ShPref.getInt("HardCodedKey", 0); // 0 is default value  // Get values without to set default values (if not exist: getString return null, getBoolena return false and other 0) String myString = ShPref.getString(R.string.some_key); int myInt= ShPref.getInt("HardCodedKey");
// Get value as an object Object obj = ShPref.get(R.string_some_key); //excluding double  // Get a list  List<Integer> list = ShPref.getListOfIntegers("myListKey");



  • Contains:

// Checking for existence boolean isContains = ShPref.contains(R.string.key_key);



  • Remove:

ShPref.remove(R.string.key_key); ShPref.remove("HardCodedKey");



  • Clear:

// Remove all values from the preferences. ShPref.clear();



  • Mode forcing methods:
    For example if you have choosen as default APPLY mode and you need to save or remove something using COMMIT there are special methods for that case:
putC();
 removeC();
 // Force commit putA();
 removeA();
 // Force apply




Bonus

Option to get application Context from anywhere:

Context appContext = Contextor.getInstance().getContext();




License

Copyright 2016 Alexey Korolev  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. 

Resources

RxObservableDiskCache is a library to save the results of Singles or single value Observables request on a local disk cache, so the next time the same request is called you get an immediate result.

Diagonalify is the library to create diagonal cut view on imageviews

A quick settings tile to quickly set the animator duration scale.

Now you can preserve object instance without any hassle during orientation change.

Set of Proguard rules that need to be configured for some frequently used libraries.

A less buggier and customizable way to handle URLs in TextViews.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes