diff options
author | Vitaly Takmazov | 2013-04-03 01:23:55 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2013-04-03 01:23:55 +0400 |
commit | 4959050e7996e92ac97f4d05064130251a0628c5 (patch) | |
tree | d7e8e84a9d1c35ce3e1fd3a20f1b33d0e8034a20 | |
parent | cc4665499b182c32481d506d2e1e65ba41a6370b (diff) |
Viewmodels refactoring
-rw-r--r-- | Juick/App.xaml.cs | 2 | ||||
-rw-r--r-- | Juick/MainPage.xaml.cs | 11 | ||||
-rw-r--r-- | Juick/ThreadView.xaml.cs | 1 | ||||
-rw-r--r-- | Juick/ViewModels/AppViewModel.cs | 6 | ||||
-rw-r--r-- | Juick/ViewModels/PageViewModel.cs | 87 |
5 files changed, 40 insertions, 67 deletions
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<PageViewModel> _pages; - public ObservableCollection<PageViewModel> Pages + public ObservableCollection<PageViewModel> Pages { get { return _pages ?? (_pages = new ObservableCollection<PageViewModel>()); } } @@ -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<PostItem>(); - LoadMessagesPageCommand = new DelegateCommand<LinkUnlinkEventArgs>(LoadData, () => !context.IsDataLoading); + LoadMessagesPageCommand = new DelegateCommand<LinkUnlinkEventArgs>(CheckNewData, () => !context.IsDataLoading); NavigateNextCommand = new DelegateCommand<SelectionChangedEventArgs>(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"; } } - /// <summary> - /// A collection for MessageViewModel objects. - /// </summary> public ObservableCollection<PostItem> Items { get; private set; } public DelegateCommand<LinkUnlinkEventArgs> 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)); } - /// <summary> - /// Creates and adds a few MessageViewModel objects into the Items collection. - /// </summary> - 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<List<Message>>(request, ProcessResponse); - _context.IsDataLoading = true; } - void ProcessResponse(IRestResponse<List<Message>> 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<List<Message>>(request, response => + { + _context.IsDataLoading = false; + response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i)); + }); + _context.IsDataLoading = true; } } } |