33import android .content .Context ;
44import android .content .Intent ;
55import android .content .SharedPreferences ;
6+
67import com .google .android .material .floatingactionbutton .FloatingActionButton ;
78
89import android .os .Bundle ;
1920import com .queue_it .androidsdk .Error ;
2021
2122public class MainActivity extends AppCompatActivity {
23+ RadioButton enableCacheRadioButton ;
24+ FloatingActionButton queue_button ;
25+ EditText customerIdEditText ;
26+ EditText eventIdEditText ;
27+ EditText layoutNameEditText ;
28+ EditText languageEditText ;
29+ EditText enqueueTokenEditText ;
30+ EditText enqueueKeyEditText ;
31+ RadioButton testRadioButton ;
32+
33+ private void runQueue (QueueITEngine queueITEngine ) throws QueueITException {
34+ String enqueueToken = enqueueTokenEditText .getText ().toString ();
35+ String enqueueKey = enqueueKeyEditText .getText ().toString ();
36+ if (enqueueToken .length () > 0 ) {
37+ queueITEngine .runWithEnqueueToken (MainActivity .this , enqueueToken , !enableCacheRadioButton .isChecked ());
38+ } else if (enqueueKey .length () > 0 ) {
39+ queueITEngine .runWithEnqueueKey (MainActivity .this , enqueueKey , !enableCacheRadioButton .isChecked ());
40+ } else {
41+ queueITEngine .run (MainActivity .this , !enableCacheRadioButton .isChecked ());
42+ }
43+ }
2244
2345 @ Override
2446 protected void onCreate (Bundle savedInstanceState ) {
2547 super .onCreate (savedInstanceState );
2648 setContentView (R .layout .activity_main );
2749
28- final FloatingActionButton queue_button = (FloatingActionButton )findViewById (R .id .queue_button );
29-
30- final EditText customerIdEditText = (EditText )findViewById (R .id .customerid_edittext );
31- final EditText eventIdEditText = (EditText )findViewById (R .id .eventid_edittext );
32- final EditText layoutNameEditText = (EditText )findViewById (R .id .layoutname_edittext );
33- final EditText languageEditText = (EditText )findViewById (R .id .language_edittext );
34- final RadioButton testRadioButton = (RadioButton )findViewById (R .id .radio_environment_test );
35- final RadioButton enableCacheRadioButton = (RadioButton )findViewById (R .id .radio_cache_enabled );
36-
50+ queue_button = findViewById (R .id .queue_button );
51+ customerIdEditText = findViewById (R .id .customerid_edittext );
52+ eventIdEditText = findViewById (R .id .eventid_edittext );
53+ layoutNameEditText = findViewById (R .id .layoutname_edittext );
54+ languageEditText = findViewById (R .id .language_edittext );
55+ testRadioButton = findViewById (R .id .radio_environment_test );
56+ enableCacheRadioButton = findViewById (R .id .radio_cache_enabled );
3757 customerIdEditText .addTextChangedListener (getRequiredTextValidator (customerIdEditText ));
3858 eventIdEditText .addTextChangedListener (getRequiredTextValidator (eventIdEditText ));
59+ enqueueTokenEditText = findViewById (R .id .enqueuetoken_edittext );
60+ enqueueKeyEditText = findViewById (R .id .enqueuekey_edittext );
3961
4062 final SharedPreferences sharedPreferences = getPreferences (Context .MODE_PRIVATE );
4163 String customerId = sharedPreferences .getString ("customerId" , "" );
4264 String eventOrAliasId = sharedPreferences .getString ("eventOrAliasId" , "" );
4365 String layoutName = sharedPreferences .getString ("layoutName" , "" );
4466 String language = sharedPreferences .getString ("language" , "" );
67+ String enqueueToken = sharedPreferences .getString ("enqueueToken" , "" );
68+ String enqueueKey = sharedPreferences .getString ("enqueueKey" , "" );
4569
4670 customerIdEditText .setText (customerId );
4771 eventIdEditText .setText (eventOrAliasId );
4872 layoutNameEditText .setText (layoutName );
4973 languageEditText .setText (language );
74+ enqueueTokenEditText .setText (enqueueToken );
75+ enqueueKeyEditText .setText (enqueueKey );
5076
5177 queue_button .setOnClickListener (new View .OnClickListener () {
5278 @ Override
@@ -63,17 +89,32 @@ public void onClick(View v) {
6389 String eventOrAliasId = eventIdEditText .getText ().toString ();
6490 String layoutName = layoutNameEditText .getText ().toString ();
6591 String language = languageEditText .getText ().toString ();
92+ String enqueueToken = enqueueTokenEditText .getText ().toString ();
93+ String enqueueKey = enqueueKeyEditText .getText ().toString ();
6694
6795 SharedPreferences .Editor editor = sharedPreferences .edit ();
6896 editor .putString ("customerId" , customerId );
6997 editor .putString ("eventOrAliasId" , eventOrAliasId );
7098 editor .putString ("layoutName" , layoutName );
7199 editor .putString ("language" , language );
100+ editor .putString ("enqueueToken" , enqueueToken );
101+ editor .putString ("enqueueKey" , enqueueKey );
72102 editor .commit ();
73103
74104 Toast .makeText (getApplicationContext (), "Please wait for your turn." , Toast .LENGTH_SHORT ).show ();
75105
76106 QueueITEngine queueITEngine = new QueueITEngine (MainActivity .this , customerId , eventOrAliasId , layoutName , language , new QueueListener () {
107+
108+ @ Override
109+ public void onSessionRestart (QueueITEngine queueITEngine ) {
110+ try {
111+ runQueue (queueITEngine );
112+ } catch (QueueITException e ) {
113+ Toast .makeText (getApplicationContext (), "Please try again." , Toast .LENGTH_LONG ).show ();
114+ queue_button .setEnabled (true );
115+ }
116+ }
117+
77118 @ Override
78119 public void onQueuePassed (QueuePassedInfo queuePassedInfo ) {
79120 showResultActivity ("You passed the queue! Your token: " + queuePassedInfo .getQueueItToken (), true );
@@ -111,48 +152,42 @@ public void onError(Error error, String errorMessage) {
111152
112153 });
113154 try {
114- queueITEngine .run (MainActivity .this , !enableCacheRadioButton .isChecked ());
115- }
116- catch (QueueITException e ) {
155+ runQueue (queueITEngine );
156+ } catch (QueueITException e ) {
117157 Toast .makeText (getApplicationContext (), "Please try again." , Toast .LENGTH_LONG ).show ();
118158 queue_button .setEnabled (true );
119159 }
120160 }
121161 });
122162 }
123163
124- private void showResultActivity (String result , boolean success )
125- {
164+ private void showResultActivity (String result , boolean success ) {
126165 Intent intent = new Intent (this , ResultActivity .class );
127166 intent .putExtra ("success" , success );
128167 intent .putExtra ("result" , result );
129168 startActivity (intent );
130169 }
131170
132- private boolean isAlphaNumeric (String s ){
171+ private boolean isAlphaNumeric (String s ) {
133172 String pattern = "^[a-zA-Z0-9]*$" ;
134173 return s .matches (pattern );
135174 }
136175
137- private void hideKeyboard ()
138- {
176+ private void hideKeyboard () {
139177 View view = this .getCurrentFocus ();
140178 if (view != null ) {
141- InputMethodManager imm = (InputMethodManager )getSystemService (Context .INPUT_METHOD_SERVICE );
179+ InputMethodManager imm = (InputMethodManager ) getSystemService (Context .INPUT_METHOD_SERVICE );
142180 imm .hideSoftInputFromWindow (view .getWindowToken (), 0 );
143181 }
144182 }
145183
146- private TextValidator getRequiredTextValidator (TextView textView )
147- {
184+ private TextValidator getRequiredTextValidator (TextView textView ) {
148185 return new TextValidator (textView ) {
149- @ Override public void validate ( TextView textView , String text ) {
150- if ( TextUtils . isEmpty ( text ))
151- {
186+ @ Override
187+ public void validate ( TextView textView , String text ) {
188+ if ( TextUtils . isEmpty ( text )) {
152189 textView .setError ("Field required" );
153- }
154- else if (!isAlphaNumeric (text ))
155- {
190+ } else if (!isAlphaNumeric (text )) {
156191 textView .setError ("Must be alphanumeric" );
157192 }
158193 }
0 commit comments