summaryrefslogtreecommitdiff
path: root/Juick/ViewModels
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2013-04-01 13:34:49 +0400
committerGravatar Vitaly Takmazov2013-04-01 13:34:49 +0400
commitfb1fab2e1593ba13fae8eae519f5482525eb68a1 (patch)
treef3557b2e8991ec53fde46d05346fab2bd8fce37e /Juick/ViewModels
parent57550f203995b61d600fae59832e409fd5feebcd (diff)
AppViewModel
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r--Juick/ViewModels/AppViewModel.cs67
-rw-r--r--Juick/ViewModels/PageViewModel.cs111
-rw-r--r--Juick/ViewModels/ThreadViewModel.cs7
-rw-r--r--Juick/ViewModels/ViewModelBase.cs122
4 files changed, 192 insertions, 115 deletions
diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs
new file mode 100644
index 0000000..5d22cab
--- /dev/null
+++ b/Juick/ViewModels/AppViewModel.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using System.Collections.ObjectModel;
+using Juick.Classes;
+using RestSharp;
+
+namespace Juick.ViewModels
+{
+ public class AppViewModel : ViewModelBase
+ {
+ private ObservableCollection<PageViewModel> _pages;
+ public ObservableCollection<PageViewModel> Pages
+ {
+ get
+ {
+ if (_pages == null)
+ _pages = new ObservableCollection<PageViewModel>();
+ return _pages;
+ }
+ }
+
+ static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<AppViewModel>(x => x.IsDataLoading);
+ bool isDataLoading;
+
+ public bool IsDataLoading
+ {
+ get { return isDataLoading; }
+ set
+ {
+ isDataLoading = value;
+ NotifyPropertyChanged(IsDataLoadingPropertyName);
+ foreach (var page in Pages)
+ {
+ page.LoadMessagesPageCommand.NotifyCanExecuteChanged();
+ }
+ }
+ }
+
+ private AccountManager _acc;
+
+ public AccountManager Account
+ {
+ get { return _acc ?? (_acc = new AccountManager()); }
+ }
+
+ private RestClient _cl;
+ public RestClient Client
+ {
+ get
+ {
+ return _cl ?? (_cl = new RestClient("http://api.juick.com")
+ {
+ UserAgent = "Juick 1.1/Windows Phone " + Environment.OSVersion.Version
+ });
+ }
+ }
+
+ }
+}
diff --git a/Juick/ViewModels/PageViewModel.cs b/Juick/ViewModels/PageViewModel.cs
new file mode 100644
index 0000000..9b9cae5
--- /dev/null
+++ b/Juick/ViewModels/PageViewModel.cs
@@ -0,0 +1,111 @@
+using System;
+using System.Linq;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Net;
+using System.Windows;
+using System.Windows.Media.Imaging;
+using Juick.Classes;
+using JuickApi;
+using RestSharp;
+using System.Diagnostics;
+using System.Windows.Navigation;
+using Microsoft.Phone.Controls;
+using System.Windows.Controls;
+
+namespace Juick.ViewModels
+{
+ public class PageViewModel : ViewModelBase
+ {
+ public PageViewModel(AppViewModel context)
+ {
+ this.context = context;
+ Items = new ObservableCollection<PostItem>();
+ LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !context.IsDataLoading);
+ NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true);
+ }
+
+ private AppViewModel context;
+
+ public string RestUri { get; set; }
+ public virtual string Caption { get { return "juick"; } }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<PostItem> Items { get; private set; }
+
+ public DelegateCommand LoadMessagesPageCommand { get; private set; }
+
+ public DelegateCommand NavigateNextCommand { get; private set; }
+
+ public void NavigateToThread(object param)
+ {
+ var selectionChangedEventArgs = param as SelectionChangedEventArgs;
+ if (param != null)
+ {
+ ((App)App.Current).NavigateTo(new Uri(string.Format("/ThreadView.xaml?mid={0}", ((PostItem)selectionChangedEventArgs.AddedItems[0]).MID), UriKind.Relative));
+ }
+
+ }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the Items collection.
+ /// </summary>
+ public void LoadData(object EventArgs)
+ {
+ const int offset = 5;
+ if (context.IsDataLoading)
+ {
+ return;
+ }
+ if (EventArgs != null)
+ {
+ var item = (EventArgs as LinkUnlinkEventArgs).ContentPresenter.Content;
+ if (!item.Equals(Items[Items.Count - offset])) {
+ return;
+ }
+ }
+
+ int count = Items.Count;
+
+
+ if (string.IsNullOrEmpty(RestUri))
+ {
+ RestUri = "/home?1=1";
+ }
+
+ // super-костыли
+ // todo: rewrite
+ else if (RestUri.StartsWith("/home?", StringComparison.InvariantCulture) && Items.Count > 0)
+ {
+ var lastItem = Items[Items.Count - 1];
+ RestUri = string.Format("/home?before_mid={0}", lastItem.MID);
+ }
+ else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0)
+ {
+ var lastItem = Items[Items.Count - 1];
+ RestUri = string.Format("/messages?before_mid={0}", lastItem.MID);
+ }
+
+ var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
+ context.Client.Authenticator = new HttpBasicAuthenticator(context.Account.Credentials.UserName, context.Account.Credentials.Password);
+ context.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
+ context.IsDataLoading = true;
+ }
+
+ void ProcessResponse(IRestResponse<List<Message>> response)
+ {
+ context.IsDataLoading = false;
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ MessageBox.Show(response.StatusCode.ToString());
+ return;
+ }
+
+ //Items.Clear();
+ response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
+ }
+ }
+}
diff --git a/Juick/ViewModels/ThreadViewModel.cs b/Juick/ViewModels/ThreadViewModel.cs
index 1cbaab7..c004851 100644
--- a/Juick/ViewModels/ThreadViewModel.cs
+++ b/Juick/ViewModels/ThreadViewModel.cs
@@ -2,8 +2,13 @@
namespace Juick.ViewModels
{
- public class ThreadViewModel : ViewModelBase
+ public class ThreadViewModel : PageViewModel
{
+ public ThreadViewModel(AppViewModel context) : base(context)
+ {
+
+ }
+
static readonly string CaptionPropertyName = ExpressionHelper.GetPropertyName<ThreadViewModel>(x => x.Caption);
private int _mid;
diff --git a/Juick/ViewModels/ViewModelBase.cs b/Juick/ViewModels/ViewModelBase.cs
index 746ce04..9ac3023 100644
--- a/Juick/ViewModels/ViewModelBase.cs
+++ b/Juick/ViewModels/ViewModelBase.cs
@@ -1,127 +1,21 @@
using System;
-using System.Linq;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.ComponentModel;
using System.Net;
using System.Windows;
-using System.Windows.Media.Imaging;
-using Juick.Classes;
-using JuickApi;
-using RestSharp;
-using System.Diagnostics;
-using System.Windows.Navigation;
-using Microsoft.Phone.Controls;
using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using System.ComponentModel;
+using Juick.Classes;
namespace Juick.ViewModels
{
public class ViewModelBase : INotifyPropertyChanged
{
- static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<ViewModelBase>(x => x.IsDataLoading);
- bool isDataLoading;
-
- public ViewModelBase()
- {
- Items = new ObservableCollection<PostItem>();
- LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading);
- NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true);
- }
-
- public string RestUri { get; set; }
- public virtual string Caption { get { return "juick"; } }
-
- /// <summary>
- /// A collection for MessageViewModel objects.
- /// </summary>
- public ObservableCollection<PostItem> Items { get; private set; }
-
- public DelegateCommand LoadMessagesPageCommand { get; private set; }
-
- public DelegateCommand NavigateNextCommand { get; private set; }
-
- public bool IsDataLoading
- {
- get { return isDataLoading; }
- set
- {
- isDataLoading = value;
- NotifyPropertyChanged(IsDataLoadingPropertyName);
- LoadMessagesPageCommand.NotifyCanExecuteChanged();
- }
- }
-
- public void NavigateToThread(object param)
- {
- var selectionChangedEventArgs = param as SelectionChangedEventArgs;
- if (param != null)
- {
- ((App)App.Current).NavigateTo(new Uri(string.Format("/ThreadView.xaml?mid={0}", ((PostItem)selectionChangedEventArgs.AddedItems[0]).MID), UriKind.Relative));
- }
-
- }
-
- /// <summary>
- /// Creates and adds a few MessageViewModel objects into the Items collection.
- /// </summary>
- public void LoadData(object EventArgs)
- {
- const int offset = 5;
- if (IsDataLoading)
- {
- return;
- }
- if (EventArgs != null)
- {
- var item = (EventArgs as LinkUnlinkEventArgs).ContentPresenter.Content;
- if (!item.Equals(Items[Items.Count - offset])) {
- return;
- }
- }
-
- int count = Items.Count;
-
-
- if (string.IsNullOrEmpty(RestUri))
- {
- RestUri = "/home?1=1";
- }
-
- // super-костыли
- // todo: rewrite
- else if (RestUri.StartsWith("/home?", StringComparison.InvariantCulture) && Items.Count > 0)
- {
- var lastItem = Items[Items.Count - 1];
- RestUri = string.Format("/home?before_mid={0}", lastItem.MID);
- }
- else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0)
- {
- var lastItem = Items[Items.Count - 1];
- RestUri = string.Format("/messages?before_mid={0}", lastItem.MID);
- }
-
- var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
- App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
- App.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
- IsDataLoading = true;
- }
-
- void ProcessResponse(IRestResponse<List<Message>> response)
- {
- IsDataLoading = false;
- if (response.StatusCode != HttpStatusCode.OK)
- {
- MessageBox.Show(response.StatusCode.ToString());
- return;
- }
-
- //Items.Clear();
- response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
- }
-
-
public event PropertyChangedEventHandler PropertyChanged;
-
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)