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 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 = ser.ReadObject(new MemoryStream(response.RawBytes)) as List; MyFeed.Clear(); messages.ForEach(post => { var item = new MessageViewModel(post) { Status = string.Format( "Posted on: {0}, replies: {1}", post. timestamp, post. replies) }; MyFeed.Add(item); var imageRequest = new RestRequest( string.Format("/as/{0}.png", post.user.uid)); App.AvatarClient.ExecuteAsync( imageRequest, restResponse => { item.UserAvatar = new BitmapImage (); item.UserAvatar.SetSource(new MemoryStream(restResponse.RawBytes)); item. NotifyPropertyChanged ("UserAvatar"); }); }); 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 => { var item = new MessageViewModel(post) { Status = string.Format( "Posted on: {0}, replies: {1}", post. timestamp, post. replies) }; Last.Add(item); var imageRequest = new RestRequest( string.Format("/as/{0}.png", post.user.uid)); App.AvatarClient.ExecuteAsync( imageRequest, restResponse => { item.UserAvatar = new BitmapImage (); item.UserAvatar.SetSource(new MemoryStream(restResponse.RawBytes)); item. NotifyPropertyChanged ("UserAvatar"); }); }); NotifyPropertyChanged("Last"); } }); } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } }