From 8316abc300d56f876a5fe3b511dc202556ad776c Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 24 Oct 2012 23:38:06 +0400 Subject: loading data on scrolling at bottom --- Juick/ViewModels/MessageListViewModelBase.cs | 126 ++++++++++++++++----------- 1 file changed, 73 insertions(+), 53 deletions(-) (limited to 'Juick/ViewModels/MessageListViewModelBase.cs') diff --git a/Juick/ViewModels/MessageListViewModelBase.cs b/Juick/ViewModels/MessageListViewModelBase.cs index 2cf5157..5dc8aed 100644 --- a/Juick/ViewModels/MessageListViewModelBase.cs +++ b/Juick/ViewModels/MessageListViewModelBase.cs @@ -1,37 +1,45 @@ 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.ComponentModel; +using System.Net; using System.Windows; -using System.Windows.Controls; using System.Windows.Media.Imaging; using Juick.Api; using RestSharp; +using System.Windows.Input; +using Juick.Classes; namespace Juick.ViewModels { public class MessageListViewModelBase : INotifyPropertyChanged { + bool isDataLoading; + public MessageListViewModelBase() { - this.Items = new ObservableCollection(); + Items = new ObservableCollection(); + LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading); } public string RestUri { get; set; } - + /// /// A collection for MessageViewModel objects. /// public ObservableCollection Items { get; private set; } - - public bool IsDataLoaded + + public DelegateCommand LoadMessagesPageCommand { get; private set; } + + public bool IsDataLoading { - get; - private set; + get { return isDataLoading; } + set + { + isDataLoading = value; + NotifyPropertyChanged("IsDataLoading"); + LoadMessagesPageCommand.NotifyCanExecuteChanged(); + } } /// @@ -39,57 +47,69 @@ namespace Juick.ViewModels /// public void LoadData() { + if (IsDataLoading) { + return; + } + + const int PageSize = 1; + if (string.IsNullOrEmpty(RestUri)) { RestUri = "/home?1=1"; } - var request = new RestRequest(RestUri +"&rnd=" + Environment.TickCount); + + // 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>(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"); - }); + App.Client.ExecuteAsync>(request, ProcessResponse); + IsDataLoading = true; + } + + void ProcessResponse(IRestResponse> 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 }; + 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) }; + } + } } - + public event PropertyChangedEventHandler PropertyChanged; - public void NotifyPropertyChanged(String propertyName) + + public void NotifyPropertyChanged(string propertyName) { - PropertyChangedEventHandler handler = PropertyChanged; - if (null != handler) + if (PropertyChanged != null) { - handler(this, new PropertyChangedEventArgs(propertyName)); + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } -} \ No newline at end of file +} -- cgit v1.2.3