summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/PageViewModel.cs
blob: 152e9d902e2e033d56c44c8c34696c4ec4db8659 (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
83
84
85
86
87
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 == null || response.ErrorException != null)
                {
                    return;
                }
                if (response.Data == null)
                    return;
                response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
            });
            _context.IsDataLoading = true;
        }
    }
}