From 2b387fb5626a57e3a6473eed66816df80152cd49 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 10 Mar 2012 22:14:25 +0400 Subject: refactoring, tags --- Juick/ViewModels/MessageListViewModelBase.cs | 95 ++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Juick/ViewModels/MessageListViewModelBase.cs (limited to 'Juick/ViewModels/MessageListViewModelBase.cs') diff --git a/Juick/ViewModels/MessageListViewModelBase.cs b/Juick/ViewModels/MessageListViewModelBase.cs new file mode 100644 index 0000000..6bbfd55 --- /dev/null +++ b/Juick/ViewModels/MessageListViewModelBase.cs @@ -0,0 +1,95 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Runtime.Serialization.Json; +using System.Collections.ObjectModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media.Imaging; +using Juick.Api; +using RestSharp; + +namespace Juick.ViewModels +{ + public class MessageListViewModelBase : INotifyPropertyChanged + { + public MessageListViewModelBase() + { + this.Items = new ObservableCollection(); + } + + public string RestUri { get; set; } + + /// + /// A collection for MessageViewModel objects. + /// + public ObservableCollection Items { get; private set; } + + public bool IsDataLoaded + { + get; + private set; + } + + /// + /// Creates and adds a few MessageViewModel objects into the Items collection. + /// + public void LoadData() + { + if (string.IsNullOrEmpty(RestUri)) + { + RestUri = "/home?1=1" + "&rnd=" + Environment.TickCount; + } + var request = new RestRequest(RestUri); + 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 messages = response.Data; + Items.Clear(); + messages.ForEach(post => + { + var item = new MessageViewModel(post) + { + Status = + string.Format( + "Posted on: {0}, replies: {1}", + post.Timestamp, + post.Replies) + }; + Items.Add(item); + var imageUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute); + item.UserAvatar = new BitmapImage + { + UriSource = imageUri + }; + if (post.Photo != null) + { + item.Attachment = new BitmapImage { UriSource = new Uri(post.Photo.Small, UriKind.Absolute) }; + } + + }); + NotifyPropertyChanged("Items"); + }); + } + + + public event PropertyChangedEventHandler PropertyChanged; + public 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