Skip to content

Commit 6540ec2

Browse files
committed
Added PendingActivation for login
1 parent 733c3fa commit 6540ec2

6 files changed

Lines changed: 85 additions & 66 deletions

File tree

gamevault/Helper/LoginManager.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ private LoginState DetermineLoginState(string code)
412412
{
413413
return LoginState.Forbidden;
414414
}
415-
case "204":
415+
case "406":
416416
{
417417
return LoginState.NotActivated;
418418
}
@@ -439,8 +439,7 @@ public void StopOnlineTimer()
439439
private async void CheckOnlineStatus(object sender, EventArgs e)
440440
{
441441
try
442-
{
443-
string serverResonse = await WebHelper.GetAsync(@$"{SettingsViewModel.Instance.ServerUrl}/api/status");
442+
{
444443
if (!IsLoggedIn())
445444
{
446445
bool isLoggedInWithOAuth = Preferences.Get(AppConfigKey.IsLoggedInWithOAuth, GetUserProfile().UserConfigFile) == "1";
@@ -449,13 +448,18 @@ private async void CheckOnlineStatus(object sender, EventArgs e)
449448
string[] credencials = WebHelper.GetCredentials();
450449
await Login(GetUserProfile().ServerUrl, credencials[0], credencials[1]);
451450
}
451+
else
452+
{
453+
//To Do: Login by Provider but make sure, the Auth window will not be opened twice
454+
}
452455
if (IsLoggedIn())
453456
{
454457
MainWindowViewModel.Instance.OnlineState = System.Windows.Visibility.Collapsed;
455458
}
456459
}
457460
else
458461
{
462+
await WebHelper.GetAsync(@$"{SettingsViewModel.Instance.ServerUrl}/api/status");
459463
MainWindowViewModel.Instance.OnlineState = System.Windows.Visibility.Collapsed;
460464
}
461465
}

gamevault/Helper/Web/OAuthHttpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public async Task<bool> LoginBasicAuthAsync(string username, string password)
4848

4949
var response = await _httpClient.GetAsync($"{ServerUrl}/api/auth/basic/login");
5050

51-
if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NoContent)
52-
{
51+
if (!response.IsSuccessStatusCode)
52+
{
5353
using JsonDocument serverResponseJson = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
5454
throw new HttpRequestException(serverResponseJson.RootElement.GetProperty("message").GetString(), null, response.StatusCode);
5555
}

gamevault/UserControls/SettingsComponents/InstallLocationUserControl.xaml.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void UserControl_Loaded(object sender, RoutedEventArgs e)
3737
if (loaded) return;
3838
loaded = true;
3939
string lastSelectedRootDirectory = Preferences.Get(AppConfigKey.LastSelectedRootDirectory, LoginManager.Instance.GetUserProfile().UserConfigFile);
40-
if (AutoConfirmIfWindowIsHidden(lastSelectedRootDirectory))//possible if called by CLI
40+
if (AutoConfirmIfWindowIsHiddenOrOnlyOneEntry(lastSelectedRootDirectory))
4141
return;
4242

4343
if (Directory.Exists(lastSelectedRootDirectory))
@@ -63,6 +63,10 @@ private void PrepareInstallLocationSelection()
6363
}
6464
}
6565
}
66+
if (RootDirectories.Count <= 1)
67+
{
68+
this.Visibility = Visibility.Collapsed;
69+
}
6670
}
6771
private string FormatBytes(long bytes)
6872
{
@@ -106,9 +110,9 @@ private void Install_Click(object sender, RoutedEventArgs e)
106110
}
107111
catch (Exception ex) { }
108112
}
109-
private bool AutoConfirmIfWindowIsHidden(string lastSelectedRootDirectory)
113+
private bool AutoConfirmIfWindowIsHiddenOrOnlyOneEntry(string lastSelectedRootDirectory)
110114
{
111-
if (App.Instance.MainWindow.IsVisible == false)
115+
if (App.Instance.MainWindow.IsVisible == false && RootDirectories.Any())//possible if called by CLI
112116
{
113117
if (Directory.Exists(lastSelectedRootDirectory))
114118
{
@@ -118,10 +122,16 @@ private bool AutoConfirmIfWindowIsHidden(string lastSelectedRootDirectory)
118122
else
119123
{
120124
MainWindowViewModel.Instance.ClosePopup();
121-
ResultTaskSource.TrySetResult(RootDirectories.ElementAt(0).Value);
125+
ResultTaskSource.TrySetResult(RootDirectories.ElementAt(0).Key.Uri);
122126
}
123127
return true;
124128
}
129+
else if (RootDirectories.Count == 1)
130+
{
131+
MainWindowViewModel.Instance.ClosePopup();
132+
ResultTaskSource.TrySetResult(RootDirectories.ElementAt(0).Key.Uri);
133+
return true;
134+
}
125135
return false;
126136
}
127137
private void DirectorySettings_Click(object sender, RoutedEventArgs e)

gamevault/UserControls/SettingsUserControl.xaml.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,29 @@ private void ClearImageCache_Clicked(object sender, RoutedEventArgs e)
5959
}
6060

