@ -1,6 +1,8 @@
using eShopOnContainers.Core.Services.OpenUrl ;
using eShopOnContainers.Core.Services.Identity ;
using eShopOnContainers.Core.Services.OpenUrl ;
using eShopOnContainers.Core.Validations ;
using eShopOnContainers.ViewModels.Base ;
using IdentityModel.Client ;
using System ;
using System.Diagnostics ;
using System.Threading.Tasks ;
@ -13,13 +15,18 @@ namespace eShopOnContainers.Core.ViewModels
{
private ValidatableObject < string > _userName ;
private ValidatableObject < string > _password ;
private bool _isMock ;
private bool _isValid ;
private string _loginUrl ;
private IOpenUrlService _openUrlService ;
private IIdentityService _identityService ;
public LoginViewModel ( IOpenUrlService openUrlService )
public LoginViewModel ( IOpenUrlService openUrlService ,
IIdentityService identityService )
{
_openUrlService = openUrlService ;
_identityService = identityService ;
_userName = new ValidatableObject < string > ( ) ;
_password = new ValidatableObject < string > ( ) ;
@ -53,6 +60,19 @@ namespace eShopOnContainers.Core.ViewModels
}
}
public bool IsMock
{
get
{
return _isMock ;
}
set
{
_isMock = value ;
RaisePropertyChanged ( ( ) = > IsMock ) ;
}
}
public bool IsValid
{
get
@ -66,11 +86,35 @@ namespace eShopOnContainers.Core.ViewModels
}
}
public string LoginUrl
{
get
{
return _loginUrl ;
}
set
{
_loginUrl = value ;
RaisePropertyChanged ( ( ) = > LoginUrl ) ;
}
}
public ICommand MockSignInCommand = > new Command ( MockSignInAsync ) ;
public ICommand SignInCommand = > new Command ( SignInAsync ) ;
public ICommand RegisterCommand = > new Command ( Register ) ;
private async void SignInAsync ( )
public ICommand NavigateCommand = > new Command < string > ( NavigateAsync ) ;
public override Task InitializeAsync ( object navigationData )
{
LoginUrl = _identityService . CreateAuthorizeRequest ( ) ;
return base . InitializeAsync ( navigationData ) ;
}
private async void MockSignInAsync ( )
{
IsBusy = true ;
IsValid = true ;
@ -104,11 +148,42 @@ namespace eShopOnContainers.Core.ViewModels
IsBusy = false ;
}
private async void SignInAsync ( )
{
IsBusy = true ;
await Task . Delay ( 5 0 0 ) ;
IsValid = true ;
IsBusy = false ;
}
private void Register ( )
{
_openUrlService . OpenUrl ( GlobalSetting . RegisterWebsite ) ;
}
private async void NavigateAsync ( string url )
{
if ( url . Contains ( GlobalSetting . IdentityCallback ) )
{
// Parse response
var authResponse = new AuthorizeResponse ( url ) ;
if ( ! string . IsNullOrWhiteSpace ( authResponse . AccessToken ) )
{
string decodedTokens = _identityService . DecodeToken ( authResponse . AccessToken ) ;
if ( decodedTokens ! = null )
{
await NavigationService . NavigateToAsync < MainViewModel > ( ) ;
await NavigationService . RemoveLastFromBackStackAsync ( ) ;
}
}
}
}
private bool Validate ( )
{
bool isValidUser = _userName . Validate ( ) ;