summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/PageViewModel.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/ViewModels/PageViewModel.cs')
-rw-r--r--Juick/ViewModels/PageViewModel.cs87
1 files changed, 33 insertions, 54 deletions
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;
}
}
}