Added some missing awaits

This commit is contained in:
Justin Swanson 2020-06-26 12:54:44 -05:00
parent 8548c0c618
commit 64cfba67a5
7 changed files with 38 additions and 13 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Wabbajack.Common;
namespace Wabbajack namespace Wabbajack
{ {
@ -12,11 +13,17 @@ namespace Wabbajack
await task.ConfigureAwait(false); await task.ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
when (onException != null) {
if (onException == null)
{
Utils.Error(ex);
}
else
{ {
onException(ex); onException(ex);
} }
} }
}
/// <summary> /// <summary>
/// returns a Task that will await the input task, but fire an action if it takes longer than a given time /// returns a Task that will await the input task, but fire an action if it takes longer than a given time

View File

@ -57,8 +57,8 @@ namespace Wabbajack.Lib.Downloaders
TriggerLogin = ReactiveCommand.CreateFromTask( TriggerLogin = ReactiveCommand.CreateFromTask(
execute: () => Utils.CatchAndLog(async () => await Utils.Log(new RequestSiteLogin(this)).Task), execute: () => Utils.CatchAndLog(async () => await Utils.Log(new RequestSiteLogin(this)).Task),
canExecute: IsLoggedIn.Select(b => !b).ObserveOnGuiThread()); canExecute: IsLoggedIn.Select(b => !b).ObserveOnGuiThread());
ClearLogin = ReactiveCommand.Create( ClearLogin = ReactiveCommand.CreateFromTask(
execute: () => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson(_encryptedKeyName)), execute: () => Utils.CatchAndLog(async () => await Utils.DeleteEncryptedJson(_encryptedKeyName)),
canExecute: IsLoggedIn.ObserveOnGuiThread()); canExecute: IsLoggedIn.ObserveOnGuiThread());
} }

View File

@ -43,7 +43,7 @@ namespace Wabbajack.Lib.Downloaders
public BethesdaNetDownloader() public BethesdaNetDownloader()
{ {
TriggerLogin = ReactiveCommand.CreateFromTask(() => Utils.CatchAndLog(RequestLoginAndCache), IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler)); TriggerLogin = ReactiveCommand.CreateFromTask(() => Utils.CatchAndLog(RequestLoginAndCache), IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler));
ClearLogin = ReactiveCommand.Create(() => Utils.CatchAndLog(() =>Utils.DeleteEncryptedJson(DataName)), IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler)); ClearLogin = ReactiveCommand.CreateFromTask(() => Utils.CatchAndLog(async () => await Utils.DeleteEncryptedJson(DataName)), IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler));
} }
private static async Task RequestLoginAndCache() private static async Task RequestLoginAndCache()

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Reactive; using System.Reactive;
using System.Security; using System.Security;
using System.Threading.Tasks;
using ReactiveUI; using ReactiveUI;
namespace Wabbajack.Lib.Downloaders namespace Wabbajack.Lib.Downloaders
@ -39,6 +40,6 @@ namespace Wabbajack.Lib.Downloaders
public interface INeedsLoginCredentials : INeedsLogin public interface INeedsLoginCredentials : INeedsLogin
{ {
LoginReturnMessage LoginWithCredentials(string username, SecureString password, string? mfa); Task<LoginReturnMessage> LoginWithCredentials(string username, SecureString password, string? mfa);
} }
} }

View File

