Skip to content

Commit

Permalink
fix: Adding dispatcher as required
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrandolph committed Sep 13, 2022
1 parent 3db11c1 commit bc5cdbe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
[ReactiveBindable(false)]
public partial class MsalAuthenticationHomeViewModel : ObservableObject
{
public INavigator Navigator { get; init; }
public IAuthenticationService Authentication { get; init; }
private readonly IDispatcher _dispatcher;
private readonly INavigator _navigator;
private readonly IAuthenticationService _authentication;

public IMsalAuthenticationTaskListEndpoint TaskEndpoint { get; init; }
public ICustomAuthenticationDummyJsonEndpoint? Endpoint { get; init; }
private readonly IMsalAuthenticationTaskListEndpoint _taskEndpoint;
private readonly ICustomAuthenticationDummyJsonEndpoint? _endpoint;

public ITokenCache Tokens { get; }
private readonly ITokenCache _tokens;

[ObservableProperty]
private MsalAuthenticationToDoTaskListData[]? tasks;
Expand All @@ -19,43 +20,45 @@ public partial class MsalAuthenticationHomeViewModel : ObservableObject
private CustomAuthenticationProduct[]? products;

public MsalAuthenticationHomeViewModel(
IDispatcher dispatcher,
INavigator navigator,
IAuthenticationService auth,
ITokenCache tokens,
IMsalAuthenticationTaskListEndpoint taskEndpoint,
ICustomAuthenticationDummyJsonEndpoint? endpoint=null)
{
Navigator = navigator;
Authentication = auth;
TaskEndpoint = taskEndpoint;
Tokens = tokens;
Endpoint = endpoint;
_dispatcher = dispatcher;
_navigator = navigator;
_authentication = auth;
_taskEndpoint = taskEndpoint;
_tokens = tokens;
_endpoint = endpoint;
}

public async void Logout()
{
await Authentication.LogoutAsync(CancellationToken.None);
await Navigator.NavigateViewModelAsync<MsalAuthenticationWelcomeViewModel>(this, qualifier: Qualifiers.ClearBackStack);
await _authentication.LogoutAsync(_dispatcher, CancellationToken.None);
await _navigator.NavigateViewModelAsync<MsalAuthenticationWelcomeViewModel>(this, qualifier: Qualifiers.ClearBackStack);
}

public async void ClearAccessToken()
{
var creds = await Tokens.GetAsync(CancellationToken.None);
var creds = await _tokens.GetAsync(CancellationToken.None);
creds.Remove(TokenCacheExtensions.AccessTokenKey);
await Tokens.SaveAsync(await Tokens.GetCurrentProviderAsync(CancellationToken.None) ?? string.Empty, creds, CancellationToken.None);
await _tokens.SaveAsync(await _tokens.GetCurrentProviderAsync(CancellationToken.None) ?? string.Empty, creds, CancellationToken.None);
}

public async void Retrieve()
{
var current = await Tokens.GetCurrentProviderAsync(CancellationToken.None);
var current = await _tokens.GetCurrentProviderAsync(CancellationToken.None);
if (current?.StartsWith("Custom") ?? false)
{
var response = await Endpoint!.Products(CancellationToken.None);
var response = await _endpoint!.Products(CancellationToken.None);
Products = response?.Products?.ToArray();
}
else
{
var tasksResponse = await TaskEndpoint.GetAllAsync(CancellationToken.None);
var tasksResponse = await _taskEndpoint.GetAllAsync(CancellationToken.None);
Tasks = tasksResponse?.Value?.ToArray();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TestHarness.Ext.Authentication.Custom;

namespace TestHarness.Ext.Authentication.MSAL;
namespace TestHarness.Ext.Authentication.MSAL;

public class MsalAuthenticationMultiHostInit : BaseMsalHostInitialization
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TestHarness.Ext.Authentication.MSAL;

public class MsalAuthenticationSettingsHostInit : BaseHostInitialization
public class MsalAuthenticationSettingsHostInit : BaseMsalHostInitialization
{
protected override string[] ConfigurationFiles => new string[] { "TestHarness.Ext.Authentication.Msal.appsettings.msalauthentication.json",
"TestHarness.Ext.Authentication.Msal.appsettings.msal.json"};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TestHarness.Ext.Authentication.MSAL;

internal record MsalAuthenticationWelcomeViewModel(INavigator Navigator, IAuthenticationService auth, IAuthenticationService Authentication, IAuthenticationRouteInfo RouteInfo)
internal record MsalAuthenticationWelcomeViewModel(IDispatcher Dispatcher, INavigator Navigator, IAuthenticationService auth, IAuthenticationService Authentication, IAuthenticationRouteInfo RouteInfo)
{
public string[] Providers => Authentication.Providers;

Expand All @@ -19,7 +19,7 @@ public async void Login()
{nameof(CustomAuthenticationCredentials.Password),DummyJsonEndpointConstants.ValidPassword }
} :
default;
var authenticated =await Authentication.LoginAsync(credentials: creds, provider: SelectedProvider);
var authenticated =await Authentication.LoginAsync(Dispatcher, credentials: creds, provider: SelectedProvider);
if (authenticated)
{
await Navigator.NavigateViewModelAsync(this, RouteInfo.HomeViewModel, qualifier: Qualifiers.ClearBackStack);
Expand Down

0 comments on commit bc5cdbe

Please sign in to comment.