SharP


Source link: https://github.com/a11n/sharp

#SharP

SharP wraps your Shared Preferences into a clean, type-safe Java interface. It uses annotation processing to generate the boilerplate code for you.

Just declare your interface and annotate it to be a @SharedPreference or a @DefaultSharedPreference:

@SharedPreference interface LocalStorage{

String getMyStringPreference();

void setMyStringPreference(String value);

int getMyIntPreference();

void setMyIntPreference(int value);
 
}

...then instantiate its auto-generated implementation using SharP:

LocalStorage storage = SharP.getInstance(context, LocalStorage.class);
  String myStringPreference = storage.getMyStringPreference();
 int myIntPreference = storage.getMyIntPreference();
  storage.setMyStringPreference("FooBar");
 storage.setMyIntPreference(42);

That's it. No struggling with keys anymore.

Please refer to the Javadoc or the sample application for more information.

##Usage SharP is available via jcenter(). The android-apt plugin is used to setup SharP as annotation processor in Android Studio.

buildscript {

repositories {

  jcenter()

}

dependencies {

  classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'

}
 
}
  apply plugin: 'com.neenbedankt.android-apt'  dependencies {

compile 'de.ad:sharp-api:0.3.0'
apt 'de.ad:sharp-processor:0.3.0' 
}

##Principles In order to be lightweight and convenient SharP is designed according to the Convention over Configuration paradigm.

###Conventions ####Interface

  • @DefaultSharedPreference:
  • The default SharedPreferences are used calling PreferencesManager.getDefaultSharedPreferences(Context context)
  • Use this if you want to access the stored preferences from PreferenceFragment
  • @SharedPreference:
  • Preferences are stored in their own file in MODE_PRIVATE
  • The fully qualified interface name is used as unique filename
  • Only top-level interfaces are supported

####Properties

  • JavaBean naming conventions are applied
  • Getters are required to:
  • start with get and
  • have no parameters
  • Example: String getMyStringPreference();
  • Boolean getters are required to:
  • start with is
  • have no parameters and
  • must return boolean
  • Example: boolean isMyBooleanPreference();
  • Setters are required:
  • to start with set
  • have exactly one parameter
  • must return void
  • Example: void setMyStringPreference(String value);
  • Each getters needs a corresponding setter
  • Example: int getMyIntPreference() requires declaration of void setMyIntPreference(int value)

####Supported types

  • SharP supports all types, but there is a twofold distinction:
  • Native types ( int, long, float, boolean and String)
  • Custom types (any non native type)
  • While native types are natively supported by SharedPreferences, custom types will be serialized/deserialized and treated as String

####Default values

####Specialities

  • The declaration of void reset() in your interface will result in a special implementation which calls editor.clear().apply()

Resources

Want to assert that class has private constructor? Let's do it!

An extension for Google's AutoValue that creates a simple Moshi JsonAdapterFactory for each AutoValue annotated object.

An extension for Google's AutoValue that supports Android's Parcelable interface.

An extension for Google's AutoValue that creates a simple Gson TypeAdapterFactory for each AutoValue annotated object.

Based on the Android support library's ViewPager class, this ViewPager supports both horizontal and vertical paging views.

A gradle plugin that facilitates using a git repository as your maven repo, useful when you want to host private, internal maven artifacts, but a full blown repository manager is more than you nee.

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