@ -49,7 +49,7 @@ namespace Wabbajack.Lib.Downloaders
} }
} }
public LoginReturnMessage LoginWithCredentials(string username, SecureString password, string? mfa) public async Task<LoginReturnMessage> LoginWithCredentials(string username, SecureString password, string? mfa)
{ {
MegaApiClient.AuthInfos authInfos; MegaApiClient.AuthInfos authInfos;
@ -57,7 +57,7 @@ namespace Wabbajack.Lib.Downloaders
{ {
MegaApiClient.Logout(); MegaApiClient.Logout();
} }
catch (NotSupportedException ex) catch (NotSupportedException)
{ {
// Not logged in, so ignore // Not logged in, so ignore
} }
@ -101,7 +101,7 @@ namespace Wabbajack.Lib.Downloaders
if (MegaApiClient.IsLoggedIn) if (MegaApiClient.IsLoggedIn)
{ {
var infos = MEGAAuthInfos.ToMEGAAuthInfos(authInfos); var infos = MEGAAuthInfos.ToMEGAAuthInfos(authInfos);
infos.ToEcryptedJson(DataName); await infos.ToEcryptedJson(DataName);
} }
return new LoginReturnMessage("Logged in successfully, you can now close this window.", return new LoginReturnMessage("Logged in successfully, you can now close this window.",

View File

@ -2,6 +2,7 @@
using System.Net.Mail; using System.Net.Mail;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Security; using System.Security;
using System.Threading.Tasks;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Wabbajack.Common; using Wabbajack.Common;
@ -21,6 +22,9 @@ namespace Wabbajack
[Reactive] [Reactive]
public LoginReturnMessage ReturnMessage { get; set; } public LoginReturnMessage ReturnMessage { get; set; }
[Reactive]
public bool LoggingIn { get; private set; }
private readonly ObservableAsPropertyHelper<bool> _loginEnabled; private readonly ObservableAsPropertyHelper<bool> _loginEnabled;
public bool LoginEnabled => _loginEnabled.Value; public bool LoginEnabled => _loginEnabled.Value;
@ -35,6 +39,12 @@ namespace Wabbajack
_loginEnabled = this.WhenAny(x => x.Username) _loginEnabled = this.WhenAny(x => x.Username)
.Select(IsValidAddress) .Select(IsValidAddress)
.CombineLatest(
this.WhenAny(x => x.LoggingIn),
(valid, loggingIn) =>
{
return valid && !loggingIn;
})
.ToGuiProperty(this, .ToGuiProperty(this,
nameof(LoginEnabled)); nameof(LoginEnabled));
@ -43,17 +53,19 @@ namespace Wabbajack
.ToGuiProperty(this, nameof(MFAVisible)); .ToGuiProperty(this, nameof(MFAVisible));
} }
public void Login(SecureString password) public async Task Login(SecureString password)
{ {
try try
{ {
LoggingIn = true;
if (password == null || password.Length == 0) if (password == null || password.Length == 0)
{ {
ReturnMessage = new LoginReturnMessage("You need to input a password!", LoginReturnCode.BadInput); ReturnMessage = new LoginReturnMessage("You need to input a password!", LoginReturnCode.BadInput);
return; return;
} }
ReturnMessage = _downloader.LoginWithCredentials(Username, password, string.IsNullOrWhiteSpace(MFAKey) ? null : MFAKey); ReturnMessage = await _downloader.LoginWithCredentials(Username, password, string.IsNullOrWhiteSpace(MFAKey) ? null : MFAKey);
password.Clear(); password.Clear();
} }
catch (Exception e) catch (Exception e)
@ -61,6 +73,10 @@ namespace Wabbajack
Utils.Error(e, "Exception while trying to login"); Utils.Error(e, "Exception while trying to login");
ReturnMessage = new LoginReturnMessage($"Unhandled exception: {e.Message}", LoginReturnCode.InternalError); ReturnMessage = new LoginReturnMessage($"Unhandled exception: {e.Message}", LoginReturnCode.InternalError);
} }
finally
{
LoggingIn = false;
}
} }
private static bool IsValidAddress(string s) private static bool IsValidAddress(string s)

View File

@ -1,5 +1,6 @@
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Windows; using System.Windows;
using MahApps.Metro.Controls;
using ReactiveUI; using ReactiveUI;
using Wabbajack.Lib.Downloaders; using Wabbajack.Lib.Downloaders;
@ -35,9 +36,9 @@ namespace Wabbajack
}); });
} }
private void LoginButton_OnClick(object sender, RoutedEventArgs e) private async void LoginButton_OnClick(object sender, RoutedEventArgs e)
{ {
ViewModel.Login(Password.SecurePassword); ViewModel.Login(Password.SecurePassword).FireAndForget();
} }
} }
} }