From 4959050e7996e92ac97f4d05064130251a0628c5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 3 Apr 2013 01:23:55 +0400 Subject: Viewmodels refactoring --- Juick/ViewModels/AppViewModel.cs | 6 ++- Juick/ViewModels/PageViewModel.cs | 87 +++++++++++++++------------------------ 2 files changed, 37 insertions(+), 56 deletions(-) (limited to 'Juick/ViewModels') diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs index 50b95b8..77970df 100644 --- a/Juick/ViewModels/AppViewModel.cs +++ b/Juick/ViewModels/AppViewModel.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; using Juick.Classes; +using JuickApi; using RestSharp; namespace Juick.ViewModels @@ -8,7 +11,7 @@ namespace Juick.ViewModels public class AppViewModel : ViewModelBase { private ObservableCollection _pages; - public ObservableCollection Pages + public ObservableCollection Pages { get { return _pages ?? (_pages = new ObservableCollection()); } } @@ -48,6 +51,5 @@ namespace Juick.ViewModels }); } } - } } diff --git a/Juick/ViewModels/PageViewModel.cs b/Juick/ViewModels/PageViewModel.cs index ff64dac..02f86ff 100644 --- a/Juick/ViewModels/PageViewModel.cs +++ b/Juick/ViewModels/PageViewModel.cs @@ -18,18 +18,25 @@ namespace Juick.ViewModels { _context = context; Items = new ObservableCollection(); - LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !context.IsDataLoading); + LoadMessagesPageCommand = new DelegateCommand(CheckNewData, () => !context.IsDataLoading); NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true); } private readonly AppViewModel _context; + private string _restUri; + + public string RestUri + { + get { return _restUri; } + set + { + _restUri = value; + RefreshData(); + } + } - public string RestUri { get; set; } public virtual string Caption { get { return "juick"; } } - /// - /// A collection for MessageViewModel objects. - /// public ObservableCollection Items { get; private set; } public DelegateCommand LoadMessagesPageCommand { get; private set; } @@ -38,66 +45,38 @@ namespace Juick.ViewModels public void NavigateToThread(SelectionChangedEventArgs param) { - if (param != null) - { - ((App)App.Current).NavigateTo(new Uri(string.Format("/ThreadView.xaml?mid={0}", ((PostItem)param.AddedItems[0]).MID), UriKind.Relative)); - } - + if (param == null) return; + if (param.AddedItems.Count > 0) + ((App)Application.Current).NavigateTo( + new Uri(string.Format("/ThreadView.xaml?mid={0}", ((PostItem)param.AddedItems[0]).MID), UriKind.Relative)); } - /// - /// Creates and adds a few MessageViewModel objects into the Items collection. - /// - public void LoadData(LinkUnlinkEventArgs e) + public void CheckNewData(LinkUnlinkEventArgs e) { const int offset = 5; - if (_context.IsDataLoading) - { - return; - } - if (e != null) - { - var item = e.ContentPresenter.Content; - if (!item.Equals(Items[Items.Count - offset])) { - return; - } - } - - 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}", lastItem.MID); - } - else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0) - { - var lastItem = Items[Items.Count - 1]; - RestUri = string.Format("/messages?before_mid={0}", lastItem.MID); + if (e == null) return; + var item = e.ContentPresenter.Content; + if (item.Equals(Items[Items.Count - offset])) { + RefreshData(); } - - var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount); - _context.Client.Authenticator = new HttpBasicAuthenticator(_context.Account.Credentials.UserName, _context.Account.Credentials.Password); - _context.Client.ExecuteAsync>(request, ProcessResponse); - _context.IsDataLoading = true; } - void ProcessResponse(IRestResponse> response) + public void RefreshData() { - _context.IsDataLoading = false; - if (response.StatusCode != HttpStatusCode.OK) + var requestUri = RestUri + "&rnd=" + Environment.TickCount; + if (Items.Count > 0) { - MessageBox.Show(response.StatusCode.ToString()); - return; + requestUri += string.Format("&before_mid={0}", Items[Items.Count - 1].MID); } - //Items.Clear(); - response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i)); + var request = new RestRequest(requestUri); + _context.Client.Authenticator = new HttpBasicAuthenticator(_context.Account.Credentials.UserName, _context.Account.Credentials.Password); + _context.Client.ExecuteAsync>(request, response => + { + _context.IsDataLoading = false; + response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i)); + }); + _context.IsDataLoading = true; } } } -- cgit v1.2.3