summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/MainViewModel.cs
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2012-02-11 22:22:50 +0400
committerGravatar Vitaly Takmazov2012-02-11 22:22:50 +0400
commit49eb44f852ab21e72c17bbc436ffa79525a1aba3 (patch)
tree249cd1d69dc8078104dc2bc35cfcbbce98cd85f4 /Juick/ViewModels/MainViewModel.cs
v 0.999 :)
Diffstat (limited to 'Juick/ViewModels/MainViewModel.cs')
-rw-r--r--Juick/ViewModels/MainViewModel.cs101
1 files changed, 101 insertions, 0 deletions
diff --git a/Juick/ViewModels/MainViewModel.cs b/Juick/ViewModels/MainViewModel.cs
new file mode 100644
index 0000000..a504658
--- /dev/null
+++ b/Juick/ViewModels/MainViewModel.cs
@@ -0,0 +1,101 @@
+using System;
+using System.ComponentModel;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Runtime.Serialization.Json;
+using System.Collections.ObjectModel;
+using System.Windows;
+using Juick.Api;
+using RestSharp;
+
+namespace Juick.ViewModels
+{
+ public class MainViewModel : INotifyPropertyChanged
+ {
+ public MainViewModel()
+ {
+ this.MyFeed = new ObservableCollection<MessageViewModel>();
+ this.Last = new ObservableCollection<MessageViewModel>();
+ }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<MessageViewModel> MyFeed { get; private set; }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<MessageViewModel> Last { get; private set; }
+
+ public bool IsDataLoaded
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the MyFeed collection.
+ /// </summary>
+ public void LoadData()
+ {
+ var request = new RestRequest("/home?1=1" + "&rnd=" + Environment.TickCount);
+ App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
+ App.Client.ExecuteAsync(request, response =>
+ {
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ MessageBox.Show(response.StatusCode.ToString());
+ return;
+ }
+ var ser = new DataContractJsonSerializer(typeof (List<Message>));
+
+ var messages =
+ (List<Message>)ser.ReadObject(new MemoryStream(response.RawBytes));
+ MyFeed.Clear();
+ messages.ForEach(post => MyFeed.Add(new MessageViewModel(post)
+ {
+ Status =
+ string.Format(
+ "Posted on: {0}, replies: {1}",
+ post.timestamp,
+ post.replies)
+ }));
+ NotifyPropertyChanged("MyFeed");
+
+ });
+ var lastrequest = new RestRequest("/messages?1=1" + "&rnd=" + Environment.TickCount);
+ App.Client.ExecuteAsync(lastrequest, response =>
+ {
+ var ser = new DataContractJsonSerializer(typeof(List<Message>));
+ using (var ms = new MemoryStream(response.RawBytes))
+ {
+ var messages =
+ (List<Message>)ser.ReadObject(ms);
+ if (messages == null) return;
+ Last.Clear();
+ messages.ForEach(post => Last.Add(new MessageViewModel(post)
+ {
+ Status = string.Format(
+ "Posted on: {0}, replies: {1}",
+ post.timestamp, post.replies
+ )
+ }));
+ NotifyPropertyChanged("Last");
+ }
+ });
+ }
+
+
+ public event PropertyChangedEventHandler PropertyChanged;
+ private void NotifyPropertyChanged(String propertyName)
+ {
+ PropertyChangedEventHandler handler = PropertyChanged;
+ if (null != handler)
+ {
+ handler(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+ }
+} \ No newline at end of file