summaryrefslogtreecommitdiff
path: root/Juick/ViewModels
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2013-04-03 05:03:20 +0400
committerGravatar Vitaly Takmazov2013-04-03 05:03:20 +0400
commit151e56ec2e3466389804b8874b4224bf6bd08097 (patch)
tree4b2c07be26036e470afba1f399619c8d64ae8216 /Juick/ViewModels
parent4959050e7996e92ac97f4d05064130251a0628c5 (diff)
New pages, LoginViewModel, Validation (in progress)
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r--Juick/ViewModels/LoginViewModel.cs72
-rw-r--r--Juick/ViewModels/PageViewModel.cs4
-rw-r--r--Juick/ViewModels/Validation/DataViewModelBase.cs44
3 files changed, 117 insertions, 3 deletions
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<RoutedEventArgs> _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<RoutedEventArgs> SignInCommand
+ {
+ get
+ {
+ return _signInCommand ?? (_signInCommand = new DelegateCommand<RoutedEventArgs>(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<PostItem> 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<List<Message>>(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<String, List<String>> _errors =
+ new Dictionary<string, List<string>>();
+
+ // 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<string>();
+
+ 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]));
+ }
+ }
+ }
+}