6161
}
62-
private void ClearOfflineCache_Clicked(object sender, RoutedEventArgs e)
62+
private async void ClearOfflineCache_Clicked(object sender, RoutedEventArgs e)
6363
{
64-
65-
try
64+
MessageDialogResult result = await ((MetroWindow)App.Current.MainWindow).ShowMessageAsync($"Are you sure you want delete the offline cache? \nThis can lead to games not being displayed correctly when you are offline.", "", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "Yes", NegativeButtonText = "No", AnimateHide = false });
65+
if (result == MessageDialogResult.Affirmative)
6666
{
67-
if (File.Exists(LoginManager.Instance.GetUserProfile().IgnoreList))
67+
try
6868
{
69-
File.Delete(LoginManager.Instance.GetUserProfile().IgnoreList);
69+
if (File.Exists(LoginManager.Instance.GetUserProfile().IgnoreList))
70+
{
71+
File.Delete(LoginManager.Instance.GetUserProfile().IgnoreList);
72+
}
73+
if (File.Exists(LoginManager.Instance.GetUserProfile().OfflineCache))
74+
{
75+
File.Delete(LoginManager.Instance.GetUserProfile().OfflineCache);
76+
}
77+
ViewModel.OfflineCacheSize = 0;
78+
MainWindowViewModel.Instance.AppBarText = "Offline cache cleared";
7079
}
71-
if (File.Exists(LoginManager.Instance.GetUserProfile().OfflineCache))
80+
catch
7281
{
73-
File.Delete(LoginManager.Instance.GetUserProfile().OfflineCache);
82+
MainWindowViewModel.Instance.AppBarText = "Something went wrong while the offline cache was cleared";
7483
}
75-
ViewModel.OfflineCacheSize = 0;
76-
MainWindowViewModel.Instance.AppBarText = "Offline cache cleared";
7784
}
78-
catch
79-
{
80-
MainWindowViewModel.Instance.AppBarText = "Something went wrong while the offline cache was cleared";
81-
}
82-
8385
}
8486
private async void UserControl_Loaded(object sender, RoutedEventArgs e)
8587
{

gamevault/ViewModels/LoginWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public LoginWindowViewModel()
1919
{
2020
}
2121

22-
private int loginStepIndex = 0;
22+
private int loginStepIndex { get; set; } = 0;
2323
public int LoginStepIndex
2424
{
2525
get { return loginStepIndex; }

gamevault/Windows/LoginWindow.xaml.cs

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Diagnostics;
1313
using System.Runtime.InteropServices;
1414
using System.Text.Json.Nodes;
15+
using static System.Runtime.InteropServices.JavaScript.JSType;
1516

1617

1718
namespace 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

Comments
 (0)