1212using System . Diagnostics ;
1313using System . Runtime . InteropServices ;
1414using System . Text . Json . Nodes ;
15+ using static System . Runtime . InteropServices . JavaScript . JSType ;
1516
1617
1718namespace gamevault . Windows
@@ -174,10 +175,13 @@ private void ValidateSignInData(LoginUser loginUser)
174175 }
175176 }
176177
177- private async Task Login ( UserProfile profile , bool firstTimeLogin = false )
178+ private async Task Login ( UserProfile profile , bool firstTimeLogin = false , bool activationCall = false )
178179 {
179- ViewModel . StatusText = "Logging in..." ;
180- ViewModel . LoginStepIndex = ( int ) LoginStep . LoadingAction ;
180+ if ( ! activationCall )
181+ {
182+ ViewModel . StatusText = "Logging in..." ;
183+ ViewModel . LoginStepIndex = ( int ) LoginStep . LoadingAction ;
184+ }
181185 bool isLoggedInWithOAuth = Preferences . Get ( AppConfigKey . IsLoggedInWithOAuth , profile . UserConfigFile ) == "1" ;
182186 LoginState state = LoginState . Success ;
183187 if ( ! isLoggedInWithOAuth )
@@ -209,40 +213,56 @@ private async Task Login(UserProfile profile, bool firstTimeLogin = false)
209213 }
210214 if ( state == LoginState . Success )
211215 {
212- LoginManager . Instance . SetUserProfile ( profile ) ;
213- SettingsViewModel . Instance . Init ( ) ;
214- Preferences . Set ( AppConfigKey . UserID , LoginManager . Instance . GetCurrentUser ( ) ! . ID , profile . UserConfigFile ) ;
215- ViewModel . StatusText = "Optimizing Cache..." ;
216- await CacheHelper . OptimizeCache ( ) ;
217- if ( ViewModel . RememberMe )
218- {
219- Preferences . Set ( AppConfigKey . LastUserProfile , profile . RootDir , ProfileManager . ProfileConfigFile ) ;
220- }
221- App . Current . MainWindow = new MainWindow ( ) ;
222- if ( ! PipeServiceHandler . Instance . IsAppStartup && ! SettingsViewModel . Instance . BackgroundStart )
223- {
224- App . Current . MainWindow . Show ( ) ;
225- }
226- //First Startup Window Visibility will be determined by the protocol handler
227- try
228- {
229- this . DialogResult = true ; //will throw error, if its called from the MainWindow
230- }
231- catch { }
232- this . Close ( ) ;
216+ await LoadMainWindow ( profile ) ;
217+ }
218+ else if ( state == LoginState . NotActivated )
219+ {
220+ ViewModel . LoginStepIndex = ( int ) LoginStep . PendingActivation ;
221+ await Task . Delay ( 5000 ) ;
222+ if ( ViewModel . LoginStepIndex == ( int ) LoginStep . PendingActivation )
223+ await Login ( profile , firstTimeLogin , true ) ;
224+ return ;
233225 }
234226 else
235227 {
236228 ViewModel . LoginStepIndex = ( int ) LoginStep . ChooseProfile ;
237229 ViewModel . AppBarText = LoginManager . Instance . GetServerLoginResponseMessage ( ) ;
238230 }
239231 }
232+ private async Task LoadMainWindow ( UserProfile profile )
233+ {
234+ LoginManager . Instance . SetUserProfile ( profile ) ;
235+ SettingsViewModel . Instance . Init ( ) ;
236+ Preferences . Set ( AppConfigKey . UserID , LoginManager . Instance . GetCurrentUser ( ) ! . ID , profile . UserConfigFile ) ;
237+ ViewModel . StatusText = "Optimizing Cache..." ;
238+ await CacheHelper . OptimizeCache ( ) ;
239+ if ( ViewModel . RememberMe )
240+ {
241+ Preferences . Set ( AppConfigKey . LastUserProfile , profile . RootDir , ProfileManager . ProfileConfigFile ) ;
242+ }
243+ App . Current . MainWindow = new MainWindow ( ) ;
244+ if ( ! PipeServiceHandler . Instance . IsAppStartup && ! SettingsViewModel . Instance . BackgroundStart )
245+ {
246+ App . Current . MainWindow . Show ( ) ;
247+ }
248+ //First Startup Window Visibility will be determined by the protocol handler
249+ try
250+ {
251+ this . DialogResult = true ; //will throw error, if its called from the MainWindow
252+ }
253+ catch { }
254+ this . Close ( ) ;
255+ }
240256 private async void SaveAndSignUp_Click ( object sender , RoutedEventArgs e )
241257 {
242258 try
243259 {
244260 ValidateSignUpData ( ) ;
245- LoginState state = await LoginManager . Instance . Register ( ViewModel . SignupUser ) ;
261+ LoginState state = LoginState . Success ;
262+ if ( ! ViewModel . SignupUser . IsLoggedInWithOAuth )
263+ {
264+ state = await LoginManager . Instance . Register ( ViewModel . SignupUser ) ; //Only non-provider login has a additional call. Else its just a login, because the server will create the account internally
265+ }
246266 UserProfile profile = null ;
247267 if ( state != LoginState . Error )
248268 {
@@ -252,26 +272,9 @@ private async void SaveAndSignUp_Click(object sender, RoutedEventArgs e)
252272 ViewModel . AppBarText = "Failed to setup User Profile" ;
253273 return ;
254274 }
255- }
256- if ( state == LoginState . Success )
257- {
258275 await Login ( profile , true ) ;
259276 return ;
260277 }
261- else if ( state == LoginState . NotActivated )
262- {
263- ViewModel . LoginStepIndex = ( int ) LoginStep . PendingActivation ;
264- while ( ViewModel . LoginStepIndex == ( int ) LoginStep . PendingActivation )
265- {
266- LoginState loginState = await LoginManager . Instance . Login ( ViewModel . SignupUser . ServerUrl , ViewModel . SignupUser . Username , ViewModel . SignupUser . Password ) ;
267- if ( loginState == LoginState . Success && LoginManager . Instance . GetCurrentUser ( ) != null )
268- {
269- await Login ( profile , true ) ;
270- }
271- await Task . Delay ( 5000 ) ;
272- }
273- return ;
274- }
275278 ViewModel . AppBarText = LoginManager . Instance . GetServerLoginResponseMessage ( ) ;
276279 }
277280 catch ( Exception ex )
0 commit comments