Subversion Repository Public Repository

Nextrek

Diff Revisions 527 vs 660 for /Android/SmartCharging/SmartCharging_WP/SmartCharging/LoginPage.xaml.cs

Diff revisions: vs.
  @@ -1,12 +1,17 @@
1 1 using SmartCharging.Common;
2 + using SmartCharging.DataModel;
3 + using SmartCharging.Net;
2 4 using System;
3 5 using System.Collections.Generic;
4 6 using System.IO;
5 7 using System.Linq;
6 8 using System.Runtime.InteropServices.WindowsRuntime;
9 + using System.Threading.Tasks;
7 10 using Windows.Foundation;
8 11 using Windows.Foundation.Collections;
9 12 using Windows.Graphics.Display;
13 + using Windows.Security.Credentials;
14 + using Windows.UI;
10 15 using Windows.UI.Core;
11 16 using Windows.UI.ViewManagement;
12 17 using Windows.UI.Xaml;
  @@ -28,6 +33,7 @@
28 33 {
29 34 private NavigationHelper navigationHelper;
30 35 private ObservableDictionary defaultViewModel = new ObservableDictionary();
36 + private SmartChargeAPI SMA;
31 37
32 38 public LoginPage()
33 39 {
  @@ -36,6 +42,8 @@
36 42 this.navigationHelper = new NavigationHelper(this);
37 43 this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
38 44 this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
45 + this.SMA = SmartChargeAPI.Instance;
46 +
39 47 }
40 48
41 49 /// <summary>
  @@ -66,8 +74,28 @@
66 74 /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
67 75 /// a dictionary of state preserved by this page during an earlier
68 76 /// session. The state will be null the first time a page is visited.</param>
69 - private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
77 + private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
70 78 {
79 + CredentialStorage cs = new CredentialStorage();
80 + PasswordCredential credentials = cs.GetCredential();
81 + this.hideLoading();
82 + // autologin
83 + if (credentials != null)
84 + {
85 + this.showLoading();
86 + credentials.RetrievePassword();
87 + User user = await this.SMA.Login(credentials.UserName, credentials.Password);
88 + this.hideLoading();
89 + if (user != null)
90 + {
91 + this.goToUserPage();
92 + }
93 + else
94 + {
95 + cs.RemoveCredentials();
96 + }
97 + }
98 +
71 99 }
72 100
73 101 /// <summary>
  @@ -109,22 +137,70 @@
109 137
110 138 #endregion
111 139
112 - private void LoginButton_Tapped(object sender, TappedRoutedEventArgs e)
140 + private async void LoginButton_Tapped(object sender, TappedRoutedEventArgs e)
141 + {
142 +
143 + if (this.ValidateFields())
144 + {
145 + this.showLoading();
146 + User user = await this.SMA.Login(this.UserNameInput.Text, this.PasswordInput.Password);
147 + CredentialStorage cs = new CredentialStorage();
148 + cs.SaveCredential(this.UserNameInput.Text, this.PasswordInput.Password);
149 + this.hideLoading();
150 + if (user != null)
151 + {
152 + this.goToUserPage();
153 + }
154 + else
155 + {
156 + cs.RemoveCredentials();
157 + }
158 +
159 + }
160 +
161 + }
162 +
163 + private void goToUserPage()
113 164 {
114 165 var aw = Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
115 - () => this.Frame.Navigate(typeof(StandardUserLoggedInPage)));
166 + () => {
167 + this.Frame.Navigate(typeof(StandardUserLoggedInPage));
168 + if (this.Frame.CanGoBack)
169 + {
170 + this.Frame.BackStack.Clear();
171 + }
172 + });
173 +
116 174 }
117 175
118 176 private void RegisterLink_Tapped(object sender, TappedRoutedEventArgs e)
119 177 {
120 178 var aw = Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
121 - () => this.Frame.Navigate(typeof(StandardUserRegistrationPage)));
179 + () => this.Frame.Navigate(typeof(SiteOwnerRegistrationPage)));
122 180 }
123 181
124 - private void RegisterSiteLink_Tapped(object sender, TappedRoutedEventArgs e)
182 +
183 +
184 + private bool ValidateFields()
125 185 {
126 - var aw = Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
127 - () => this.Frame.Navigate(typeof(SiteOwnerRegistrationPage)));
186 + SolidColorBrush red = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
187 + SolidColorBrush white = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
188 +
189 + this.UserNameInput.BorderBrush = this.UserNameInput.Text == "" ? red : white;
190 + this.PasswordInput.BorderBrush = this.PasswordInput.Password == "" ? red : white;
191 +
192 +
193 + return !this.UserNameInput.BorderBrush.Equals(red) && !this.PasswordInput.BorderBrush.Equals(red);
194 + }
195 +
196 + private void showLoading()
197 + {
198 + this.Loader.Visibility = Visibility.Visible;
199 + }
200 +
201 + private void hideLoading()
202 + {
203 + this.Loader.Visibility = Visibility.Collapsed;
128 204 }
129 205 }
130 206 }