diff options
author | Vitaly Takmazov | 2012-03-10 22:14:25 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2012-03-10 22:14:25 +0400 |
commit | 2b387fb5626a57e3a6473eed66816df80152cd49 (patch) | |
tree | 0b2f2ede5dcc79c1adbb8b64c2296aaeed1416fb /Juick/ViewModels | |
parent | 4a664984f58b00493a61cb19fafc0ca01d35b930 (diff) |
refactoring, tags
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r-- | Juick/ViewModels/MessageListViewModelBase.cs | 95 | ||||
-rw-r--r-- | Juick/ViewModels/MessageViewModel.cs | 16 | ||||
-rw-r--r-- | Juick/ViewModels/ThreadViewModel.cs | 72 |
3 files changed, 114 insertions, 69 deletions
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<MessageViewModel>();
+ }
+
+ public string RestUri { get; set; }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<MessageViewModel> Items { get; private set; }
+
+ public bool IsDataLoaded
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the Items collection.
+ /// </summary>
+ 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<List<Message>>(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 diff --git a/Juick/ViewModels/MessageViewModel.cs b/Juick/ViewModels/MessageViewModel.cs index 3a324c4..e120c5d 100644 --- a/Juick/ViewModels/MessageViewModel.cs +++ b/Juick/ViewModels/MessageViewModel.cs @@ -2,6 +2,7 @@ using System.ComponentModel;
using System.Net;
using System.Windows.Media.Imaging;
+using System.Linq;
using Juick.Api;
namespace Juick.ViewModels
@@ -19,10 +20,15 @@ namespace Juick.ViewModels RID = message.Rid;
Username = message.User.UName;
MessageText = HttpUtility.HtmlDecode(message.Body);
+
+ if (message.Tags != null)
+ {
+ MessageText = string.Join(", ", message.Tags) + Environment.NewLine + MessageText;
+ }
}
private int _mid;
/// <summary>
- /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// Sample _viewModelBase property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public int MID
@@ -43,7 +49,7 @@ namespace Juick.ViewModels private int _rid;
/// <summary>
- /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// Sample _viewModelBase property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public int RID
@@ -63,7 +69,7 @@ namespace Juick.ViewModels }
private string _username;
/// <summary>
- /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// Sample _viewModelBase property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public string Username
@@ -112,7 +118,7 @@ namespace Juick.ViewModels private string _messageText;
/// <summary>
- /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// Sample _viewModelBase property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public string MessageText
@@ -133,7 +139,7 @@ namespace Juick.ViewModels private string _status;
/// <summary>
- /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
+ /// Sample _viewModelBase property; this property is used in the view to display its value using a Binding.
/// </summary>
/// <returns></returns>
public string Status
diff --git a/Juick/ViewModels/ThreadViewModel.cs b/Juick/ViewModels/ThreadViewModel.cs index a929700..e1f8152 100644 --- a/Juick/ViewModels/ThreadViewModel.cs +++ b/Juick/ViewModels/ThreadViewModel.cs @@ -1,73 +1,17 @@ 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 System.Windows.Media.Imaging;
-using Juick.Api;
-using RestSharp;
namespace Juick.ViewModels
{
- public class ThreadViewModel : INotifyPropertyChanged
+ public class ThreadViewModel : MessageListViewModelBase
{
- public ThreadViewModel()
+ private int _mid;
+ public int Mid
{
- 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<List<Message>>(request, response =>
- {
- var messages = response.Data;
- Items.Clear();
- messages.ForEach(post =>
- {
- var item = new MessageViewModel(post);
- 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
- };
- item.NotifyPropertyChanged("UserAvatar");
- });
- IsDataLoaded = true;
- NotifyPropertyChanged("Items");
-
- });
- }
-
- public event PropertyChangedEventHandler PropertyChanged;
- private void NotifyPropertyChanged(String propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (null != handler)
- {
- handler(this, new PropertyChangedEventArgs(propertyName));
+ get { return _mid; }
+ set
+ {
+ _mid = value;
+ RestUri = string.Format("/thread?mid={0}", _mid) + "&rnd=" + Environment.TickCount;
}
}
}
|