summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/ViewModelBase.cs
blob: 746ce0465b2952fd6645520cdc4c02a9b082175f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Media.Imaging;
using Juick.Classes;
using JuickApi;
using RestSharp;
using System.Diagnostics;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Windows.Controls;

namespace Juick.ViewModels
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<ViewModelBase>(x => x.IsDataLoading);
        bool isDataLoading;

        public ViewModelBase()
        {
            Items = new ObservableCollection<PostItem>();
            LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading);
            NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true);
        }

        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 LoadMessagesPageCommand { get; private set; }

        public DelegateCommand NavigateNextCommand { get; private set; }

        public bool IsDataLoading
        {
            get { return isDataLoading; }
            set
            {
                isDataLoading = value;
                NotifyPropertyChanged(IsDataLoadingPropertyName);
                LoadMessagesPageCommand.NotifyCanExecuteChanged();
            }
        }

        public void NavigateToThread(object param)
        {
            var selectionChangedEventArgs = param as SelectionChangedEventArgs;
            if (param != null)
            {
                ((App)App.Current).NavigateTo(new Uri(string.Format("/ThreadView.xaml?mid={0}", ((PostItem)selectionChangedEventArgs.AddedItems[0]).MID), UriKind.Relative));
            }
            
        }

        /// <summary>
        /// Creates and adds a few MessageViewModel objects into the Items collection.
        /// </summary>
        public void LoadData(object EventArgs)
        {
            const int offset = 5;
            if (IsDataLoading)
            {
                return;
            }
            if (EventArgs != null)
            {
                var item = (EventArgs as LinkUnlinkEventArgs).ContentPresenter.Content;
                if (!item.Equals(Items[Items.Count - offset])) {
                    return;
                }
            }

            int count = Items.Count;
            

            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);
            }

            var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
            App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
            App.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
            IsDataLoading = true;
        }

        void ProcessResponse(IRestResponse<List<Message>> response)
        {
            IsDataLoading = false;
            if (response.StatusCode != HttpStatusCode.OK)
            {
                MessageBox.Show(response.StatusCode.ToString());
                return;
            }

            //Items.Clear();
            response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}