From 6a92a6a4d27dc07f8b32bd7d57ffbcbe15630ab9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 12 Feb 2012 01:53:36 +0400 Subject: Avatars support + broken clickable urls --- Juick/ViewModels/MainViewModel.cs | 76 ++++++++++++++++++++++++++++-------- Juick/ViewModels/MessageViewModel.cs | 8 ++-- Juick/ViewModels/ThreadViewModel.cs | 19 ++++++++- 3 files changed, 82 insertions(+), 21 deletions(-) (limited to 'Juick/ViewModels') diff --git a/Juick/ViewModels/MainViewModel.cs b/Juick/ViewModels/MainViewModel.cs index a504658..23ff020 100644 --- a/Juick/ViewModels/MainViewModel.cs +++ b/Juick/ViewModels/MainViewModel.cs @@ -2,10 +2,13 @@ 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; @@ -52,16 +55,36 @@ namespace Juick.ViewModels var ser = new DataContractJsonSerializer(typeof (List)); var messages = - (List)ser.ReadObject(new MemoryStream(response.RawBytes)); + ser.ReadObject(new MemoryStream(response.RawBytes)) as List; MyFeed.Clear(); - messages.ForEach(post => MyFeed.Add(new MessageViewModel(post) - { - Status = - string.Format( - "Posted on: {0}, replies: {1}", - post.timestamp, - post.replies) - })); + 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"); }); @@ -75,13 +98,34 @@ namespace Juick.ViewModels (List)ser.ReadObject(ms); if (messages == null) return; Last.Clear(); - messages.ForEach(post => Last.Add(new MessageViewModel(post) + messages.ForEach(post => { - Status = string.Format( - "Posted on: {0}, replies: {1}", - post.timestamp, post.replies - ) - })); + 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"); } }); @@ -89,7 +133,7 @@ namespace Juick.ViewModels public event PropertyChangedEventHandler PropertyChanged; - private void NotifyPropertyChanged(String propertyName) + public void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) diff --git a/Juick/ViewModels/MessageViewModel.cs b/Juick/ViewModels/MessageViewModel.cs index 32da0aa..8620ab3 100644 --- a/Juick/ViewModels/MessageViewModel.cs +++ b/Juick/ViewModels/MessageViewModel.cs @@ -9,6 +9,7 @@ using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; +using System.Windows.Media.Imaging; using System.Windows.Shapes; using Juick.Api; @@ -90,8 +91,8 @@ namespace Juick } } - private Image _avatar; - public Image UserAvatar + private BitmapImage _avatar; + public BitmapImage UserAvatar { get { return _avatar; } set @@ -147,7 +148,8 @@ namespace Juick } public event PropertyChangedEventHandler PropertyChanged; - private void NotifyPropertyChanged(String propertyName) + + public void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) diff --git a/Juick/ViewModels/ThreadViewModel.cs b/Juick/ViewModels/ThreadViewModel.cs index 48ebe65..bff72b7 100644 --- a/Juick/ViewModels/ThreadViewModel.cs +++ b/Juick/ViewModels/ThreadViewModel.cs @@ -6,6 +6,7 @@ using System.IO; using System.Net; using System.Net.Browser; using System.Runtime.Serialization.Json; +using System.Windows.Media.Imaging; using Juick.Api; using RestSharp; @@ -48,8 +49,22 @@ namespace Juick.ViewModels Items.Clear(); messages.ForEach(post => { - this.Items.Add( - new MessageViewModel(post)); + var item = new MessageViewModel(post); + Items.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"); + }); }); IsDataLoaded = true; NotifyPropertyChanged("Items"); -- cgit v1.2.3