summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/ViewModelBase.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/ViewModels/ViewModelBase.cs')
-rw-r--r--Juick/ViewModels/ViewModelBase.cs118
1 files changed, 118 insertions, 0 deletions
diff --git a/Juick/ViewModels/ViewModelBase.cs b/Juick/ViewModels/ViewModelBase.cs
new file mode 100644
index 0000000..e86ff4a
--- /dev/null
+++ b/Juick/ViewModels/ViewModelBase.cs
@@ -0,0 +1,118 @@
+using System;
+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;
+
+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);
+ }
+
+ 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 bool IsDataLoading
+ {
+ get { return isDataLoading; }
+ set
+ {
+ isDataLoading = value;
+ NotifyPropertyChanged(IsDataLoadingPropertyName);
+ LoadMessagesPageCommand.NotifyCanExecuteChanged();
+ }
+ }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the Items collection.
+ /// </summary>
+ public void LoadData()
+ {
+ if (IsDataLoading) {
+ return;
+ }
+
+ const int PageSize = 1;
+
+ 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}&page={1}", lastItem.MID, PageSize);
+ }
+ else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0)
+ {
+ var lastItem = Items[Items.Count - 1];
+ RestUri = string.Format("/messages?before_mid={0}&page={1}", lastItem.MID, PageSize);
+ }
+
+ 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();
+ foreach (var post in response.Data)
+ {
+ var status = string.Format("Posted on: {0}, replies: {1}", post.Timestamp, post.Replies);
+ var item = new PostItem(post)
+ {
+ Status = status,
+ AvatarUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute)
+ };
+ if (post.Photo != null)
+ {
+ item.Attachment = new Uri(post.Photo.Small, UriKind.Absolute) ;
+ }
+ Items.Add(item);
+ }
+ }
+
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ public void NotifyPropertyChanged(string propertyName)
+ {
+ if (PropertyChanged != null)
+ {
+ PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+ }
+}