From 49eb44f852ab21e72c17bbc436ffa79525a1aba3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 11 Feb 2012 22:22:50 +0400 Subject: v 0.999 :) --- Juick/ViewModels/MainViewModel.cs | 101 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Juick/ViewModels/MainViewModel.cs (limited to 'Juick/ViewModels/MainViewModel.cs') 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(); + this.Last = new ObservableCollection(); + } + + /// + /// A collection for MessageViewModel objects. + /// + public ObservableCollection MyFeed { get; private set; } + + /// + /// A collection for MessageViewModel objects. + /// + public ObservableCollection Last { get; private set; } + + public bool IsDataLoaded + { + get; + private set; + } + + /// + /// Creates and adds a few MessageViewModel objects into the MyFeed collection. + /// + 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)); + + var messages = + (List)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)); + using (var ms = new MemoryStream(response.RawBytes)) + { + var messages = + (List)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 -- cgit v1.2.3