2021-05-13 20:03:07 +02:00
using System.Windows.Input ;
using Xamarin.Forms ;
using TINK.View ;
using TINK.Model.User ;
using TINK.Model.User.Account ;
using System.ComponentModel ;
using System ;
using System.Threading.Tasks ;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Exception ;
2021-05-13 20:03:07 +02:00
using Serilog ;
using TINK.ViewModel.Map ;
using Plugin.Connectivity ;
using TINK.Model ;
using System.Linq ;
using System.Collections.Generic ;
using TINK.MultilingualResources ;
2021-06-26 20:57:55 +02:00
using TINK.ViewModel.Info ;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel
{
public class LoginPageViewModel : INotifyPropertyChanged
{
/// <summary>
2021-08-01 17:24:15 +02:00
/// Reference on view service to show modal notifications and to perform navigation.
2021-05-13 20:03:07 +02:00
/// </summary>
2021-06-26 20:57:55 +02:00
private readonly IViewService m_oViewService ;
2021-05-13 20:03:07 +02:00
#if BACKSTYLE
/// <summary> Reference to naviagion object to navigate back to map page when login succeeded. </summary>
private INavigation m_oNavigation ;
#endif
/// <summary> Reference on the tink app instance. </summary>
private ITinkApp TinkApp { get ; }
/// <summary>
/// Holds the mail address candidate being entered by user.
/// </summary>
private string m_strMailAddress ;
/// <summary>
/// Holds the password candidate being entered by user.
/// </summary>
private string m_strPassword ;
private bool m_bMailAndPasswordCandidatesOk = false ;
/// <summary> Holds a reference to the external trigger service. </summary>
private Action < string > OpenUrlInExternalBrowser { get ; }
/// <summary>
/// Event which notifies clients about changed properties.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged ;
/// <summary>
///
/// </summary>
/// <param name="p_oSetCredentials">Delegate to set new mail address and password to model.</param>
/// <param name="p_oUser">Mail address of user if login succeeded.</param>
/// <param name="openUrlInExternalBrowser">Action to open an external browser.</param>
/// <param name="p_oViewService">Interface to actuate methodes on GUI.</param>
/// <param name="p_oNavigation">Interface to navigate.</param>
public LoginPageViewModel (
ITinkApp tinkApp ,
Action < string > openUrlInExternalBrowser ,
#if ! BACKSTYLE
IViewService p_oViewService )
#else
IViewService p_oViewService ,
INavigation p_oNavigation )
#endif
{
TinkApp = tinkApp
? ? throw new ArgumentException ( "Can not instantiate map page view model- object. No tink app object available." ) ;
OpenUrlInExternalBrowser = openUrlInExternalBrowser
? ? throw new ArgumentException ( "Can not instantiate login page view model- object. No user external browse service available." ) ;
m_oViewService = p_oViewService
? ? throw new ArgumentException ( "Can not instantiate login page view model- object. No view available." ) ;
#if BACKSTYLE
m_oNavigation = p_oNavigation
? ? throw new ArgumentException ( "Can not instantiate login page view model- object. No navigation service available." ) ;
#endif
m_strMailAddress = tinkApp . ActiveUser . Mail ;
m_strPassword = tinkApp . ActiveUser . Password ;
IsRegisterTargetsInfoVisible = new List < string > { Model . Services . CopriApi . ServerUris . CopriServerUriList . TINK_LIVE , Model . Services . CopriApi . ServerUris . CopriServerUriList . TINK_DEVEL } . Contains ( tinkApp . Uris . ActiveUri . AbsoluteUri ) ;
tinkApp . ActiveUser . StateChanged + = OnStateChanged ;
}
/// <summary>
/// Login state changed.
/// </summary>
/// <param name="p_oSender"></param>
/// <param name="p_oEventArgs"></param>
private void OnStateChanged ( object p_oSender , EventArgs p_oEventArgs )
{
var l_oPropertyChanged = PropertyChanged ;
if ( l_oPropertyChanged ! = null )
{
l_oPropertyChanged ( this , new PropertyChangedEventArgs ( nameof ( IsLoggedOut ) ) ) ;
l_oPropertyChanged ( this , new PropertyChangedEventArgs ( nameof ( IsLoginRequestAllowed ) ) ) ;
}
}
/// <summary> Gets a value indicating whether user is logged out or not.</summary>
public bool IsLoggedOut { get { return ! TinkApp . ActiveUser . IsLoggedIn ; } }
/// <summary> Gets a value indicating whether user can try to login.</summary>
public bool IsLoginRequestAllowed
{
get
{
return ! TinkApp . ActiveUser . IsLoggedIn
& & m_bMailAndPasswordCandidatesOk ;
}
}
/// <summary> Gets mail address of user.</summary>
public string MailAddress
{
get
{
return m_strMailAddress ;
}
set
{
m_strMailAddress = value ;
UpdateAndFireIfRequiredOnEnteringMailOrPwd ( ) ;
}
}
/// <summary> Gets password user.</summary>
public string Password
{
get
{
return m_strPassword ;
}
set
{
m_strPassword = value ;
UpdateAndFireIfRequiredOnEnteringMailOrPwd ( ) ;
}
}
/// <summary>Update on password or mailaddress set. </summary>
private void UpdateAndFireIfRequiredOnEnteringMailOrPwd ( )
{
var l_bLastMailAndPasswordCandidatesOk = m_bMailAndPasswordCandidatesOk ;
m_bMailAndPasswordCandidatesOk = Validator . ValidateMailAndPasswordDelegate ( MailAddress , Password ) . ValidElement = = Elements . Account ;
if ( m_bMailAndPasswordCandidatesOk ! = l_bLastMailAndPasswordCandidatesOk )
{
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( IsLoginRequestAllowed ) ) ) ;
}
}
/// <summary>
/// Command object to bind login button to view model.
/// </summary>
public ICommand OnLoginRequest
{
get
{
2021-06-26 20:57:55 +02:00
return new Command ( async ( ) = > await Login ( ) ) ;
2021-05-13 20:03:07 +02:00
}
}
/// <summary> Processes request to register a new user. </summary>
public ICommand OnRegisterRequest = > new Command ( async ( ) = >
{
if ( CrossConnectivity . Current . IsConnected )
{
2021-08-01 17:24:15 +02:00
await m_oViewService . PushAsync ( ViewTypes . RegisterPage ) ;
2021-05-13 20:03:07 +02:00
}
else
{
await m_oViewService . DisplayAlert (
AppResources . MessageTitleHint ,
AppResources . MessageLoginRegisterNoNet ,
AppResources . MessageAnswerOk ) ;
}
} ) ;
/// <summary> Processes request to recover password. </summary>
public ICommand OnPasswordForgottonRequest = > new Command ( async ( ) = >
{
if ( CrossConnectivity . Current . IsConnected )
{
await m_oViewService . PushAsync ( ViewTypes . PasswordForgottenPage ) ;
}
else
{
await m_oViewService . DisplayAlert (
AppResources . MessageTitleHint ,
AppResources . MessageLoginRecoverPassword ,
AppResources . MessageAnswerOk ) ;
}
} ) ;
/// <summary>
/// User request to log in.
/// </summary>
#if BACKSTYLE
2021-06-26 20:57:55 +02:00
public async void Login ( )
2021-05-13 20:03:07 +02:00
#else
2021-06-26 20:57:55 +02:00
public async Task Login ( )
2021-05-13 20:03:07 +02:00
#endif
{
2022-01-04 18:59:16 +01:00
IAccount account = new EmptyAccount ( ) ;
2021-05-13 20:03:07 +02:00
try
{
Log . ForContext < LoginPageViewModel > ( ) . Information ( "User taped login button." ) ;
try
{
TinkApp . ActiveUser . CheckIsPasswordValid ( MailAddress , Password ) ;
// Do login.
2022-01-04 18:59:16 +01:00
account = await TinkApp . GetConnector ( CrossConnectivity . Current . IsConnected ) . Command . DoLogin ( MailAddress , Password , TinkApp . ActiveUser . DeviceId ) ;
2021-05-13 20:03:07 +02:00
2022-01-04 18:59:16 +01:00
await TinkApp . ActiveUser . Login ( account ) ;
2021-05-13 20:03:07 +02:00
// Update map page filter because user might be of both groups TINK an Konrad.
TinkApp . GroupFilterMapPage =
GroupFilterMapPageHelper . CreateUpdated (
TinkApp . GroupFilterMapPage ,
TinkApp . ActiveUser . DoFilter ( TinkApp . FilterGroupSetting . DoFilter ( ) ) ) ;
// Update settings page filter because user might be of both groups TINK an Konrad.
TinkApp . FilterGroupSetting . DoFilter ( TinkApp . ActiveUser . Group ) ;
// Persist new settings.
TinkApp . Save ( ) ;
TinkApp . UpdateConnector ( ) ;
}
catch ( InvalidAuthorizationResponseException l_oException )
{
// Copri response is invalid.
Log . ForContext < LoginPageViewModel > ( ) . Error ( "Login failed (invalid. auth. response). {@l_oException}." , l_oException ) ;
await m_oViewService . DisplayAlert (
AppResources . MessageLoginErrorTitle ,
l_oException . Message ,
AppResources . MessageAnswerOk ) ;
return ;
}
2021-06-26 20:57:55 +02:00
catch ( UnsupportedCopriVersionDetectedException )
{
await m_oViewService . DisplayAlert (
AppResources . MessageLoginErrorTitle ,
string . Format ( AppResources . MessageAppVersionIsOutdated , ContactPageViewModel . GetAppName ( TinkApp . Uris . ActiveUri ) ) ,
AppResources . MessageAnswerOk ) ;
return ;
}
2021-05-13 20:03:07 +02:00
catch ( Exception l_oException )
{
// Copri server is not reachable.
if ( l_oException is WebConnectFailureException )
{
Log . ForContext < LoginPageViewModel > ( ) . Information ( "Login failed (web communication exception). {@l_oException}." , l_oException ) ;
await m_oViewService . DisplayAlert (
AppResources . MessageLoginConnectionErrorTitle ,
string . Format ( "{0}\r\n{1}" , l_oException . Message , WebConnectFailureException . GetHintToPossibleExceptionsReasons ) ,
AppResources . MessageAnswerOk ) ;
} else if ( l_oException is UsernamePasswordInvalidException )
{
// Cookie is empty.
Log . ForContext < LoginPageViewModel > ( ) . Error ( "Login failed (empty cookie). {@l_oException}." , l_oException ) ;
await m_oViewService . DisplayAlert (
AppResources . MessageLoginErrorTitle ,
string . Format ( AppResources . MessageLoginConnectionErrorMessage , l_oException . Message ) ,
"OK" ) ;
}
else
{
Log . ForContext < LoginPageViewModel > ( ) . Error ( "Login failed. {@l_oException}." , l_oException ) ;
await m_oViewService . DisplayAlert (
AppResources . MessageLoginErrorTitle ,
l_oException . Message ,
AppResources . MessageAnswerOk ) ;
}
return ;
}
// Display information that login succeeded.
Log . ForContext < LoginPageViewModel > ( ) . Information ( "Login succeeded. {@tinkApp.ActiveUser}." , TinkApp . ActiveUser ) ;
2022-01-04 18:54:03 +01:00
var title = TinkApp . ActiveUser . Group . Intersect ( new List < string > { Model . Connector . FilterHelper . CARGOBIKE , Model . Connector . FilterHelper . CITYBIKE } ) . Any ( )
2021-05-13 20:03:07 +02:00
? string . Format ( AppResources . MessageLoginWelcomeTitleGroup , TinkApp . ActiveUser . GetUserGroupDisplayName ( ) )
: string . Format ( AppResources . MessageLoginWelcomeTitle ) ;
await m_oViewService . DisplayAlert (
title ,
string . Format ( AppResources . MessageLoginWelcome , TinkApp . ActiveUser . Mail ) ,
AppResources . MessageAnswerOk ) ;
}
catch ( Exception p_oException )
{
Log . ForContext < LoginPageViewModel > ( ) . Error ( "An unexpected error occurred displaying log out page. {@Exception}" , p_oException ) ;
return ;
}
try
{
2022-01-04 18:54:03 +01:00
if ( ! TinkApp . ActiveUser . Group . Contains ( Model . Connector . FilterHelper . CARGOBIKE ) )
2021-05-13 20:03:07 +02:00
{
// No need to show "Anleitung TINK Räder" because user can not use tink.
2021-08-28 10:04:10 +02:00
#if USEFLYOUT
2022-01-04 18:59:16 +01:00
m_oViewService . ShowPage ( account . IsAgbAcknowledged ? ViewTypes . MapPage : ViewTypes . ManageAccountPage ) ;
2021-06-26 20:57:55 +02:00
#else
await m_oViewService . ShowPage ( "//MapPage" ) ;
#endif
2021-05-13 20:03:07 +02:00
return ;
}
// Swich to map page
2021-08-28 10:04:10 +02:00
#if USEFLYOUT
2021-05-13 20:03:07 +02:00
m_oViewService . ShowPage ( ViewTypes . BikeInfoCarouselPage , AppResources . MarkingLoginInstructions ) ;
2021-06-26 20:57:55 +02:00
#else
await m_oViewService . ShowPage ( "//MapPage" ) ;
#endif
2021-05-13 20:03:07 +02:00
}
catch ( Exception p_oException )
{
Log . ForContext < LoginPageViewModel > ( ) . Error ( "Ein unerwarteter Fehler ist auf der Seite Anleitung TINK Räder (nach Anmeldeseite) aufgetreten. {@Exception}" , p_oException ) ;
return ;
}
#if BACKSTYLE
// Navigate back to map page.
await m_oNavigation . PopToRootAsync ( ) ;
#endif
}
/// <summary> Holds whether TINK/ Copri info is shown.</summary>
public bool IsRegisterTargetsInfoVisible { get ; private set ; }
/// <summary> Text providing info about TINK/ konrad registration. </summary>
public FormattedString RegisterTargetsInfo
{
get
{
var l_oHint = new FormattedString ( ) ;
l_oHint . Spans . Add ( new Span { Text = AppResources . MarkingLoginInstructionsTinkKonradTitle } ) ;
l_oHint . Spans . Add ( new Span { Text = AppResources . MarkingLoginInstructionsTinkKonradMessage } ) ;
return l_oHint ;
}
}
/// <summary> Opens login page.</summary>
public void RegisterRequest ( string url )
{
try
{
OpenUrlInExternalBrowser ( url ) ;
}
catch ( Exception p_oException )
{
Log . Error ( "Ein unerwarteter Fehler ist auf der Login Seite beim Öffnen eines Browsers, Seite {url}, aufgetreten. {@Exception}" , url , p_oException ) ;
return ;
}
}
}
}