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/App.xaml.cs | 2 +- Juick/MainPage.xaml.cs | 11 +---- Juick/ThreadView.xaml.cs | 1 - Juick/ViewModels/AppViewModel.cs | 6 ++- Juick/ViewModels/PageViewModel.cs | 87 +++++++++++++++------------------------ 5 files changed, 40 insertions(+), 67 deletions(-) (limited to 'Juick') diff --git a/Juick/App.xaml.cs b/Juick/App.xaml.cs index 8662972..98dc31c 100644 --- a/Juick/App.xaml.cs +++ b/Juick/App.xaml.cs @@ -22,7 +22,7 @@ namespace Juick // Delay creation of the view model until necessary if (context == null) { context = new AppViewModel(); - context.Pages.Add(new PageViewModel(context)); + context.Pages.Add(new PageViewModel(context) { RestUri = "/home?1=1" }); context.Pages.Add(new PageViewModel(context) { RestUri = "/messages?1=1" }); }; return context; diff --git a/Juick/MainPage.xaml.cs b/Juick/MainPage.xaml.cs index 4861114..a634e7f 100644 --- a/Juick/MainPage.xaml.cs +++ b/Juick/MainPage.xaml.cs @@ -112,13 +112,6 @@ namespace Juick { NavigationService.Navigate(new Uri(navigateUri, UriKind.Relative)); } - else - { - if (((ViewModels.PageViewModel)Home.DataContext).Items.Count == 0) - ((ViewModels.PageViewModel)Home.DataContext).LoadData(null); - if (((ViewModels.PageViewModel)Last.DataContext).Items.Count == 0) - ((ViewModels.PageViewModel)Last.DataContext).LoadData(null); - } } void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) @@ -172,8 +165,8 @@ namespace Juick { App.AppContext.Pages[0].Items.Clear(); App.AppContext.Pages[1].Items.Clear(); - App.AppContext.Pages[0].LoadData(null); - App.AppContext.Pages[1].LoadData(null); + App.AppContext.Pages[0].RefreshData(); + App.AppContext.Pages[1].RefreshData(); } private void ApplicationBarMenuItemClick(object sender, EventArgs e) diff --git a/Juick/ThreadView.xaml.cs b/Juick/ThreadView.xaml.cs index 50ea979..5c1c0fc 100644 --- a/Juick/ThreadView.xaml.cs +++ b/Juick/ThreadView.xaml.cs @@ -27,7 +27,6 @@ namespace Juick if (NavigationContext.QueryString.TryGetValue("mid", out _mid)) { Model.Mid = int.Parse(_mid); - Model.LoadData(null); } } 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