11package com.droidground.multistep
22
3+ import android.content.Context
4+ import android.content.Intent
35import android.os.Bundle
6+ import android.widget.TextView
47import androidx.activity.enableEdgeToEdge
58import androidx.appcompat.app.AppCompatActivity
69import 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}
0 commit comments