Toggle


Source link: https://github.com/s0h4m/toggle

##A simple android library to add Feature-Toggle capability in your app

###Philosophy

A feature toggle, is a technique in software development that helps alter the state of a feature in an application dynamically. For more information, check out this link.

###Example So say, you'd like to disable features like video (or analytics) for a certain group of users (based on api levels and devices) and enable them for the rest, you would need to do the following

####Step 1: Create a configuration file in the following json format and host it somewhere (on your server, S3, etc.)

{
"name": "myapp", "features":  [

{
"name":"video", "default": "enabled", "rules":[

 {
"state": "disabled", "value":

{
"apilevel_min": 21, "apilevel_max": 23, "device":[{
"manufacturer":"xiaomi","model":"mi3"
}
, {
"manufacturer":"samsung", "model":"s4"
}
]
}

}
,

  {
"state": "disabled", "value": {
"appversion_max": 13
}

}

]
}
,

{
"name":"crash_reporting", "rules":[

  {
"state": "disabled", "value":

 {
"appversion": 11, "buildtype": "false"
}

  
}

 ]

}
,

{
"name":"mixpanel","default": "enabled", "rules":[

 {
"state": "disabled", "value":

{
"device":[{
"model":"Google Nexus 5 - 5.1.0 - API 22 - 1080x1920"
}
, {
"manufacturer":"samsung", "model":"s4"
}
]
}

}

]
}
  ] 
}

This configuration file allows you to:

  • disable the 'video' feature when all of the following get satisfied:
    • api level between 21 and 23
    • the device type is either a xiaomi mi3 or a samsung s4
  • disable the 'video' feature if the app version is less than 13
  • enable the 'video' feature otherwise (default: enabled)
  • disable the 'mixpanel' feature if the device is a google nexus 5 or a samsung s4
  • enable the 'mixpanel' feature otherwise (default: enabled)

You can find more information on the configuration parameters and how they work in the wiki here.

####Step 2: Download a new configuration You can download a new configuration in any of the following ways:

  • By passing a url
Toggle.with(context).setConfig(myUrl);
  • Downloading the config manually and then pass the same to Toggle as a String (in the Config JSON format)
String configInJson = ... // my custom code for downloading the config from my server and retrieving it as a json Toggle.with(context).setConfig(configInJson);
  • Downloading the config manually and then pass the same to Toggle as a Config object
Config config = ... // my custom call (say Retrofit for example) for retrieving the Config object from my server Toggle.with(context).setConfig(config);

Once setConfig is called (in any form) the config is then cached locally, so you can always check for a feature later

####Step 3: Check for the state of a feature at any time (even if you are offline) You can check for a feature using the check method

Toggle.with(context).check("custom_network_component").defaultState(Toggle.ENABLED).start(new cc.soham.toggle.callbacks.Callback() {

 @Override

 public void onStatusChecked(CheckResponse checkResponse) {

  updateUiAfterResponse(checkResponse.featureName, checkResponse.state, checkResponse.featureMetaData, checkResponse.ruleMetadata, checkResponse.cached);

 
}

}
);

In case you used a URL in setConfig, you can also use the getLatest flag to get the latest config before making the callback

Toggle.with(context).check("custom_network_component").getLatest().defaultState(Toggle.ENABLED).start(new cc.soham.toggle.callbacks.Callback() {

 @Override

 public void onStatusChecked(CheckResponse checkResponse) {

  updateUiAfterResponse(checkResponse.featureName, checkResponse.state, checkResponse.featureMetaData, checkResponse.ruleMetadata, checkResponse.cached);

 
}

}
);

The state of the feature can be found in checkResponse.state along with other things present in the config like metadata

###Optional parameters when using check

  • onError(ErrorCallback errorCallback): provide an optional callback in case any error is encountered in checking the state of the feature
  • getLatest(): checks for the latest Config before returning the state (only valid for URL powered configs)
  • defaultState(String defaultState): sets the default state of this request

For more information do check the implementation of CheckRequest.Builder

###Other resources You can find out more about Toggle in our wiki.

###Download

Get this via Gradle:

compile 'cc.soham:toggle:0.1'

or Maven:

<dependency>
<groupId>cc.soham</groupId>
<artifactId>toggle</artifactId>
<version>0.1</version> </dependency>

###ProGuard

If you are using ProGuard you might need to add the following option:

-dontwarn cc.soham.toggle.** 

###License

Copyright 2016 Soham Mondal  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.

###What's next These are the things that are coming up in Toggle

  • custom rule-matchers/checks (beyond the usual api-level/app-version/date etc.) that you can define
  • faster lookups in check (use hashmaps to look up the states)
  • more tests
  • multiple configs

Resources

CatanArchitecture is a sample repository to illustrate Hexagonal architecture in Android.

Nice AssertJ assertions for RxJava.

A Kotlin lightweight library to easily draw shapes that may contain text within them.

A simple library based on PopupWindow for creating Tooltips on Android.

An Android task queue library. Support priority, timeout, multiple queue and auto-retry.

Save time & clear your unit tests on Android!

Use annotations to inject Context, Activities, Fragments and Views into your tests.

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