TinyMachine


Source link: https://github.com/beworker/tinymachine

TinyMachine is fun to use finite-state machine. It helps you to write compact and clearly structured code when used in event-driver programming. No deep if-else or switch-case statements, no static state transition tables, no complex, hard to understand models. Just clearly structured state handling and transition logic. This is what TinyMachine is.

TinyMachine is

  • extra small (~ 5K jar)
  • easy to use
  • well tested
  • annotation based (no requiremens on method names, no interfaces to implement)

TinyMachine API in a nutshell

  • @StateHandler(state=STATE_A, type=Type.OnEntry) annotates handler methods receiving OnEntry event in STATE_A.
  • @StateHandler(state=STATE_A, type=Type.OnExit) annotates handler methods receiving OnExit event in STATE_A.
  • @StateHandler(state=STATE_A, type=Type.OnEvent) annotates handler methods receiving custom events in STATE_A.
  • TinyMachine.fireEvent(Object event) forwards given event to the corresponding handler method.
  • TinyMachine.transitionTo(int state) transtions the state machine into a new state.
  • TinyMachine.getCurrentState() returns current machine's state
  • TinyMachine.setTraceTag(String tag) enables or disables state change and event handling traces in LogCat using given tag.

Usage example

public class Example {

// Define states
  private static final int STATE_A = 0;
  private static final int STATE_B = 1;

// Define state handler class with handler methods
  public static class TinyHandler {

  // Handlers for ANY_STATE

@StateHandler(state = StateHandler.STATE_ANY, type = Type.OnEntry)

public void onEntryStateA() {

 // This method is called when machine enters any new state

}

 // Handlers for STATE_A

 @StateHandler(state = STATE_A, type = Type.OnEntry)

public void onEntryStateA() {

 // This method is called when machine enters STATE_A

}

 @StateHandler(state = STATE_A, type = Type.OnExit)

public void onExitStateA() {

 // This method is called when machine exits STATE_A

}

 @StateHandler(state = STATE_A)

public void onEventStateA(String event, TinyMachine tm) {

 // It's called when an event of type String is fired while machine is in STATE_A

 // As an example, let's transition into STATE_B when event "DONE" is received

 if ("DONE".equals(event)) {

tm.transitionTo(STATE_B);

// Since now all further events will be received by STATE_B handlers

 
}

}

 // Handlers for STATE_B

 @StateHandler(state = STATE_B, type = Type.OnEntry)

public void onEntryStateB() {

 // This method is called when machine enters STATE_B

}

 // and so on
  
}

 private TinyMachine mTinyMachine;

 public Example() {

// Create state machine with TinyHandler and put it into initial STATE_A state

mTinyMachine = new TinyMachine(new TinyHandler(), STATE_A);

  
}

// Now, when we receive events we just need to forward them to TinyMachine instance.
  // The machine is responsible to deliver them to the right handler depending on event
  // type and the current state.

 @Subscribe
  public void onEvent(String event) {

// E.g. forward this event coming from TinyBus to the state machine for processing.

mTinyMachine.fireEvent(event);

  
}

 // etc ... We can forward more events in the same way. 
}

Gradle dependencies

dependencies {

  compile 'de.halfbit:tinymachine:1.1.+' 
}
 

ProGuard configuration

If you use Gradle build, then you don't need to configure anything, because it will use proper configuration already delivered with Android library archive. Otherwise you can use the configuration below:

-keepclassmembers, allowobfuscation class ** {

  @de.halfbit.tinymachine.StateHandler public *; 
}
 

Used in

License

Copyright (c) 2015 Sergej Shafarenka, halfbit.de  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

DroidBot is a lightweight test input generator for Android.

It can send random or scripted input events to an Android app, achieve higher test coverage more quickly, and generate a UI transition graph (UTG) after testing.

A wonderful library to display 2D fancy carousels for Android.

A library of classes to create every kind of gauges.

A library used to make good looking curved SeekBar.

SelfAdjointView is an Android library that allows developers to easily let view change with the parent which can scroll. The animation contains one or more of parallax, alpha, scale.

Apply to

  • ScrollView/HorizontalScrollView
  • ListView/GridView
  • RecyclerView
  • ViewPager
  • sth else that can scroll

A simple to use networking library that makes it easy to retrieve data from REST APIs. Also provides a mock function to build/test your app without actually making the http request.

Features:

  • Supports GET/POST/PUT/DELETE (for now)
  • Supports file downloads and uploads (with progress callbacks)
  • Supports http post data (json, multipart, url-encoded, plain text, xml)
  • Simple OAuth logins using password resource flow
  • Queued requests
  • Global and per request mocking
  • Built in deserialization for JSON (using gson)
  • Automatically handles redirects

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