├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── StateMachine.iml ├── build.gradle ├── dev-tools └── statemachine.keystore ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── statemachine-sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── src │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── polidea │ │ │ └── statemachine │ │ │ └── ApplicationTest.java │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── polidea │ │ │ └── statemachine │ │ │ └── sample │ │ │ ├── Application.java │ │ │ ├── LoginActivity.java │ │ │ ├── LoginStateableHandler.java │ │ │ ├── di │ │ │ ├── ApplicationComponent.java │ │ │ └── MainModule.java │ │ │ ├── events │ │ │ ├── BackStackChangedEvent.java │ │ │ ├── LoginEvent.java │ │ │ └── StartLoginEvent.java │ │ │ ├── fragment │ │ │ ├── LoggedInFragment.java │ │ │ ├── LoginFragment.java │ │ │ └── NotLoggedInFragment.java │ │ │ ├── manager │ │ │ └── NetworkManager.java │ │ │ └── state │ │ │ ├── BaseLoginState.java │ │ │ ├── LoginActionInterface.java │ │ │ ├── LoginEvents.java │ │ │ ├── LoginInitialState.java │ │ │ ├── LoginProvider.java │ │ │ ├── OnGoingLoginState.java │ │ │ └── WaitingForLoginRequestState.java │ │ └── res │ │ ├── layout │ │ ├── activity_login.xml │ │ ├── fragment_logged_in.xml │ │ ├── fragment_login.xml │ │ └── fragment_not_logged_in.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml └── statemachine-sample.iml └── statemachine ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── src ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── polidea │ │ │ └── statemachine │ │ │ ├── State.java │ │ │ ├── StateChanger.java │ │ │ ├── StateEventHandlerInterface.java │ │ │ ├── StateMachine.java │ │ │ ├── StateProvider.java │ │ │ ├── Stateable.java │ │ │ ├── Transition.java │ │ │ ├── handler │ │ │ └── BaseStateableHandler.java │ │ │ ├── log │ │ │ ├── DefaultLogHandler.java │ │ │ ├── LogHandler.java │ │ │ └── Logger.java │ │ │ └── states │ │ │ └── InitialState.java │ └── res │ │ └── values │ │ └── strings.xml └── test │ └── groovy │ └── com │ └── polidea │ └── statemachine │ ├── StateChangerTest.groovy │ ├── StateMachineTest.groovy │ └── states │ ├── SampleFirstState.java │ └── SampleSecondState.java └── statemachine.iml /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | StateMachine -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 22 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Android 42 | 43 | 44 | Android Lint 45 | 46 | 47 | Java 48 | 49 | 50 | Java language level migration aidsJava 51 | 52 | 53 | 54 | 55 | Android 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 77 | 78 | 79 | 80 | 81 | Android API 22 Platform 82 | 83 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Polidea 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # State Machine for Android 2 | 3 | A lightweight state machine implementation for Android. 4 | 5 | ## Download 6 | 7 | Via gradle. In main build.gradle : 8 | 9 | ```gradle 10 | allprojects { 11 | repositories { 12 | ... 13 | maven { 14 | url "https://oss.sonatype.org/content/repositories/snapshots" 15 | } 16 | } 17 | } 18 | ``` 19 | 20 | Add library: 21 | 22 | ```gradle 23 | compile 'com.polidea:statemachine:1.0.0-SNAPSHOT' 24 | ``` 25 | 26 | or Maven. Add plugin repository: 27 | 28 | ```xml 29 | 30 | https://oss.sonatype.org/content/repositories/snapshots/ 31 | 32 | true 33 | 34 | 35 | ``` 36 | 37 | Add library: 38 | 39 | ```xml 40 | 41 | com.polidea 42 | statemachine 43 | 1.0.0-SNAPSHOT 44 | 45 | ``` 46 | 47 | **It is a stable version of library** 48 | 49 | ## Usage 50 | 51 | ### Creating custom BaseStateableHandler 52 | 53 | The easiest way to use StateMachine is to extend `BaseStateableHandler`. It have 4 main methods that we should override: 54 | 55 | * `getStateProvider()` - used by State's to get data from our Fragment/Activity. Base StateProvider contains one method `provideContext()` 56 | * `getActionInterface()` - used by State's to perform action's on Activity/Fragment, e.g. show Toast. 57 | * `getInitialStateClass()` - initial state class for `StateMachine`. By default it returns `InitialState` class. 58 | * `onStateMachineDescribe(StateMachine stateMachine)` - here we should describe transitions between states in our `StateMachine`. Each transition is a set of: from class, to class, event id. It means: when current state is 'from state' and it will propagate 'event id' then state machine should go to 'to state'. 59 | 60 | Sample `BaseStateableHandler`: 61 | 62 | ```java 63 | public class LoginStateableHandler extends BaseStateableHandler implements LoginProvider, LoginActionInterface { 64 | @Override 65 | public LoginProvider getStateProvider() { 66 | return this; 67 | } 68 | 69 | @Override 70 | public LoginActionInterface getActionInterface() { 71 | return this; 72 | } 73 | 74 | @Override 75 | public int provideFragmentContainerId() { 76 | return R.id.fragment_container; 77 | } 78 | 79 | @Override 80 | public void onStateMachineDescribe(StateMachine stateMachine) { 81 | stateMachine.addTransitionFromClass(LoginInitialState.class, LoginEvents.START_LOGIN, OnGoingLoginState.class); 82 | 83 | stateMachine.addTransitionFromClass(OnGoingLoginState.class, LoginEvents.CANCELLED, LoginInitialState.class); 84 | stateMachine.addTransitionFromClass(OnGoingLoginState.class, LoginEvents.SENDING_IN_PROGRESS, WaitingForLoginRequestState.class); 85 | 86 | stateMachine.addTransitionFromClass(WaitingForLoginRequestState.class, LoginEvents.FINISHED, InitialState.class); 87 | } 88 | 89 | @Override 90 | public Class getInitialStateClass() { 91 | return LoginInitialState.class; 92 | } 93 | 94 | ... 95 | } 96 | ``` 97 | 98 | As you can see here, state machine contains 3 states: `LoginInitialState`, `OnGoingLoginState` and `WaitingForLoginRequestState`. Initial state is `LoginInitialState`. Looking at one of transition: 99 | 100 | ```java 101 | stateMachine.addTransitionFromClass(LoginInitialState.class, LoginEvents.START_LOGIN, OnGoingLoginState.class); 102 | ``` 103 | 104 | means that, when state machine is in `LoginInitialState` and that state will fire `LoginEvents.START_LOGIN` event, then state machine should go to state `OnGoingLoginState`. 105 | 106 | ### Starting custom BaseStateableHandler 107 | 108 | To make `LoginStateableHandler` work, you must remember to call it's `onCreate(Bundle savedInstanceState)`, `onResume()`, `onPause()`, `onSaveInstanceState(Bundle outState)` methods in appropriate Fragment/Activity lifecycle method's. Sample usage: 109 | 110 | ```java 111 | @Override 112 | protected void onCreate(Bundle savedInstanceState) { 113 | super.onCreate(savedInstanceState); 114 | ... 115 | handler = new LoginStateableHandler(); 116 | handler.onCreate(savedInstanceState); 117 | } 118 | 119 | @Override 120 | protected void onResume() { 121 | super.onResume(); 122 | handler.onResume(); 123 | } 124 | 125 | @Override 126 | protected void onPause() { 127 | super.onPause(); 128 | handler.onPause(); 129 | } 130 | 131 | @Override 132 | protected void onSaveInstanceState(Bundle outState) { 133 | super.onSaveInstanceState(outState); 134 | handler.onSaveInstanceState(outState); 135 | } 136 | ``` 137 | 138 | Of course you can initialize handler from other places, not only Fragment/Activity. Here is an example of starting handler in singleton class like `Application`: 139 | 140 | ```java 141 | public class MyApplication extends Application { 142 | 143 | LoginStateableHandler handler; 144 | 145 | @Override 146 | public void onCreate() { 147 | super.onCreate(); 148 | 149 | handler = new LoginStateableHandler(); 150 | handler.onCreate(null); 151 | handler.onResume(); 152 | } 153 | } 154 | ``` 155 | 156 | ### Creating states 157 | 158 | State machine consist of states. Each state should extends `State` that contains two methods: 159 | 160 | * `onStateApplied()` - called when entering state 161 | * `onStateLeft()` - called when leaving state 162 | 163 | Each state should call `fireEvent(int eventId)` when it finish it's job. Sample `State`: 164 | 165 | ```java 166 | public class OnGoingLoginState extends State{ 167 | 168 | @Inject 169 | Bus bus; 170 | 171 | @Inject 172 | NetworkManager networkManager; 173 | 174 | public OnGoingLoginState() { 175 | Application.getComponentInstance().inject(this); 176 | } 177 | 178 | @Override 179 | public void onStateApplied() { 180 | bus.register(this); 181 | } 182 | 183 | @Override 184 | public void onStateLeft() { 185 | bus.unregister(this); 186 | } 187 | 188 | @Subscribe 189 | public void onBusLoginEvent(BusLoginEvent loginEvent) { 190 | networkManager.loginUser(loginEvent.getEmail(), loginEvent.getPassword()); 191 | fireEvent(LoginEvents.SENDING_IN_PROGRESS); 192 | } 193 | } 194 | ``` 195 | 196 | It is nice to use some Bus implementation, like [EventBus](https://github.com/greenrobot/EventBus) or [Otto](https://github.com/square/otto), to receive applications events.

