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.cs165
1 files changed, 83 insertions, 82 deletions
diff --git a/Juick/ViewModels/PageViewModel.cs b/Juick/ViewModels/PageViewModel.cs
index 0d57f6a..9781a9e 100644
--- a/Juick/ViewModels/PageViewModel.cs
+++ b/Juick/ViewModels/PageViewModel.cs
@@ -1,82 +1,83 @@
-using System;
-using System.Linq;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Net;
-using System.Windows;
-using Juick.Classes;
-using JuickApi;
-using RestSharp;
-using Microsoft.Phone.Controls;
-using System.Windows.Controls;
-
-namespace Juick.ViewModels
-{
- public class PageViewModel : ViewModelBase
- {
- public PageViewModel(AppViewModel context)
- {
- _context = context;
- Items = new ObservableCollection<PostItem>();
- 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;
- }
- }
-
- public virtual string Caption { get; set; }
-
- public ObservableCollection<PostItem> Items { get; private set; }
-
- public DelegateCommand<LinkUnlinkEventArgs> LoadMessagesPageCommand { get; private set; }
-
- public DelegateCommand<SelectionChangedEventArgs> NavigateNextCommand { get; private set; }
-
- public void NavigateToThread(SelectionChangedEventArgs param)
- {
- 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));
- }
-
- public void CheckNewData(LinkUnlinkEventArgs e)
- {
- const int offset = 5;
- if (e == null) return;
- var item = e.ContentPresenter.Content;
- if (item.Equals(Items[Items.Count - offset])) {
- RefreshData();
- }
- }
-
- public void RefreshData()
- {
- var requestUri = RestUri + "&rnd=" + Environment.TickCount;
- if (Items.Count > 0)
- {
- requestUri += string.Format("&before_mid={0}", Items[Items.Count - 1].MID);
- }
-
- var request = new RestRequest(requestUri);
- _context.Client.ExecuteAsync<List<Message>>(request, response =>
- {
- _context.IsDataLoading = false;
- if (response.Data == null)
- return;
- response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
- });
- _context.IsDataLoading = true;
- }
- }
-}
+using System;
+using System.Linq;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using Juick.Classes;
+using JuickApi;
+using RestSharp;
+using Microsoft.Phone.Controls;
+
+namespace Juick.ViewModels
+{
+ public class PageViewModel : ViewModelBase
+ {
+ public PageViewModel(AppViewModel context)
+ {
+ _context = context;
+ Items = new ObservableCollection<PostItem>();
+ LoadMessagesPageCommand = new DelegateCommand<ItemRealizationEventArgs>(CheckNewData, () => !context.IsDataLoading);
+ NavigateNextCommand = new DelegateCommand<GestureEventArgs>(NavigateToThread, () => true);
+ }
+
+ private readonly AppViewModel _context;
+ private string _restUri;
+
+ public string RestUri
+ {
+ get { return _restUri; }
+ set
+ {
+ _restUri = value;
+ }
+ }
+
+ public virtual string Caption { get; set; }
+
+ public ObservableCollection<PostItem> Items { get; private set; }
+
+ public DelegateCommand<ItemRealizationEventArgs> LoadMessagesPageCommand { get; private set; }
+
+ public DelegateCommand<GestureEventArgs> NavigateNextCommand { get; private set; }
+
+ public void NavigateToThread(GestureEventArgs param)
+ {
+ PostItem post = ((FrameworkElement)param.OriginalSource).DataContext as PostItem;
+
+ if (post != null)
+ ((App)Application.Current).NavigateTo(
+ new Uri(string.Format("/ThreadView.xaml?mid={0}", post.MID), UriKind.Relative));
+ }
+
+ public void CheckNewData(ItemRealizationEventArgs e)
+ {
+ const int offset = 5;
+ if (e == null) return;
+ var item = e.Container.Content;
+ if (item.Equals(Items[Items.Count - offset])) {
+ RefreshData();
+ }
+ }
+
+ public void RefreshData()
+ {
+ var requestUri = RestUri + "&rnd=" + Environment.TickCount;
+ if (Items.Count > 0)
+ {
+ requestUri += string.Format("&before_mid={0}", Items[Items.Count - 1].MID);
+ }
+
+ var request = new RestRequest(requestUri);
+ _context.Client.ExecuteAsync<List<Message>>(request, response =>
+ {
+ _context.IsDataLoading = false;
+ if (response.Data == null)
+ return;
+ response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
+ });
+ _context.IsDataLoading = true;
+ }
+ }
+}