From 151e56ec2e3466389804b8874b4224bf6bd08097 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 3 Apr 2013 05:03:20 +0400 Subject: New pages, LoginViewModel, Validation (in progress) --- Juick/ViewModels/LoginViewModel.cs | 72 ++++++++++++++++++++++++ Juick/ViewModels/PageViewModel.cs | 4 +- Juick/ViewModels/Validation/DataViewModelBase.cs | 44 +++++++++++++++ 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 Juick/ViewModels/LoginViewModel.cs create mode 100644 Juick/ViewModels/Validation/DataViewModelBase.cs (limited to 'Juick/ViewModels') diff --git a/Juick/ViewModels/LoginViewModel.cs b/Juick/ViewModels/LoginViewModel.cs new file mode 100644 index 0000000..95528fe --- /dev/null +++ b/Juick/ViewModels/LoginViewModel.cs @@ -0,0 +1,72 @@ +using System; +using System.Net; +using System.Windows; +using Juick.Classes; +using Juick.ViewModels.Validation; +using RestSharp; + +namespace Juick.ViewModels +{ + public class LoginViewModel : DataViewModelBase + { + private string _username; + private string _password; + private DelegateCommand _signInCommand; + + public string Username + { + get + { + return _username ?? (_username = App.AppContext.Account.UserName); + } + set + { + _username = value; + NotifyPropertyChanged("Username"); + } + } + + public string Password + { + get + { + return _password ?? (_password = App.AppContext.Account.Password); + } + set + { + _password = value; + NotifyPropertyChanged("Password"); + } + } + + public Uri NextUri { get; set; } + + public DelegateCommand SignInCommand + { + get + { + return _signInCommand ?? (_signInCommand = new DelegateCommand(CheckAuth, () => true)); + } + } + + public void CheckAuth(RoutedEventArgs e) + { + App.AppContext.Client.Authenticator = new HttpBasicAuthenticator(Username, Password); + App.AppContext.Client.ExecuteAsync(new RestRequest("/post", Method.POST), response => + { + if (response.StatusCode == + HttpStatusCode.BadRequest) + { + App.AppContext.Account.UserName = Username; + App.AppContext.Account.Password = Password; + App.AppContext.Account.IsAuthenticated = true; + ((App)Application.Current).NavigateTo(NextUri, true); + } + else + { + AddError("Username", "Invalid username or password", false); + } + }); + } + } +} diff --git a/Juick/ViewModels/PageViewModel.cs b/Juick/ViewModels/PageViewModel.cs index 02f86ff..e398caf 100644 --- a/Juick/ViewModels/PageViewModel.cs +++ b/Juick/ViewModels/PageViewModel.cs @@ -31,11 +31,10 @@ namespace Juick.ViewModels set { _restUri = value; - RefreshData(); } } - public virtual string Caption { get { return "juick"; } } + public virtual string Caption { get; set; } public ObservableCollection Items { get; private set; } @@ -70,7 +69,6 @@ namespace Juick.ViewModels } var request = new RestRequest(requestUri); - _context.Client.Authenticator = new HttpBasicAuthenticator(_context.Account.Credentials.UserName, _context.Account.Credentials.Password); _context.Client.ExecuteAsync>(request, response => { _context.IsDataLoading = false; diff --git a/Juick/ViewModels/Validation/DataViewModelBase.cs b/Juick/ViewModels/Validation/DataViewModelBase.cs new file mode 100644 index 0000000..dcbf34b --- /dev/null +++ b/Juick/ViewModels/Validation/DataViewModelBase.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; + +namespace Juick.ViewModels.Validation +{ + public class DataViewModelBase : ViewModelBase, IDataErrorInfo + { + + private readonly Dictionary> _errors = + new Dictionary>(); + + // Adds the specified error to the errors collection if it is not already + // present, inserting it in the first position if isWarning is false. + public void AddError(string propertyName, string error, bool isWarning) + { + if (!_errors.ContainsKey(propertyName)) + _errors[propertyName] = new List(); + + if (_errors[propertyName].Contains(error)) return; + if (isWarning) _errors[propertyName].Add(error); + else _errors[propertyName].Insert(0, error); + } + + // Removes the specified error from the errors collection if it is present. + public void RemoveError(string propertyName, string error) + { + if (!_errors.ContainsKey(propertyName) || !_errors[propertyName].Contains(error)) return; + _errors[propertyName].Remove(error); + if (_errors[propertyName].Count == 0) _errors.Remove(propertyName); + } + + public string Error { get { throw new NotImplementedException(); } } + + public string this[string columnName] + { + get + { + return (!_errors.ContainsKey(columnName) ? null : + String.Join(Environment.NewLine, _errors[columnName])); + } + } + } +} -- cgit v1.2.3