Subversion Repository Public Repository

Nextrek

Diff Revisions 659 vs 660 for /Android/SmartCharging/SmartCharging_WP/SmartCharging/SiteOwnerRegistrationPage.xaml.cs

Diff revisions: vs.
  @@ -1,13 +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.Text.RegularExpressions;
7 10 using Windows.Foundation;
8 11 using Windows.Foundation.Collections;
9 12 using Windows.Graphics.Display;
10 13 using Windows.UI;
14 + using Windows.UI.Core;
11 15 using Windows.UI.ViewManagement;
12 16 using Windows.UI.Xaml;
13 17 using Windows.UI.Xaml.Controls;
  @@ -28,6 +32,8 @@
28 32 {
29 33 private NavigationHelper navigationHelper;
30 34 private ObservableDictionary defaultViewModel = new ObservableDictionary();
35 + private SiteType selectedSiteType;
36 + private SmartChargeAPI SMA;
31 37
32 38 public SiteOwnerRegistrationPage()
33 39 {
  @@ -36,6 +42,7 @@
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;
39 46 }
40 47
41 48 /// <summary>
  @@ -66,8 +73,11 @@
66 73 /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
67 74 /// a dictionary of state preserved by this page during an earlier
68 75 /// session. The state will be null the first time a page is visited.</param>
69 - private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
76 + private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
70 77 {
78 +
79 + List<SiteType> types = await this.SMA.getSiteTypesList();
80 + this.SiteTypeList.ItemsSource = types;
71 81 }
72 82
73 83 /// <summary>
  @@ -110,10 +120,44 @@
110 120 #endregion
111 121
112 122
113 - private void SubmitButton_Tapped(object sender, TappedRoutedEventArgs e)
123 + private async void SubmitButton_Tapped(object sender, TappedRoutedEventArgs e)
114 124 {
115 125 bool isValid = ValidateFields();
116 - var t = 9;
126 + if (isValid)
127 + { //register user
128 + User newUser = new User()
129 + {
130 + Name = this.UsernameInput.Text,
131 + Surname = this.SurnameInput.Text,
132 + Email = this.EmailInput.Text,
133 + CellphoneNumber = this.PhoneNumberInput.Text,
134 + UserType = this.UserTypeToggleSwitch.IsOn ? USER_TYPE.Owner : USER_TYPE.Standard
135 + };
136 + this.showLoading();
137 + bool registrationResult = await this.SMA.RegisterUser(newUser, this.AvatarPicker.storageFile, this.UsernameInput.Text, this.PasswordInput.Password);
138 +
139 + if (this.UserTypeToggleSwitch.IsOn)
140 + {
141 + //register site
142 + if (SiteImagePicker.images.Count > 0)
143 + {
144 +
145 + bool uploadResult = await this.SMA.UploadSiteImages(s, this.SiteImagePicker.images.ToList());
146 +
147 + }
148 +
149 + }
150 + else
151 + {
152 + this.goToUserPage();
153 + }
154 +
155 + this.hideLoading();
156 + }
157 +
158 +
159 +
160 +
117 161 }
118 162
119 163 private bool ValidateFields()
  @@ -121,14 +165,69 @@
121 165 SolidColorBrush red = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
122 166 SolidColorBrush white = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
123 167
124 - this.NameInput.BorderBrush = this.NameInput.Text == "" ? red : white;
125 - this.SurnameInput.BorderBrush = this.SurnameInput.Text == "" ? red : white;
126 168 this.UsernameInput.BorderBrush = this.UsernameInput.Text == "" ? red : white;
127 - this.PasswordInput.BorderBrush = this.PasswordInput.Password == "" || this.PasswordInput.Password.Length < 6 ? red : white;
169 + this.EmailInput.BorderBrush = this.EmailInput.Text == "" || !this.IsValidEmailAddress(this.EmailInput.Text) ? red : white;
170 + this.PasswordInput.BorderBrush = this.PasswordInput.Password == "" ? red : white;
171 +
172 + bool userOk = !this.UsernameInput.BorderBrush.Equals(red) && !this.PasswordInput.BorderBrush.Equals(red) && !this.EmailInput.BorderBrush.Equals(red);
173 + bool ownerOK = false;
174 + if (this.UserTypeToggleSwitch.IsOn)
175 + {
176 + this.SiteNameInput.BorderBrush = this.SiteNameInput.Text == "" ? red : white;
177 + this.SiteTypeButton.BorderBrush = selectedSiteType == null ? red : white;
178 + ownerOK = !this.SiteNameInput.BorderBrush.Equals(red) && !this.SiteTypeButton.BorderBrush.Equals(red);
179 + }
180 +
128 181
182 + return userOk && (!this.UserTypeToggleSwitch.IsOn || ownerOK);
183 + }
129 184
130 - return !this.NameInput.BorderBrush.Equals(red) && !this.SurnameInput.BorderBrush.Equals(red) &&
131 - !this.UsernameInput.BorderBrush.Equals(red) && !this.PasswordInput.BorderBrush.Equals(red);
185 + private void UserTypeToggleSwitch_Toggled(object sender, RoutedEventArgs e)
186 + {
187 +
188 + }
189 +
190 + private void ListBoxItem_Tapped(object sender, TappedRoutedEventArgs e)
191 + {
192 + this.SiteTypeButton.Flyout.Hide();
193 + SiteType selected = ((Control)sender).DataContext as SiteType;
194 + this.SiteTypeButton.Content = "Tipologia locale : " + selected.Label;
195 + this.selectedSiteType = selected;
196 + }
197 +
198 + private void showLoading()
199 + {
200 + this.Loader.Visibility = Visibility.Visible;
201 + }
202 +
203 + private void hideLoading()
204 + {
205 + this.Loader.Visibility = Visibility.Collapsed;
206 + }
207 +
208 + private void goToUserPage()
209 + {
210 + var aw = Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
211 + () =>
212 + {
213 + this.Frame.Navigate(typeof(StandardUserLoggedInPage));
214 + if (this.Frame.CanGoBack)
215 + {
216 + this.Frame.BackStack.Clear();
217 + }
218 + });
219 +
220 + }
221 +
222 + private bool IsValidEmailAddress(string s)
223 + {
224 + if (string.IsNullOrEmpty(s))
225 + return false;
226 + else
227 + {
228 + var regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
229 + return regex.IsMatch(s) && !s.EndsWith(".");
230 + }
132 231 }
133 232
134 233 }