/
LoginViewModel.cs
150 lines (130 loc) · 4.05 KB
/
LoginViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace ArcOrganization
{
using System;
using System.Threading.Tasks;
using System.Windows.Controls;
using ArcOrganization.Hub;
using ArcOrganization.Infrastructure.ViewModels;
using ArcOrganization.Services;
using Caliburn.Micro;
public class LoginViewModel : ScreenViewModel
{
private readonly INavigationService _navigationService;
private readonly PortalService _portalService;
private PasswordBox _passwordBox;
private string _username;
private bool _canLogin;
private string _errorMessage;
/// <summary>
/// Initializes a new instance of the <see cref="LoginViewModel"/> class.
/// </summary>
/// <param name="navigationService">
/// The navigation service.
/// </param>
/// <param name="portalService">
/// The portal service.
/// </param>
public LoginViewModel(INavigationService navigationService, PortalService portalService)
{
_navigationService = navigationService;
_portalService = portalService;
// Set default values
Username = "kajanus_dev";
CanLogin = true;
}
/// <summary>
/// Gets or sets the username.
/// </summary>
public string Username
{
get
{
return _username;
}
set
{
if (value.Equals(_username))
{
return;
}
_username = value;
NotifyOfPropertyChange(() => Username);
}
}
/// <summary>
/// Gets or sets a value indicating whether the user can execute Login process.
/// </summary>
public bool CanLogin
{
get
{
return _canLogin;
}
set
{
if (value.Equals(_canLogin))
{
return;
}
_canLogin = value;
NotifyOfPropertyChange(() => CanLogin);
}
}
/// <summary>
/// Gets or sets the error message for the UI.
/// </summary>
public string ErrorMessage
{
get
{
return _errorMessage;
}
set
{
if (value.Equals(_errorMessage))
{
return;
}
_errorMessage = value;
NotifyOfPropertyChange(() => ErrorMessage);
}
}
/// <summary>
/// Logs user into the application with given credentials.
/// </summary>
/// <param name="password"></param>
public async void Login(string password)
{
SetBusy("Logining...");
ErrorMessage = string.Empty;
CanLogin = false;
await LoginAndNavigateAsync();
}
private async Task LoginAndNavigateAsync()
{
try
{
// Generates token for the user and then naviates to Map view.
await _portalService.InitializeAsync(Username, _passwordBox.Password);
_navigationService.UriFor<HubViewModel>().WithParam(p => p.RemoveFromBackstackNavigation, true).Navigate();
SetNotBusy();
}
catch (Exception exception)
{
ErrorMessage = exception.Message;
CanLogin = true;
SetNotBusy();
}
}
/// <summary>
/// Executed when the view is loaded. This violates the MVVM pattern, but is used to keep password secured.
/// </summary>
/// <param name="view">The view.</param>
protected override void OnViewLoaded(object view)
{
_passwordBox = (view as LoginView).PasswordBox;
_passwordBox.Password = "PreviousCheckInIsChangedForNow:)";
base.OnViewLoaded(view);
}
}
}