197 | In that example we use [Otto](https://github.com/square/otto) for sending `BusLoginEvent` (it may be send by activity when 'Login' button tapped) and receiving that event in `OnGoingLoginState`. 198 | 199 | ## Used libraries 200 | 201 | * **[otto]** https://github.com/square/otto 202 | * **[dagger 2]** https://github.com/google/dagger 203 | * **[spock]** https://code.google.com/p/spock/ 204 | * **[android support library v7]** 205 | 206 | ## LICENSE 207 | 208 | [LICENSE](./LICENSE) -------------------------------------------------------------------------------- /StateMachine.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.2.3' 9 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' 10 | classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6' 11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /dev-tools/statemachine.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Polidea/state-machine-android/7ff0cf1eae7aa9d809bd31538d184037d550d551/dev-tools/statemachine.keystore -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | 20 | version=1.0.0-SNAPSHOT -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Polidea/state-machine-android/7ff0cf1eae7aa9d809bd31538d184037d550d551/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jul 03 10:14:31 CEST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':statemachine-sample', ':statemachine' 2 | -------------------------------------------------------------------------------- /statemachine-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /statemachine-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | 4 | android { 5 | compileSdkVersion 22 6 | buildToolsVersion "23.0.0 rc2" 7 | 8 | defaultConfig { 9 | applicationId "com.polidea.statemachine.sample" 10 | minSdkVersion 8 11 | targetSdkVersion 22 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | compile 'com.android.support:appcompat-v7:22.2.0' 26 | compile 'com.squareup:otto:1.3.8' 27 | compile 'com.google.dagger:dagger:2.0' 28 | compile 'javax.inject:javax.inject:1' 29 | provided 'javax.annotation:jsr250-api:1.0' 30 | apt 'com.google.dagger:dagger-compiler:2.0' 31 | compile project(':statemachine') 32 | } 33 | -------------------------------------------------------------------------------- /statemachine-sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/paweljaneczek/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /statemachine-sample/src/androidTest/java/com/polidea/statemachine/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | 11 | public ApplicationTest() { 12 | super(Application.class); 13 | } 14 | } -------------------------------------------------------------------------------- /statemachine-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/Application.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample; 2 | 3 | import com.polidea.statemachine.log.Logger; 4 | import com.polidea.statemachine.sample.di.ApplicationComponent; 5 | 6 | public class Application extends android.app.Application { 7 | 8 | private static Application instance; 9 | 10 | private ApplicationComponent component; 11 | 12 | @Override 13 | public void onCreate() { 14 | super.onCreate(); 15 | 16 | instance = this; 17 | 18 | Logger.setEnabled(true); 19 | 20 | component = ApplicationComponent.Initializer.init(this); 21 | } 22 | 23 | public static ApplicationComponent getComponentInstance() { 24 | return instance.component; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v4.app.FragmentTransaction; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.ViewGroup; 9 | import com.polidea.statemachine.sample.fragment.NotLoggedInFragment; 10 | 11 | public class LoginActivity extends AppCompatActivity { 12 | 13 | LoginStateableHandler handler; 14 | 15 | ViewGroup progressContainer; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_login); 21 | 22 | progressContainer = (ViewGroup) findViewById(R.id.progress_container); 23 | 24 | showFragment(new NotLoggedInFragment()); 25 | 26 | handler = new LoginStateableHandler(this); 27 | handler.onCreate(savedInstanceState); 28 | } 29 | 30 | @Override 31 | protected void onResume() { 32 | super.onResume(); 33 | 34 | handler.onResume(); 35 | } 36 | 37 | @Override 38 | protected void onPause() { 39 | super.onPause(); 40 | 41 | handler.onPause(); 42 | } 43 | 44 | @Override 45 | protected void onSaveInstanceState(Bundle outState) { 46 | super.onSaveInstanceState(outState); 47 | handler.onSaveInstanceState(outState); 48 | } 49 | 50 | private void showFragment(Fragment fragment) { 51 | FragmentManager fragmentManager = getSupportFragmentManager(); 52 | 53 | Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_container); 54 | if (currentFragment != null) { 55 | return; 56 | } 57 | 58 | FragmentTransaction transaction = fragmentManager.beginTransaction(); 59 | transaction.replace(R.id.fragment_container, fragment); 60 | transaction.commit(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/LoginStateableHandler.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample; 2 | 3 | import android.support.annotation.StringRes; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.view.View; 7 | import android.widget.Toast; 8 | import com.polidea.statemachine.State; 9 | import com.polidea.statemachine.StateMachine; 10 | import com.polidea.statemachine.handler.BaseStateableHandler; 11 | import com.polidea.statemachine.sample.events.BackStackChangedEvent; 12 | import com.polidea.statemachine.sample.state.LoginActionInterface; 13 | import com.polidea.statemachine.sample.state.LoginEvents; 14 | import com.polidea.statemachine.sample.state.LoginInitialState; 15 | import com.polidea.statemachine.sample.state.LoginProvider; 16 | import com.polidea.statemachine.sample.state.OnGoingLoginState; 17 | import com.polidea.statemachine.sample.state.WaitingForLoginRequestState; 18 | import com.squareup.otto.Bus; 19 | import javax.inject.Inject; 20 | 21 | public class LoginStateableHandler extends BaseStateableHandler implements LoginProvider, LoginActionInterface { 22 | 23 | @Inject 24 | Bus bus; 25 | 26 | private final LoginActivity mainActivity; 27 | 28 | protected LoginStateableHandler(LoginActivity mainActivity) { 29 | super(mainActivity); 30 | Application.getComponentInstance().inject(this); 31 | this.mainActivity = mainActivity; 32 | addOnBackStackChangedListener(); 33 | } 34 | 35 | private void addOnBackStackChangedListener() { 36 | final FragmentManager supportFragmentManager = mainActivity.getSupportFragmentManager(); 37 | supportFragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { 38 | @Override 39 | public void onBackStackChanged() { 40 | Fragment currentFragment = supportFragmentManager.findFragmentById(R.id.fragment_container); 41 | bus.post(new BackStackChangedEvent(currentFragment)); 42 | } 43 | }); 44 | } 45 | 46 | @Override 47 | public LoginProvider getStateProvider() { 48 | return this; 49 | } 50 | 51 | @Override 52 | public LoginActionInterface getActionInterface() { 53 | return this; 54 | } 55 | 56 | @Override 57 | public FragmentManager provideFragmentManager() { 58 | return mainActivity.getSupportFragmentManager(); 59 | } 60 | 61 | @Override 62 | public int provideFragmentContainerId() { 63 | return R.id.fragment_container; 64 | } 65 | 66 | @Override 67 | public void onStateMachineDescribe(StateMachine stateMachine) { 68 | stateMachine.addTransitionFromClass(LoginInitialState.class, LoginEvents.START_LOGIN, OnGoingLoginState.class); 69 | 70 | stateMachine.addTransitionFromClass(OnGoingLoginState.class, LoginEvents.CANCELLED, LoginInitialState.class); 71 | stateMachine.addTransitionFromClass(OnGoingLoginState.class, LoginEvents.LOGIN_IN_PROGRESS, WaitingForLoginRequestState.class); 72 | 73 | stateMachine.addTransitionFromClass(WaitingForLoginRequestState.class, LoginEvents.FINISHED, LoginInitialState.class); 74 | stateMachine.addTransitionFromClass(WaitingForLoginRequestState.class, LoginEvents.CANCELLED, LoginInitialState.class); 75 | } 76 | 77 | @Override 78 | public Class getInitialStateClass() { 79 | return LoginInitialState.class; 80 | } 81 | 82 | @Override 83 | public void loginSuccess() { 84 | showMessage(R.string.login_success); 85 | } 86 | 87 | @Override 88 | public void loginError() { 89 | showMessage(R.string.login_error); 90 | } 91 | 92 | @Override 93 | public void loginCancelled() { 94 | showMessage(R.string.login_cancelled); 95 | } 96 | 97 | @Override 98 | public void showLoginLoading() { 99 | mainActivity.progressContainer.setVisibility(View.VISIBLE); 100 | } 101 | 102 | @Override 103 | public void hideLoginLoading() { 104 | mainActivity.progressContainer.setVisibility(View.GONE); 105 | } 106 | 107 | public void showMessage(@StringRes int messageRes) { 108 | Toast.makeText(mainActivity, messageRes, Toast.LENGTH_SHORT).show(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/di/ApplicationComponent.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.di; 2 | 3 | import android.app.Application; 4 | import com.polidea.statemachine.sample.LoginStateableHandler; 5 | import com.polidea.statemachine.sample.fragment.LoginFragment; 6 | import com.polidea.statemachine.sample.fragment.NotLoggedInFragment; 7 | import com.polidea.statemachine.sample.state.LoginInitialState; 8 | import com.polidea.statemachine.sample.state.OnGoingLoginState; 9 | import com.polidea.statemachine.sample.state.WaitingForLoginRequestState; 10 | import dagger.Component; 11 | import javax.inject.Singleton; 12 | 13 | @Singleton 14 | @Component(modules = {MainModule.class}) 15 | public interface ApplicationComponent { 16 | 17 | void inject(NotLoggedInFragment notLoggedInFragment); 18 | 19 | void inject(LoginInitialState loginInitialState); 20 | 21 | void inject(LoginFragment loginFragment); 22 | 23 | void inject(OnGoingLoginState onGoingLoginState); 24 | 25 | void inject(WaitingForLoginRequestState waitingForLoginRequestState); 26 | 27 | void inject(LoginStateableHandler loginStateableHandler); 28 | 29 | final class Initializer { 30 | 31 | private Initializer() { 32 | } 33 | 34 | public static ApplicationComponent init(Application app) { 35 | DaggerApplicationComponent.Builder builder = DaggerApplicationComponent.builder(); 36 | 37 | return builder 38 | .mainModule(new MainModule(app)) 39 | .build(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/di/MainModule.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.di; 2 | 3 | import android.app.Application; 4 | import com.polidea.statemachine.sample.manager.NetworkManager; 5 | import com.squareup.otto.Bus; 6 | import dagger.Module; 7 | import dagger.Provides; 8 | import javax.inject.Singleton; 9 | 10 | @Module 11 | public class MainModule { 12 | 13 | private final Application application; 14 | 15 | public MainModule(Application application) { 16 | this.application = application; 17 | } 18 | 19 | @Singleton 20 | @Provides 21 | Bus provideBus() { 22 | return new Bus(); 23 | } 24 | 25 | @Singleton 26 | @Provides 27 | NetworkManager provideNetworkManager() { 28 | return new NetworkManager(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/events/BackStackChangedEvent.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.events; 2 | 3 | import android.support.v4.app.Fragment; 4 | 5 | public class BackStackChangedEvent { 6 | 7 | private final Fragment currentFragment; 8 | 9 | public BackStackChangedEvent(Fragment currentFragment) { 10 | this.currentFragment = currentFragment; 11 | } 12 | 13 | public Fragment getCurrentFragment() { 14 | return currentFragment; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/events/LoginEvent.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.events; 2 | 3 | public class LoginEvent { 4 | 5 | private final String email, password; 6 | 7 | public LoginEvent(String email, String password) { 8 | this.email = email; 9 | this.password = password; 10 | } 11 | 12 | public String getEmail() { 13 | return email; 14 | } 15 | 16 | public String getPassword() { 17 | return password; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/events/StartLoginEvent.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.events; 2 | 3 | public class StartLoginEvent { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/fragment/LoggedInFragment.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import com.polidea.statemachine.sample.R; 10 | 11 | public class LoggedInFragment extends Fragment{ 12 | 13 | @Nullable 14 | @Override 15 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 16 | return inflater.inflate(R.layout.fragment_logged_in, container, false); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/fragment/LoginFragment.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.Button; 10 | import android.widget.EditText; 11 | import com.polidea.statemachine.sample.Application; 12 | import com.polidea.statemachine.sample.R; 13 | import com.polidea.statemachine.sample.events.LoginEvent; 14 | import com.squareup.otto.Bus; 15 | import javax.inject.Inject; 16 | 17 | public class LoginFragment extends Fragment{ 18 | 19 | @Inject 20 | Bus bus; 21 | 22 | public LoginFragment() { 23 | Application.getComponentInstance().inject(this); 24 | } 25 | 26 | @Nullable 27 | @Override 28 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 29 | return inflater.inflate(R.layout.fragment_login, container, false); 30 | } 31 | 32 | @Override 33 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 34 | super.onViewCreated(view, savedInstanceState); 35 | 36 | final EditText emailEditText = (EditText) view.findViewById(R.id.email); 37 | final EditText passwordEditText = (EditText) view.findViewById(R.id.password); 38 | 39 | Button loginButton = (Button) view.findViewById(R.id.login); 40 | loginButton.setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View v) { 43 | bus.post(new LoginEvent(emailEditText.getText().toString(), passwordEditText.getText().toString())); 44 | } 45 | }); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/fragment/NotLoggedInFragment.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.Button; 10 | import com.polidea.statemachine.sample.Application; 11 | import com.polidea.statemachine.sample.R; 12 | import com.polidea.statemachine.sample.events.StartLoginEvent; 13 | import com.squareup.otto.Bus; 14 | import javax.inject.Inject; 15 | 16 | public class NotLoggedInFragment extends Fragment{ 17 | 18 | @Inject 19 | Bus bus; 20 | 21 | public NotLoggedInFragment() { 22 | Application.getComponentInstance().inject(this); 23 | } 24 | 25 | @Nullable 26 | @Override 27 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 28 | return inflater.inflate(R.layout.fragment_not_logged_in, container, false); 29 | } 30 | 31 | @Override 32 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 33 | super.onViewCreated(view, savedInstanceState); 34 | 35 | Button loginButton = (Button) view.findViewById(R.id.login); 36 | loginButton.setOnClickListener(new View.OnClickListener() { 37 | @Override 38 | public void onClick(View v) { 39 | bus.post(new StartLoginEvent()); 40 | } 41 | }); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/manager/NetworkManager.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.manager; 2 | 3 | import android.os.Handler; 4 | 5 | public class NetworkManager { 6 | 7 | private LoginUserListener listener; 8 | 9 | public interface LoginUserListener { 10 | 11 | void loginUserSuccess(); 12 | 13 | void loginUserFailure(); 14 | } 15 | 16 | public Handler handler = new Handler(); 17 | 18 | public void loginUser(String email, String password) { 19 | handler.postDelayed(new Runnable() { 20 | @Override 21 | public void run() { 22 | if(listener != null) { 23 | listener.loginUserSuccess(); 24 | } 25 | } 26 | }, 1500); 27 | } 28 | 29 | public void setLoginUserListener(LoginUserListener listener) { 30 | this.listener = listener; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/BaseLoginState.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentManager; 5 | import android.support.v4.app.FragmentTransaction; 6 | import com.polidea.statemachine.State; 7 | import com.polidea.statemachine.sample.fragment.LoginFragment; 8 | import com.squareup.otto.Bus; 9 | import com.squareup.otto.Subscribe; 10 | import javax.inject.Inject; 11 | 12 | public abstract class BaseLoginState extends State { 13 | 14 | protected abstract void injectDependencies(); 15 | 16 | @Inject 17 | Bus bus; 18 | 19 | public BaseLoginState() { 20 | injectDependencies(); 21 | } 22 | 23 | @Override 24 | public void onStateApplied() { 25 | bus.register(this); 26 | } 27 | 28 | @Override 29 | public void onStateLeft() { 30 | bus.unregister(this); 31 | } 32 | 33 | protected void changeFragment(Fragment fragment) { 34 | FragmentManager fragmentManager = getProvider().provideFragmentManager(); 35 | int fragmentContainerId = getProvider().provideFragmentContainerId(); 36 | 37 | FragmentTransaction transaction = fragmentManager.beginTransaction(); 38 | transaction.replace(fragmentContainerId, fragment); 39 | transaction.commit(); 40 | } 41 | 42 | protected void popFragment() { 43 | FragmentManager fragmentManager = getProvider().provideFragmentManager(); 44 | fragmentManager.popBackStack(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/LoginActionInterface.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | public interface LoginActionInterface { 4 | 5 | void loginSuccess(); 6 | 7 | void loginError(); 8 | 9 | void loginCancelled(); 10 | 11 | void showLoginLoading(); 12 | 13 | void hideLoginLoading(); 14 | } 15 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/LoginEvents.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | public interface LoginEvents { 4 | int START_LOGIN = 1; 5 | int CANCELLED = 2; 6 | int LOGIN_IN_PROGRESS = 3; 7 | int FINISHED = 4; 8 | } 9 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/LoginInitialState.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | import com.polidea.statemachine.sample.Application; 4 | import com.polidea.statemachine.sample.events.StartLoginEvent; 5 | import com.polidea.statemachine.sample.fragment.LoginFragment; 6 | import com.squareup.otto.Subscribe; 7 | 8 | public class LoginInitialState extends BaseLoginState { 9 | 10 | 11 | @Override 12 | protected void injectDependencies() { 13 | Application.getComponentInstance().inject(this); 14 | } 15 | 16 | @Subscribe 17 | public void onStartLoginEvent(StartLoginEvent startLoginEvent) { 18 | changeFragment(new LoginFragment()); 19 | 20 | fireEvent(LoginEvents.START_LOGIN); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/LoginProvider.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | import android.support.v4.app.FragmentManager; 4 | import com.polidea.statemachine.StateProvider; 5 | 6 | public interface LoginProvider extends StateProvider { 7 | 8 | FragmentManager provideFragmentManager(); 9 | 10 | int provideFragmentContainerId(); 11 | } 12 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/OnGoingLoginState.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | import com.polidea.statemachine.sample.Application; 4 | import com.polidea.statemachine.sample.events.BackStackChangedEvent; 5 | import com.polidea.statemachine.sample.events.LoginEvent; 6 | import com.polidea.statemachine.sample.manager.NetworkManager; 7 | import com.squareup.otto.Subscribe; 8 | import javax.inject.Inject; 9 | 10 | public class OnGoingLoginState extends BaseLoginState { 11 | 12 | @Inject 13 | NetworkManager networkManager; 14 | 15 | @Override 16 | protected void injectDependencies() { 17 | Application.getComponentInstance().inject(this); 18 | } 19 | 20 | @Subscribe 21 | public void onLoginEvent(LoginEvent loginEvent) { 22 | networkManager.loginUser(loginEvent.getEmail(), loginEvent.getPassword()); 23 | fireEvent(LoginEvents.LOGIN_IN_PROGRESS); 24 | } 25 | 26 | @Subscribe 27 | public void onBackStackChangedEvent(BackStackChangedEvent backStackChangedEvent) { 28 | fireEvent(LoginEvents.CANCELLED); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/java/com/polidea/statemachine/sample/state/WaitingForLoginRequestState.java: -------------------------------------------------------------------------------- 1 | package com.polidea.statemachine.sample.state; 2 | 3 | import com.polidea.statemachine.sample.Application; 4 | import com.polidea.statemachine.sample.events.BackStackChangedEvent; 5 | import com.polidea.statemachine.sample.fragment.LoggedInFragment; 6 | import com.polidea.statemachine.sample.manager.NetworkManager; 7 | import com.squareup.otto.Subscribe; 8 | import javax.inject.Inject; 9 | 10 | public class WaitingForLoginRequestState extends BaseLoginState { 11 | 12 | @Inject 13 | NetworkManager networkManager; 14 | 15 | @Override 16 | protected void injectDependencies() { 17 | Application.getComponentInstance().inject(this); 18 | } 19 | 20 | @Override 21 | public void onStateApplied() { 22 | super.onStateApplied(); 23 | 24 | getActionInterface().showLoginLoading(); 25 | 26 | networkManager.setLoginUserListener(new NetworkManager.LoginUserListener() { 27 | @Override 28 | public void loginUserSuccess() { 29 | getActionInterface().loginSuccess(); 30 | 31 | changeFragment(new LoggedInFragment()); 32 | 33 | fireEvent(LoginEvents.FINISHED); 34 | } 35 | 36 | @Override 37 | public void loginUserFailure() { 38 | getActionInterface().loginError(); 39 | 40 | popFragment(); 41 | 42 | fireEvent(LoginEvents.FINISHED); 43 | } 44 | }); 45 | } 46 | 47 | @Override 48 | public void onStateLeft() { 49 | super.onStateLeft(); 50 | 51 | networkManager.setLoginUserListener(null); 52 | getActionInterface().hideLoginLoading(); 53 | } 54 | 55 | @Subscribe 56 | public void onBackStackChangedEvent(BackStackChangedEvent backStackChangedEvent) { 57 | fireEvent(LoginEvents.CANCELLED); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 12 | 13 | 19 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/res/layout/fragment_logged_in.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 10 | -------------------------------------------------------------------------------- /statemachine-sample/src/main/res/layout/fragment_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 14 | 15 | 22 | 23 |