summaryrefslogtreecommitdiff
path: root/Juick/ViewModels
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2013-03-29 13:21:34 +0400
committerGravatar Vitaly Takmazov2013-03-29 13:21:34 +0400
commit3be6eccab55ca90a060cb56882c5675a7ad875ce (patch)
tree9fe780d5ed3067b7e55aaec691e360066182778b /Juick/ViewModels
parente20d55789c6ab0c2c6324eb6806c12cc96723eca (diff)
there is no need to implement INotifyPropertyChanged on post item
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r--Juick/ViewModels/MessageViewModel.cs186
-rw-r--r--Juick/ViewModels/PostItem.cs46
-rw-r--r--Juick/ViewModels/ThreadViewModel.cs2
-rw-r--r--Juick/ViewModels/ViewModelBase.cs (renamed from Juick/ViewModels/MessageListViewModelBase.cs)236
4 files changed, 165 insertions, 305 deletions
diff --git a/Juick/ViewModels/MessageViewModel.cs b/Juick/ViewModels/MessageViewModel.cs
deleted file mode 100644
index 750864a..0000000
--- a/Juick/ViewModels/MessageViewModel.cs
+++ /dev/null
@@ -1,186 +0,0 @@
-using System;
-using System.ComponentModel;
-using System.Net;
-using System.Windows.Media.Imaging;
-using JuickApi;
-
-namespace Juick.ViewModels
-{
- public class MessageViewModel : INotifyPropertyChanged
- {
- public MessageViewModel()
- {
-
- }
-
- public MessageViewModel(Message message)
- {
- MID = message.Mid;
- MessageUri = string.Format("/ThreadView.xaml?mid={0}", MID);
- 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 _viewModelBase 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 _viewModelBase 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 _viewModelBase 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 Uri _avatarUri;
- public Uri AvatarUri
- {
- get { return _avatarUri; }
- set
- {
- if (value != _avatarUri)
- {
- _avatarUri = value;
- NotifyPropertyChanged("AvatarUri");
- }
- }
- }
-
- private Uri _attach;
- public Uri Attachment
- {
- get { return _attach; }
- set
- {
- if (value != _attach)
- {
- _attach = value;
- NotifyPropertyChanged("Attachment");
- }
- }
- }
-
- private string _messageText;
- /// <summary>
- /// Sample _viewModelBase 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 _viewModelBase 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");
- }
- }
- }
-
- private string _messageUri;
-
- public string MessageUri
- {
- get { return _messageUri; }
- set
- {
- _messageUri = value;
- NotifyPropertyChanged("MessageUri");
- }
- }
-
- 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/PostItem.cs b/Juick/ViewModels/PostItem.cs
new file mode 100644
index 0000000..7e30a43
--- /dev/null
+++ b/Juick/ViewModels/PostItem.cs
@@ -0,0 +1,46 @@
+using System;
+using System.ComponentModel;
+using System.Net;
+using System.Windows.Media.Imaging;
+using JuickApi;
+
+namespace Juick.ViewModels
+{
+ public class PostItem
+ {
+ public PostItem()
+ {
+
+ }
+
+ public PostItem(Message message)
+ {
+ MID = message.Mid;
+ MessageUri = string.Format("/ThreadView.xaml?mid={0}", MID);
+ RID = message.Rid;
+ Username = message.User.UName;
+ MessageText = HttpUtility.HtmlDecode(message.Body);
+
+ if (message.Tags != null)
+ {
+ MessageText = string.Join(", ", message.Tags) + Environment.NewLine + MessageText;
+ }
+ }
+
+ public int MID {get;set;}
+
+ public int RID {get;set;}
+
+ public string Username {get;set;}
+
+ public Uri AvatarUri {get;set;}
+
+ public Uri Attachment {get;set;}
+
+ public string MessageText {get;set;}
+ public string Status {get;set;}
+
+ public string MessageUri {get;set;}
+
+ }
+} \ No newline at end of file
diff --git a/Juick/ViewModels/ThreadViewModel.cs b/Juick/ViewModels/ThreadViewModel.cs
index ec1f92d..1cbaab7 100644
--- a/Juick/ViewModels/ThreadViewModel.cs
+++ b/Juick/ViewModels/ThreadViewModel.cs
@@ -2,7 +2,7 @@
namespace Juick.ViewModels
{
- public class ThreadViewModel : MessageListViewModelBase
+ public class ThreadViewModel : ViewModelBase
{
static readonly string CaptionPropertyName = ExpressionHelper.GetPropertyName<ThreadViewModel>(x => x.Caption);
diff --git a/Juick/ViewModels/MessageListViewModelBase.cs b/Juick/ViewModels/ViewModelBase.cs
index 3eb1842..e86ff4a 100644
--- a/Juick/ViewModels/MessageListViewModelBase.cs
+++ b/Juick/ViewModels/ViewModelBase.cs
@@ -1,118 +1,118 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.ComponentModel;
-using System.Net;
-using System.Windows;
-using System.Windows.Media.Imaging;
-using Juick.Classes;
-using JuickApi;
-using RestSharp;
-
-namespace Juick.ViewModels
-{
- public class MessageListViewModelBase : INotifyPropertyChanged
- {
- static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<MessageListViewModelBase>(x => x.IsDataLoading);
- bool isDataLoading;
-
- public MessageListViewModelBase()
- {
- Items = new ObservableCollection<MessageViewModel>();
- LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading);
- }
-
- public string RestUri { get; set; }
- public virtual string Caption { get { return "juick"; } }
-
- /// <summary>
- /// A collection for MessageViewModel objects.
- /// </summary>
- public ObservableCollection<MessageViewModel> Items { get; private set; }
-
- public DelegateCommand LoadMessagesPageCommand { get; private set; }
-
- public bool IsDataLoading
- {
- get { return isDataLoading; }
- set
- {
- isDataLoading = value;
- NotifyPropertyChanged(IsDataLoadingPropertyName);
- LoadMessagesPageCommand.NotifyCanExecuteChanged();
- }
- }
-
- /// <summary>
- /// Creates and adds a few MessageViewModel objects into the Items collection.
- /// </summary>
- public void LoadData()
- {
- if (IsDataLoading) {
- return;
- }
-
- const int PageSize = 1;
-
- if (string.IsNullOrEmpty(RestUri))
- {
- RestUri = "/home?1=1";
- }
-
- // super-костыли
- // todo: rewrite
- else if (RestUri.StartsWith("/home?", StringComparison.InvariantCulture) && Items.Count > 0)
- {
- var lastItem = Items[Items.Count - 1];
- RestUri = string.Format("/home?before_mid={0}&page={1}", lastItem.MID, PageSize);
- }
- else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0)
- {
- var lastItem = Items[Items.Count - 1];
- RestUri = string.Format("/messages?before_mid={0}&page={1}", lastItem.MID, PageSize);
- }
-
- var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
- App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
- App.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
- IsDataLoading = true;
- }
-
- void ProcessResponse(IRestResponse<List<Message>> response)
- {
- IsDataLoading = false;
- if (response.StatusCode != HttpStatusCode.OK)
- {
- MessageBox.Show(response.StatusCode.ToString());
- return;
- }
-
- //Items.Clear();
- foreach (var post in response.Data)
- {
- var status = string.Format("Posted on: {0}, replies: {1}", post.Timestamp, post.Replies);
- var item = new MessageViewModel(post)
- {
- Status = status,
- AvatarUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute)
- };
- if (post.Photo != null)
- {
- item.Attachment = new Uri(post.Photo.Small, UriKind.Absolute) ;
- }
- Items.Add(item);
- }
- }
-
-
- public event PropertyChangedEventHandler PropertyChanged;
-
- public void NotifyPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Net;
+using System.Windows;
+using System.Windows.Media.Imaging;
+using Juick.Classes;
+using JuickApi;
+using RestSharp;
+
+namespace Juick.ViewModels
+{
+ public class ViewModelBase : INotifyPropertyChanged
+ {
+ static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<ViewModelBase>(x => x.IsDataLoading);
+ bool isDataLoading;
+
+ public ViewModelBase()
+ {
+ Items = new ObservableCollection<PostItem>();
+ LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading);
+ }
+
+ public string RestUri { get; set; }
+ public virtual string Caption { get { return "juick"; } }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<PostItem> Items { get; private set; }
+
+ public DelegateCommand LoadMessagesPageCommand { get; private set; }
+
+ public bool IsDataLoading
+ {
+ get { return isDataLoading; }
+ set
+ {
+ isDataLoading = value;
+ NotifyPropertyChanged(IsDataLoadingPropertyName);
+ LoadMessagesPageCommand.NotifyCanExecuteChanged();
+ }
+ }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the Items collection.
+ /// </summary>
+ public void LoadData()
+ {
+ if (IsDataLoading) {
+ return;
+ }
+
+ const int PageSize = 1;
+
+ if (string.IsNullOrEmpty(RestUri))
+ {
+ RestUri = "/home?1=1";
+ }
+
+ // super-костыли
+ // todo: rewrite
+ else if (RestUri.StartsWith("/home?", StringComparison.InvariantCulture) && Items.Count > 0)
+ {
+ var lastItem = Items[Items.Count - 1];
+ RestUri = string.Format("/home?before_mid={0}&page={1}", lastItem.MID, PageSize);
+ }
+ else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0)
+ {
+ var lastItem = Items[Items.Count - 1];
+ RestUri = string.Format("/messages?before_mid={0}&page={1}", lastItem.MID, PageSize);
+ }
+
+ var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
+ App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
+ App.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
+ IsDataLoading = true;
+ }
+
+ void ProcessResponse(IRestResponse<List<Message>> response)
+ {
+ IsDataLoading = false;
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ MessageBox.Show(response.StatusCode.ToString());
+ return;
+ }
+
+ //Items.Clear();
+ foreach (var post in response.Data)
+ {
+ var status = string.Format("Posted on: {0}, replies: {1}", post.Timestamp, post.Replies);
+ var item = new PostItem(post)
+ {
+ Status = status,
+ AvatarUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute)
+ };
+ if (post.Photo != null)
+ {
+ item.Attachment = new Uri(post.Photo.Small, UriKind.Absolute) ;
+ }
+ Items.Add(item);
+ }
+ }
+
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ public void NotifyPropertyChanged(string propertyName)
+ {
+ if (PropertyChanged != null)
+ {
+ PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+ }
+}