diff options
author | Vitaly Takmazov | 2012-02-11 22:22:50 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2012-02-11 22:22:50 +0400 |
commit | 49eb44f852ab21e72c17bbc436ffa79525a1aba3 (patch) | |
tree | 249cd1d69dc8078104dc2bc35cfcbbce98cd85f4 /Juick/ViewModels |
v 0.999 :)
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r-- | Juick/ViewModels/MainViewModel.cs | 101 | ||||
-rw-r--r-- | Juick/ViewModels/MessageViewModel.cs | 159 | ||||
-rw-r--r-- | Juick/ViewModels/ThreadViewModel.cs | 70 |
3 files changed, 330 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 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;
+ /// <summary>
+ /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// </summary>
+ /// <returns></returns>
+ public int MID
+ {
+ get
+ {
+ return _mid;
+ }
+ set
+ {
+ if (value != _mid)
+ {
+ _mid = value;
+ NotifyPropertyChanged("MID");
+ }
+ }
+ }
+
+ private int _rid;
+ /// <summary>
+ /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// </summary>
+ /// <returns></returns>
+ public int RID
+ {
+ get
+ {
+ return _rid;
+ }
+ set
+ {
+ if (value != _rid)
+ {
+ _rid = value;
+ NotifyPropertyChanged("RID");
+ }
+ }
+ }
+ private string _username;
+ /// <summary>
+ /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// </summary>
+ /// <returns></returns>
+ 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;
+ /// <summary>
+ /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// </summary>
+ /// <returns></returns>
+ public string MessageText
+ {
+ get
+ {
+ return _messageText;
+ }
+ set
+ {
+ if (value != _messageText)
+ {
+ _messageText = value;
+ NotifyPropertyChanged("MessageText");
+ }
+ }
+ }
+
+ private string _status;
+ /// <summary>
+ /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// </summary>
+ /// <returns></returns>
+ 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<MessageViewModel>();
+ }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<MessageViewModel> Items { get; private set; }
+
+ public bool IsDataLoaded
+ {
+ get;
+ private set;
+ }
+
+ public MessageViewModel Root { get; set; }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the MyFeed collection.
+ /// </summary>
+ 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<Message>));
+ var messages = (List<Message>) 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));
+ }
+ }
+ }
+}
|