SimpleConfig


Source link: https://github.com/BCsl/SimpleConfig

SimpleConfig

Remove boilerplate code for using SharePreference to persist app's config data, Inspire by retrofit

???????

Gradle

compile 'github.hellocsl:simpleconfig:{
lastest-version
}
' 

Usage

Step1 Define your config interface

Config Interface? Recommended

@CONFIG(name = "Simple") public interface TestConfig {

  String TEST_KEY_INT = "HELLO_INT";

@GET(key = TEST_KEY_INT)
  int getIndex(int def);

@GET(key = TEST_KEY_INT)
  int getIndex();

@APPLY(key = TEST_KEY_INT)
  void applyIndex(int value);

 @COMMIT(key = TEST_KEY_INT)
  boolean commitIndex(int value);
  
}

Other way to define your Config Interface? (Be careful of ProGuard)

//config name will be NoAnnotationConfig public interface NoAnnotationConfig {

  //get as prefix?HelloInt will be consider as the key?default value is def?return type is int
  int getHelloInt(int def);

//get as prefix?HelloInt will be consider as the key?default value is -1?return type is int
  int getHelloInt();

//apply as prefix?HelloInt will be consider as the key
  void applyHelloInt(int value);

//commit as prefix?HelloInt will be consider as the key value?return type is boolean which indicated the new values were successfully written to persistent storage.
  boolean commitHelloInt(int value);
 
}

Since this lib use SharePreference to save [key-value] at default, so only int|Integer? long|Long? float|Float? boolean|Boolean? String? Set<String> are support to

Step2 Use SimpleConfig to parse config interface

Use SimpleConfig.Builder to build a SimpleConfig object ?and then use SimpleConfig#create method to parse Config interface and it will return a dynamic proxy object witch implements the config interface ?now you can use this to update values to config or get values from config

SimpleConfig simpleConfig = new SimpleConfig.Builder(mAppContext).build();
 TestConfig testConfig = mSimpleConfig.create(TestConfig.class);
 //... testConfig.applyIndex(111);
 int result = testConfig.getIndex(100);
 //...

Further settings

How to define your config interface

Config interface contains two type of method? Method to get value ? Method to update value

  • Method to get value

    This type of method can be annotate by @Get or with a get as prefix of the method name.At most one argument allowed by these methods, and it will consider to be the default value , SimpleConfig use the method return type as the type of the value to retrieve

  • Method to update value

    This type of method can be annotate by @APPLY or @COMMIT,with apply or commit as prefix of the method name is also support.Only one argument allowed by these methods represent a new value for the config. Both boolean and void are support as the return type?but boolean type is meaningless when defines in apply method and alway true is returned

Here Annotations can use

Annotation Target Function Note
@CONFIG Config interface Specified the name of config If not set?Interface name will be consider as name of config
@GET Method to get value Specified the key be used to retrieve value from config If not set and the method start with "get"?such as "getHelloInt" ?"HelloInt" will be consider as the key
@APPLY Method to update value Specified the key be used to modify the config?it will update config asynchronously If not set and the method start with "apply"?such as "applyHelloInt" ?"HelloInt" will be consider as the key
@COMMIT Method to update value Specified the key be used to modify the config If not set and the method start with "commit"?such as "commitHelloInt" ?"HelloInt" will be consider as the key

Implement your Config

At Default , SimpleConfig use SharePreference to persist config value, PreferenceConfig?but you can implement your persistent systems by specified your Config.Factory when build a SimpleConfig

mSimpleConfig = new SimpleConfig.Builder(mAppContext).configFactory(new Config.Factory() {

@Override

public Config newConfig(Context context, String name, int mode) {

  // return your persistent systems

 return null;

}
  
}
).build();

DEMO

Unit Test is provided. TestConfigTest & NoAnnotationConfigTest

ProGuard

Since ProGuard may confuse your Config Interface , so define your Config Interface with annotations is recommended

License

The MIT License (MIT)  Copyright 2017 BCsl  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. 

Resources

jus

Jus is a flexible and easy HTTP/REST client library for Java and Android.

Animating Google emojis with Android animated vector drawables.

droitatedDB is a lightweight framework, which frees you from the burden of dealing with the Android SQLite database directly if you don't want to but lets you access it directly if you need to.

With an annotation based approach you are able to get a database up and running in no time. Simply annotate your data classes and let droitatedDB do the work.

Force clear delaying & no longer needed Gradle tasks.

A more elegant and easy way to build an multifunctional adapter for ListView or RecyclerView in Android.

An imitation of the clock in Google I/O 2016. Almost all of the animations and images in it are made by the SVG, so this library may be a wonderful material to learn the SVG's usage in Android.

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