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 ++++++++++++++++++++++ Juick/ViewModels/MessageViewModel.cs | 159 +++++++++++++++++++++++++++++++++++ Juick/ViewModels/ThreadViewModel.cs | 70 +++++++++++++++ 3 files changed, 330 insertions(+) create mode 100644 Juick/ViewModels/MainViewModel.cs create mode 100644 Juick/ViewModels/MessageViewModel.cs create mode 100644 Juick/ViewModels/ThreadViewModel.cs (limited to 'Juick/ViewModels') 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 diff --git a/Juick/ViewModels/MessageViewModel.cs b/Juick/ViewModels/MessageViewModel.cs new file mode 100644 index 0000000..32da0aa --- /dev/null +++ b/Juick/ViewModels/MessageViewModel.cs @@ -0,0 +1,159 @@ +using System; +using System.ComponentModel; +using System.Diagnostics; +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 Juick.Api; + +namespace Juick +{ + public class MessageViewModel : INotifyPropertyChanged + { + public MessageViewModel() + { + + } + + public MessageViewModel(Message message) + { + MID = message.mid; + RID = message.rid; + Username = message.user.uname; + MessageText = HttpUtility.HtmlDecode(message.body); + } + private int _mid; + /// + /// Sample ViewModel property; this property is used in the view to display its value using a Binding. + /// + /// + public int MID + { + get + { + return _mid; + } + set + { + if (value != _mid) + { + _mid = value; + NotifyPropertyChanged("MID"); + } + } + } + + private int _rid; + /// + /// Sample ViewModel property; this property is used in the view to display its value using a Binding. + /// + /// + public int RID + { + get + { + return _rid; + } + set + { + if (value != _rid) + { + _rid = value; + NotifyPropertyChanged("RID"); + } + } + } + private string _username; + /// + /// Sample ViewModel property; this property is used in the view to display its value using a Binding. + /// + /// + public string Username + { + get + { + return _username; + } + set + { + if (value != _username) + { + _username = value; + NotifyPropertyChanged("Username"); + } + } + } + + private Image _avatar; + public Image UserAvatar + { + get { return _avatar; } + set + { + if (value != _avatar) + { + _avatar = value; + NotifyPropertyChanged("Useravatar"); + } + } + } + + private string _messageText; + /// + /// Sample ViewModel property; this property is used in the view to display its value using a Binding. + /// + /// + public string MessageText + { + get + { + return _messageText; + } + set + { + if (value != _messageText) + { + _messageText = value; + NotifyPropertyChanged("MessageText"); + } + } + } + + private string _status; + /// + /// Sample ViewModel property; this property is used in the view to display its value using a Binding. + /// + /// + public string Status + { + get + { + return _status; + } + set + { + if (value != _status) + { + _status = value; + NotifyPropertyChanged("Status"); + } + } + } + + 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 diff --git a/Juick/ViewModels/ThreadViewModel.cs b/Juick/ViewModels/ThreadViewModel.cs new file mode 100644 index 0000000..48ebe65 --- /dev/null +++ b/Juick/ViewModels/ThreadViewModel.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.IO; +using System.Net; +using System.Net.Browser; +using System.Runtime.Serialization.Json; +using Juick.Api; +using RestSharp; + +namespace Juick.ViewModels +{ + public class ThreadViewModel : INotifyPropertyChanged + { + public ThreadViewModel() + { + this.Items = new ObservableCollection(); + } + + /// + /// A collection for MessageViewModel objects. + /// + public ObservableCollection Items { get; private set; } + + public bool IsDataLoaded + { + get; + private set; + } + + public MessageViewModel Root { get; set; } + + /// + /// Creates and adds a few MessageViewModel objects into the MyFeed collection. + /// + public void LoadData() + { + var request = new RestRequest("/thread?mid={mid}" + "&rnd=" + Environment.TickCount); + request.AddUrlSegment("mid", string.Format("{0}",Root.MID)); + App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password); + App.Client.ExecuteAsync(request, response => + { + using (var responseStream = new MemoryStream(response.RawBytes)) + { + var ser = new DataContractJsonSerializer(typeof (List)); + var messages = (List) ser.ReadObject(responseStream); + Items.Clear(); + messages.ForEach(post => + { + this.Items.Add( + new MessageViewModel(post)); + }); + IsDataLoaded = true; + NotifyPropertyChanged("Items"); + } + }); + } + + public event PropertyChangedEventHandler PropertyChanged; + private void NotifyPropertyChanged(String propertyName) + { + PropertyChangedEventHandler handler = PropertyChanged; + if (null != handler) + { + handler(this, new PropertyChangedEventArgs(propertyName)); + } + } + } +} -- cgit v1.2.3