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.Threading.Tasks;
using Wabbajack.Common;
namespace Wabbajack
{
@ -12,9 +13,15 @@ namespace Wabbajack
await task.ConfigureAwait(false);
}
catch (Exception ex)
when (onException != null)
{
onException(ex);
if (onException == null)
{
Utils.Error(ex);
}
else
{
onException(ex);
}
}
}

View File

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

View File

@ -43,7 +43,7 @@ namespace Wabbajack.Lib.Downloaders
public BethesdaNetDownloader()
{
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()

View File

@ -1,6 +1,7 @@
using System;
using System.Reactive;
using System.Security;
using System.Threading.Tasks;
using ReactiveUI;
namespace Wabbajack.Lib.Downloaders
@ -39,6 +40,6 @@ namespace Wabbajack.Lib.Downloaders
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;
@ -57,7 +57,7 @@ namespace Wabbajack.Lib.Downloaders
{
MegaApiClient.Logout();
}
catch (NotSupportedException ex)
catch (NotSupportedException)
{
// Not logged in, so ignore
}
@ -101,7 +101,7 @@ namespace Wabbajack.Lib.Downloaders
if (MegaApiClient.IsLoggedIn)
{
var infos = MEGAAuthInfos.ToMEGAAuthInfos(authInfos);
infos.ToEcryptedJson(DataName);
await infos.ToEcryptedJson(DataName);
}
return new LoginReturnMessage("Logged in successfully, you can now close this window.",

View File

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

View File

@ -1,5 +1,6 @@
using System.Reactive.Disposables;
using System.Windows;
using MahApps.Metro.Controls;
using ReactiveUI;
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();
}
}
}