Skip to content

Commit afe9154

Browse files
committed
feat: complete 'Multi Step' sample app
1 parent af7a103 commit afe9154

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

multi-step/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
tools:targetApi="31">
1515
<activity
1616
android:name=".StateMachineActivity"
17-
android:exported="false" />
17+
android:exported="true" />
1818
<activity
1919
android:name=".MainActivity"
2020
android:exported="true">

multi-step/app/src/main/java/com/droidground/multistep/StateMachineActivity.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.droidground.multistep
22

3+
import android.content.Context
4+
import android.content.Intent
35
import android.os.Bundle
6+
import android.widget.TextView
47
import androidx.activity.enableEdgeToEdge
58
import androidx.appcompat.app.AppCompatActivity
69
import androidx.core.view.ViewCompat
@@ -18,5 +21,51 @@ class StateMachineActivity : AppCompatActivity() {
1821
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
1922
insets
2023
}
24+
25+
stateMachine(intent);
26+
}
27+
28+
29+
private fun getCurrentState(): Int {
30+
val sharedPref = getPreferences(Context.MODE_PRIVATE) ?: return 0
31+
val currentState = sharedPref.getInt("currentState", 0)
32+
return currentState
33+
}
34+
35+
private fun setCurrentState(state: Number) {
36+
val sharedPref = getPreferences(Context.MODE_PRIVATE) ?: return
37+
with (sharedPref.edit()) {
38+
putInt("currentState", state.toInt())
39+
apply()
40+
}
41+
}
42+
43+
private fun setText(txt: String) {
44+
val myTextView = findViewById<TextView>(R.id.state_machine_txt)
45+
myTextView.text = txt
46+
}
47+
48+
fun stateMachine(intent: Intent) {
49+
val action = intent.action
50+
val ordinal = getCurrentState()
51+
if (ordinal != 0) {
52+
if (ordinal != 1) {
53+
if (ordinal == 2) {
54+
setCurrentState(0)
55+
setText(flag)
56+
return
57+
}
58+
} else if ("GET_FLAG" == action) {
59+
setCurrentState(2)
60+
setText("Transitioned from PREPARE to GET_FLAG.")
61+
return
62+
}
63+
} else if ("PREPARE_FLAG" == action) {
64+
setCurrentState(1)
65+
setText("Transitioned from INIT to PREPARE.")
66+
return
67+
}
68+
69+
setCurrentState(0)
2170
}
2271
}

multi-step/app/src/main/res/layout/activity_state_machine.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@
77
android:layout_height="match_parent"
88
tools:context=".StateMachineActivity">
99

10+
<TextView
11+
android:id="@+id/state_machine_txt"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_marginStart="69dp"
15+
android:layout_marginTop="356dp"
16+
android:text="Hi! I'm a state machine. I'm in the INIT state."
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
1019
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)