summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/MainViewModel.cs
blob: 3ee4a1612be905c221b09311448a78b721528314 (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
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Juick.Api;
using RestSharp;

namespace Juick.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.MyFeed = new ObservableCollection<MessageViewModel>();
            this.Last = new ObservableCollection<MessageViewModel>();
        }
        
        /// <summary>
        /// A collection for MessageViewModel objects.
        /// </summary>
        public ObservableCollection<MessageViewModel> MyFeed { get; private set; }

        /// <summary>
        /// A collection for MessageViewModel objects.
        /// </summary>
        public ObservableCollection<MessageViewModel> Last { get; private set; }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few MessageViewModel objects into the MyFeed collection.
        /// </summary>
        public void LoadData()
        {
            var request = new RestRequest("/home?1=1" + "&rnd=" + Environment.TickCount);
            App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
            App.Client.ExecuteAsync<List<Message>>(request, response => 
                                                 {
                                                     if (response.StatusCode != HttpStatusCode.OK)
                                                     {
                                                         MessageBox.Show(response.StatusCode.ToString());
                                                         return;
                                                     }

                                                     var messages = response.Data;
                                                     MyFeed.Clear();
                                                     messages.ForEach(post =>
                                                                          {
                                                                              var item = new MessageViewModel(post)
                                                                                             {
                                                                                                 Status =
                                                                                                     string.Format(
                                                                                                         "Posted on: {0}, replies: {1}",
                                                                                                         post.Timestamp,
                                                                                                         post.Replies)
                                                                                             };
                                                                              MyFeed.Add(item);
                                                                              var imageUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute);
                                                                              item.UserAvatar = new BitmapImage
                                                                                                    {
                                                                                                        UriSource = imageUri
                                                                                                    };
                                                                              item.NotifyPropertyChanged("UserAvatar");
                                                                              
                                                                          });
                                                         NotifyPropertyChanged("MyFeed");
                                                     
                                                 });
            var lastrequest = new RestRequest("/messages?1=1&media=all" + "&rnd=" + Environment.TickCount);
            App.Client.ExecuteAsync<List<Message>>(lastrequest, response =>
            {
                
                    var messages = response.Data;
                    if (messages.Count == 0) return;
                    Last.Clear();
                    messages.ForEach(post => 
                    {
                        var item = new MessageViewModel(post)
                        {
                            Status =
                                string.Format(
                                    "Posted on: {0}, replies: {1}",
                                    post.Timestamp,
                                    post.Replies)
                        };
                        Last.Add(item);
                        var imageUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute);
                        item.UserAvatar = new BitmapImage {UriSource = imageUri};
                        item.NotifyPropertyChanged("UserAvatar");
                        
                        if (post.Photo != null)
                        {
                            item.Attachment = new BitmapImage {UriSource = new Uri(post.Photo.Small, UriKind.Absolute)};
                            item.NotifyPropertyChanged("Attachment");
                        }
                        
                    });
                    NotifyPropertyChanged("Last");
                
            });
        }

       
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}