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

namespace Juick.ViewModels
{
    public class ThreadViewModel : INotifyPropertyChanged
    {
        public ThreadViewModel()
        {
            this.Items = new ObservableCollection<MessageViewModel>();
        }

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

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        public MessageViewModel Root { get; set; }

        /// <summary>
        /// Creates and adds a few MessageViewModel objects into the MyFeed collection.
        /// </summary>
        public void LoadData()
        {
            var request = new RestRequest("/thread?mid={mid}" + "&rnd=" + Environment.TickCount);
            request.AddUrlSegment("mid", string.Format("{0}",Root.MID));
            App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
            App.Client.ExecuteAsync(request, response =>
                                                 {
                                                     using (var responseStream = new MemoryStream(response.RawBytes))
                                                     {
                                                         var ser = new DataContractJsonSerializer(typeof (List<Message>));
                                                         var messages = (List<Message>) ser.ReadObject(responseStream);
                                                         Items.Clear();
                                                         messages.ForEach(post =>
                                                                              {
                                                                                  var item = new MessageViewModel(post);
                                                                                  Items.Add(item);
                                                                                  var imageRequest =
                                                                                      new RestRequest(
                                                                                          string.Format("/as/{0}.png",
                                                                                                        post.user.uid));
                                                                                  App.AvatarClient.ExecuteAsync(
                                                                                      imageRequest, restResponse =>
                                                                                      {
                                                                                          item.UserAvatar = new BitmapImage
                                                                                                  ();
                                                                                          item.UserAvatar.SetSource(new MemoryStream(restResponse.RawBytes));
                                                                                          item.
                                                                                              NotifyPropertyChanged
                                                                                              ("UserAvatar");
                                                                                      });
                                                                              });
                                                         IsDataLoaded = true;
                                                         NotifyPropertyChanged("Items");
                                                     }
                                                 });
        }
        
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}