blob: 0d57f6a9427fcc5da5fc3775f71be710d6bc43fd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
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;
}
}
}
|