Guest User

Untitled

a guest
Dec 26th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="mvvm.Views.LoginPage"
  5. xmlns:converters="clr-namespace:XFPocketApp.Core.Converters;assembly=XFPocketApp.Core">
  6.  
  7. <ContentPage.Resources>
  8. <ResourceDictionary>
  9. <converters:ValidationErrorConverter x:Key="validationErrorConverter" />
  10. </ResourceDictionary>
  11. </ContentPage.Resources>
  12.  
  13. <StackLayout Padding="10,100,10,10">
  14. <StackLayout>
  15. <Label
  16. Text="{Binding Errors, Converter={StaticResource validationErrorConverter}}" />
  17.  
  18. <Label Text="User name:" />
  19. <Entry x:Name="entryUsername" Keyboard="Email" Text="{Binding Username, Mode=TwoWay}" Style="{StaticResource EntryStyle}" />
  20.  
  21. <Label Text="Password:" />
  22. <Entry x:Name="entryPassword" IsPassword="true" Text="{Binding Password, Mode=TwoWay}" Style="{StaticResource EntryStyle}" />
  23. </StackLayout>
  24. <Button VerticalOptions="EndAndExpand" Text="LOGIN" Command="{Binding AuthenticateUserCommand}" />
  25. </StackLayout>
  26. </ContentPage>
  27.  
  28. using mvvm.ViewModels;
  29. using Xamarin.Forms;
  30.  
  31. namespace mvvm.Views
  32. {
  33. public partial class LoginPage : ContentPage
  34. {
  35. LoginViewModel viewModel = new LoginViewModel();
  36.  
  37. public LoginPage()
  38. {
  39. InitializeComponent();
  40.  
  41. this.BindingContext = viewModel;
  42. }
  43. }
  44. }
  45.  
  46. using System.Windows.Input;
  47. using Xamarin.Forms;
  48. using mvvm.Models;
  49. using System.Collections.Generic;
  50. using XFPocketApp.Core.ViewModels;
  51.  
  52. namespace mvvm.ViewModels
  53. {
  54. public class LoginViewModel : ViewModelBase
  55. {
  56. private string _username;
  57. private string _password;
  58. private bool _isValid;
  59. private IList<string> _errors;
  60.  
  61. public LoginViewModel()
  62. {
  63. _username = "";
  64. _password = "";
  65.  
  66. _errors = new List<string>();
  67. }
  68.  
  69. public IList<string> Errors
  70. {
  71. set { SetProperty(ref _errors, value); }
  72. get { return _errors; }
  73. }
  74.  
  75. public string Username {
  76. set {
  77. SetProperty(ref _username, value);
  78. ((Command)AuthenticateUserCommand).ChangeCanExecute();
  79. }
  80. get { return _username; }
  81. }
  82.  
  83. public string Password
  84. {
  85. set {
  86. SetProperty(ref _password, value);
  87. ((Command)AuthenticateUserCommand).ChangeCanExecute();
  88. }
  89. get { return _password; }
  90. }
  91.  
  92. public bool IsValid
  93. {
  94. set { SetProperty(ref _isValid, value); }
  95. get { return _isValid; }
  96. }
  97.  
  98. public ICommand AuthenticateUserCommand => new Command(
  99. execute: () => {
  100. AuthenticateUserAsync();
  101. }
  102. //,
  103. //canExecute: () => {
  104. // return (Username.Length > 0);
  105. //}
  106. );
  107.  
  108. private bool Validate()
  109. {
  110. if (_username == "")
  111. {
  112. _errors.Add("An e-mail address is required.");
  113. Errors = _errors;
  114. }
  115.  
  116. if (_password == "")
  117. {
  118. _errors.Add("A password is required.");
  119. Errors = _errors;
  120. }
  121.  
  122. return _errors.Count == 0;
  123. }
  124.  
  125. private async void AuthenticateUserAsync()
  126. {
  127. Errors.Clear();
  128.  
  129. IsValid = true;
  130. bool isValid = Validate();
  131. bool isAuthenticated = false;
  132. Token token = null;
  133.  
  134. if (!isValid)
  135. {
  136. IsValid = false;
  137. return;
  138. }
  139.  
  140. token = await Services.UserService.IsValidUser(_username, _password);
  141. if (token != null)
  142. isAuthenticated = true;
  143.  
  144. if (isAuthenticated)
  145. {
  146. //new Messenger().Subscribe<LoginFailedMessage>(this, loginPage.LoginFailed);
  147. //MainPage = new NavigationPage(new mvvmPage());
  148. }
  149. else
  150. {
  151. //Errors.Clear();
  152. //Errors.Add("Invalid credentials.");
  153. }
  154. }
  155. }
  156. }
  157.  
  158. using System;
  159. using System.Collections.Generic;
  160. using System.Globalization;
  161. using Xamarin.Forms;
  162.  
  163. namespace XFPocketApp.Core.Converters
  164. {
  165. public class ValidationErrorConverter : IValueConverter
  166. {
  167. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  168. {
  169. List<string> errors = value as List<string>;
  170. return errors != null && errors.Count > 0 ? string.Join("rn", errors.ToArray()) : "";
  171. }
  172.  
  173. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  174. {
  175. throw new NotImplementedException();
  176. }
  177. }
  178. }
  179.  
  180. using System;
  181. using System.ComponentModel;
  182. using System.Runtime.CompilerServices;
  183.  
  184. namespace XFPocketApp.Core.ViewModels
  185. {
  186. public class ViewModelBase : INotifyPropertyChanged
  187. {
  188. public event PropertyChangedEventHandler PropertyChanged;
  189.  
  190. protected bool SetProperty<T>(ref T storage, T value,
  191. [CallerMemberName] string propertyName = null)
  192. {
  193. if (Object.Equals(storage, value))
  194. return false;
  195.  
  196. storage = value;
  197. OnPropertyChanged(propertyName);
  198. return true;
  199. }
  200.  
  201. protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
  202. {
  203. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  204. }
  205. }
  206. }
Add Comment
Please, Sign In to add comment