44
55using System ;
66using System . ComponentModel ;
7+ using System . Diagnostics ;
78using System . Threading . Tasks ;
8- using Microsoft . Graph . Auth ;
99using Microsoft . Toolkit . Graph . Providers ;
1010using Windows . UI . Xaml ;
1111using Windows . UI . Xaml . Controls ;
@@ -33,22 +33,15 @@ public LoginButton()
3333 {
3434 this . DefaultStyleKey = typeof ( LoginButton ) ;
3535
36- ProviderManager . Instance . ProviderUpdated += ( sender , args ) =>
37- {
38- if ( ! IsLoading && ProviderManager . Instance ? . GlobalProvider ? . State == ProviderState . Loading )
39- {
40- IsLoading = true ;
41- }
42-
43- LoadData ( ) ;
44- } ;
36+ ProviderManager . Instance . ProviderUpdated += ( sender , args ) => LoadData ( ) ;
4537 }
4638
4739 /// <inheritdoc/>
4840 protected override void OnApplyTemplate ( )
4941 {
5042 base . OnApplyTemplate ( ) ;
5143
44+ IsLoading = true ;
5245 LoadData ( ) ;
5346
5447 if ( _loginButton != null )
@@ -111,7 +104,11 @@ private async void LoadData()
111104 return ;
112105 }
113106
114- if ( provider . State == ProviderState . SignedIn )
107+ if ( provider . State == ProviderState . Loading )
108+ {
109+ IsLoading = true ;
110+ }
111+ else if ( provider . State == ProviderState . SignedIn )
115112 {
116113 try
117114 {
@@ -123,18 +120,20 @@ private async void LoadData()
123120 {
124121 LoginFailed ? . Invoke ( this , new LoginFailedEventArgs ( e ) ) ;
125122 }
123+
124+ IsLoading = false ;
126125 }
127126 else if ( provider . State == ProviderState . SignedOut )
128127 {
129128 UserDetails = null ; // What if this was user provided? Should we not hook into these events then?
129+
130+ IsLoading = false ;
130131 }
131132 else
132133 {
133134 // Provider in Loading state
134- return ;
135+ Debug . Fail ( "unsupported state" ) ;
135136 }
136-
137- IsLoading = false ;
138137 }
139138
140139 /// <summary>
@@ -143,7 +142,7 @@ private async void LoadData()
143142 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
144143 public async Task LoginAsync ( )
145144 {
146- if ( UserDetails != null )
145+ if ( UserDetails != null || IsLoading )
147146 {
148147 return ;
149148 }
@@ -154,6 +153,7 @@ public async Task LoginAsync()
154153 {
155154 try
156155 {
156+ IsLoading = true ;
157157 await provider . LoginAsync ( ) ;
158158
159159 if ( provider . State == ProviderState . SignedIn )
@@ -172,6 +172,10 @@ public async Task LoginAsync()
172172 {
173173 LoginFailed ? . Invoke ( this , new LoginFailedEventArgs ( e ) ) ;
174174 }
175+ finally
176+ {
177+ IsLoading = false ;
178+ }
175179 }
176180 }
177181
@@ -181,6 +185,11 @@ public async Task LoginAsync()
181185 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
182186 public async Task LogoutAsync ( )
183187 {
188+ if ( IsLoading )
189+ {
190+ return ;
191+ }
192+
184193 var cargs = new CancelEventArgs ( ) ;
185194 LogoutInitiated ? . Invoke ( this , cargs ) ;
186195
@@ -202,7 +211,9 @@ public async Task LogoutAsync()
202211
203212 if ( provider != null )
204213 {
214+ IsLoading = true ;
205215 await provider . LogoutAsync ( ) ;
216+ IsLoading = false ;
206217
207218 LogoutCompleted ? . Invoke ( this , new EventArgs ( ) ) ;
208219 }
0 commit comments