2121import org .greenrobot .eventbus .Subscribe ;
2222import org .greenrobot .eventbus .ThreadMode ;
2323
24+ import java .util .concurrent .atomic .AtomicBoolean ;
25+
2426import co .krypt .krypton .R ;
2527import co .krypt .krypton .exception .CryptoException ;
2628import co .krypt .krypton .me .MeStorage ;
@@ -36,10 +38,14 @@ public class OnboardingVerifyEmailFragment extends Fragment {
3638
3739 private final String TAG = "OnboardingVerifyEmail" ;
3840
39- private CreateTeamProgress progress ;
40-
4141 private AppCompatEditText emailEditText ;
4242
43+ private ProgressBar progressBar ;
44+ private Button cancelButton ;
45+ private Button changeButton ;
46+ private Button resendButton ;
47+ ConstraintLayout checkYourEmailContainer ;
48+
4349 public OnboardingVerifyEmailFragment () {
4450 }
4551
@@ -56,106 +62,43 @@ public void onCreate(Bundle savedInstanceState) {
5662 public View onCreateView (LayoutInflater inflater , ViewGroup container ,
5763 Bundle savedInstanceState ) {
5864 final View rootView = inflater .inflate (R .layout .fragment_teams_onboarding_verify_email , container , false );
59- Button cancelButton = rootView .findViewById (R .id .cancelButton );
65+ cancelButton = rootView .findViewById (R .id .cancelButton );
6066 cancelButton .setOnClickListener (v -> {
61- progress .reset ();
67+ new CreateTeamProgress ( v . getContext ()) .reset ();
6268 getActivity ().finish ();
6369 });
6470
65- ProgressBar progressBar = rootView .findViewById (R .id .progressBar );
71+ progressBar = rootView .findViewById (R .id .progressBar );
6672 progressBar .setAlpha (0 );
6773
68- Button changeButton = rootView .findViewById (R .id .changeButton );
74+ changeButton = rootView .findViewById (R .id .changeButton );
6975 changeButton .setEnabled (false );
70- Button resendButton = rootView .findViewById (R .id .resendButton );
76+ resendButton = rootView .findViewById (R .id .resendButton );
7177 resendButton .setEnabled (false );
72- ConstraintLayout checkYourEmailContainer = rootView .findViewById (R .id .checkYourEmailContainer );
78+ checkYourEmailContainer = rootView .findViewById (R .id .checkYourEmailContainer );
7379 checkYourEmailContainer .setAlpha (0 );
7480
7581 AppCompatTextView teamName = rootView .findViewById (R .id .teamName );
76- teamName .setText (progress .getTeamOnboardingData ().name );
82+ teamName .setText (new CreateTeamProgress ( getContext ()) .getTeamOnboardingData ().name );
7783
7884 emailEditText = rootView .findViewById (R .id .profileEmail );
7985
80- InputMethodManager imm = (InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
81- Runnable focusEmail = () -> {
82- emailEditText .setEnabled (true );
83- emailEditText .requestFocus ();
84- if (imm != null ) {
85- imm .toggleSoftInput (InputMethodManager .SHOW_FORCED , 0 );
86- }
87- progressBar .animate ().alpha (0 ).setDuration (500 ).start ();
88- };
89- focusEmail .run ();
90-
91- Runnable requestEmailChallenge = () -> {
92- emailEditText .setEnabled (false );
93- emailEditText .clearFocus ();
94- if (imm != null ) {
95- imm .hideSoftInputFromWindow (emailEditText .getWindowToken (), 0 );
96- }
97- progressBar .animate ().alpha (1 ).setDuration (1000 ).start ();
98-
99- final String email = emailEditText .getEditableText ().toString ();
100-
101- new Thread (() -> {
102- MeStorage meStorage = Silo .shared (getContext ()).meStorage ();
103- try {
104- meStorage .setEmail (email );
105- } catch (CryptoException e ) {
106- Error .shortToast (getContext (), "Failed to set email: " + e .getMessage ());
107- e .printStackTrace ();
108- return ;
109- }
110- Profile me = meStorage .load ();
111- progress .updateTeamData ((s , d ) -> {
112- if (s .equals (CreateStage .VERIFY_EMAIL ) || s .equals (CreateStage .EMAIL_CHALLENGE_SENT )) {
113- d .creatorProfile = me ;
114- Log .i (TAG , "requesting email challenge..." );
115- try {
116- final Sigchain .NativeResult <JsonObject > result = TeamDataProvider .requestEmailChallenge (getContext (), email );
117- emailEditText .post (() -> {
118- if (result .success != null ) {
119- Log .i (TAG , "request email challenge success" );
120- progressBar .animate ().alpha (0 ).setDuration (500 ).start ();
121- checkYourEmailContainer .animate ().alpha (1 ).setDuration (1000 ).start ();
122- changeButton .setEnabled (true );
123- resendButton .setEnabled (true );
124- } else {
125- Log .i (TAG , "request email challenge failure: " + result .error );
126- Error .shortToast (getContext (), "Error sending email: " + result .error );
127- focusEmail .run ();
128- }
129- });
130- if (result .success != null ) {
131- return CreateStage .EMAIL_CHALLENGE_SENT ;
132- } else {
133- return s ;
134- }
135- } catch (Native .NotLinked notLinked ) {
136- notLinked .printStackTrace ();
137- }
138- }
139- return s ;
140- });
141- }).start ();
142- };
14386 emailEditText .setOnEditorActionListener ((v , i , ev ) -> {
144- requestEmailChallenge . run ( );
87+ EventBus . getDefault (). post ( new RequestEmailChallenge () );
14588 return false ;
14689 });
14790 emailEditText .setOnFocusChangeListener ((view , b ) -> {
14891 if (view .isEnabled () && !b ) {
149- requestEmailChallenge . run ( );
92+ EventBus . getDefault (). post ( new RequestEmailChallenge () );
15093 }
15194 });
15295
15396 resendButton .setOnClickListener (v -> {
154- requestEmailChallenge . run ( );
97+ EventBus . getDefault (). post ( new RequestEmailChallenge () );
15598 });
15699
157100 changeButton .setOnClickListener (v -> {
158- focusEmail . run ( );
101+ EventBus . getDefault (). post ( new FocusEmail () );
159102 });
160103
161104 Button submitButton = rootView .findViewById (R .id .submitButton );
@@ -173,35 +116,94 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
173116
174117 EventBus .getDefault ().register (this );
175118 EventBus .getDefault ().post (new IdentityService .GetProfile (getContext ()));
119+
120+ EventBus .getDefault ().post (new FocusEmail ());
176121 return rootView ;
177122 }
178123
124+ private AtomicBoolean updatedInitialEmail = new AtomicBoolean (false );
179125 @ Subscribe (sticky = true , threadMode = ThreadMode .MAIN )
180126 public void updateInitialEmail (IdentityService .GetProfileResult r ) {
181- EventBus .getDefault ().unregister (this );
182- if (emailEditText .getText ().length () == 0 && r .profile != null ) {
127+ if (emailEditText .getText ().length () == 0 && r .profile != null && !updatedInitialEmail .getAndSet (true )) {
183128 emailEditText .setText (r .profile .email );
184129 }
185130 }
186131
187- @ Override
188- public void onDestroyView () {
189- if (EventBus .getDefault ().isRegistered (this )) {
190- EventBus .getDefault ().unregister (this );
132+ private static class FocusEmail {}
133+ @ Subscribe (threadMode = ThreadMode .MAIN )
134+ public void focusEmail (FocusEmail focusEmail ) {
135+ InputMethodManager imm = (InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
136+ emailEditText .setEnabled (true );
137+ emailEditText .requestFocus ();
138+ if (imm != null ) {
139+ imm .toggleSoftInput (InputMethodManager .SHOW_FORCED , 0 );
191140 }
192- super . onDestroyView ();
141+ progressBar . animate (). alpha ( 0 ). setDuration ( 500 ). start ();
193142 }
194143
195- @ Override
196- public void onAttach (Context context ) {
197- super .onAttach (context );
144+ private static class RequestEmailChallenge {}
145+ @ Subscribe (threadMode = ThreadMode .MAIN )
146+ public void requestEmailChallenge (RequestEmailChallenge requestEmailChallenge ) {
147+ InputMethodManager imm = (InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
148+ emailEditText .setEnabled (false );
149+ emailEditText .clearFocus ();
150+ if (imm != null ) {
151+ imm .hideSoftInputFromWindow (emailEditText .getWindowToken (), 0 );
152+ }
153+ progressBar .animate ().alpha (1 ).setDuration (1000 ).start ();
154+
155+ final String email = emailEditText .getEditableText ().toString ();
156+
157+ new Thread (() -> {
158+ MeStorage meStorage = Silo .shared (getContext ()).meStorage ();
159+ try {
160+ meStorage .setEmail (email );
161+ } catch (CryptoException e ) {
162+ Error .shortToast (getContext (), "Failed to set email: " + e .getMessage ());
163+ e .printStackTrace ();
164+ return ;
165+ }
166+ Profile me = meStorage .load ();
167+ new CreateTeamProgress (getContext ()).updateTeamData ((s , d ) -> {
168+ if (s .equals (CreateStage .VERIFY_EMAIL ) || s .equals (CreateStage .EMAIL_CHALLENGE_SENT )) {
169+ d .creatorProfile = me ;
170+ Log .i (TAG , "requesting email challenge..." );
171+ try {
172+ final Sigchain .NativeResult <JsonObject > result = TeamDataProvider .requestEmailChallenge (getContext (), email );
173+ emailEditText .post (() -> {
174+ if (result .success != null ) {
175+ Log .i (TAG , "request email challenge success" );
176+ progressBar .animate ().alpha (0 ).setDuration (500 ).start ();
177+ checkYourEmailContainer .animate ().alpha (1 ).setDuration (1000 ).start ();
178+ changeButton .setEnabled (true );
179+ resendButton .setEnabled (true );
180+ } else {
181+ Log .i (TAG , "request email challenge failure: " + result .error );
182+ Error .shortToast (getContext (), "Error sending email: " + result .error );
183+ EventBus .getDefault ().post (new FocusEmail ());
184+ }
185+ });
186+ if (result .success != null ) {
187+ return CreateStage .EMAIL_CHALLENGE_SENT ;
188+ } else {
189+ return s ;
190+ }
191+ } catch (Native .NotLinked notLinked ) {
192+ notLinked .printStackTrace ();
193+ }
194+ }
195+ return s ;
196+ });
197+ }).start ();
198198
199- progress = new CreateTeamProgress (context );
200199 }
201200
202201 @ Override
203- public void onDetach () {
204- super .onDetach ();
205- progress = null ;
202+ public void onDestroyView () {
203+ if (EventBus .getDefault ().isRegistered (this )) {
204+ EventBus .getDefault ().unregister (this );
205+ }
206+ updatedInitialEmail .set (false );
207+ super .onDestroyView ();
206208 }
207209}
0 commit comments