PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost.Mimir/Features/General/Pivot/ViewModel.cs

#
C# | 87 lines | 54 code | 14 blank | 19 comment | 1 complexity | e3626c32c0340890bcad3a041e409936 MD5 | raw file
 1#region License
 2//
 3// Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
 4//
 5// Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
 6// With one exception :
 7//   Commercial libraries that is based partly or fully on Bifrost and is sold commercially, 
 8//   must obtain a commercial license.
 9//
10// You may not use this file except in compliance with the License.
11// You may obtain a copy of the license at 
12//
13//   http://bifrost.codeplex.com/license
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21#endregion
22
23using System;
24using System.Collections.Generic;
25using System.Collections.ObjectModel;
26using System.IO;
27using System.Net;
28using System.Runtime.Serialization;
29using Bifrost.Events;
30using Bifrost.Interaction;
31using System.Windows.Input;
32using Bifrost.Serialization;
33using System.Windows;
34
35namespace Bifrost.Mimir.Features.General.Pivot
36{
37    public class ViewModel
38	{
39        ISerializer _serializer;
40
41    	public ViewModel(ISerializer serializer)
42        {
43            Events = new ObservableCollection<EventHolder>();
44            _serializer = serializer;
45            ReloadCommand = DelegateCommand.Create(Reload);
46            Load();
47        }
48
49
50        public virtual ObservableCollection<EventHolder> Events { get; private set; }
51        public virtual ICommand ReloadCommand { get; private set; }
52
53
54        public void Load()
55        {
56            
57
58            var webClient = new WebClient();
59            webClient.DownloadStringCompleted += (s, e) =>
60            {
61                var serializer = new DataContractSerializer(typeof(string));
62                var bytes = System.Text.Encoding.UTF8.GetBytes(e.Result);
63                var memoryStream = new MemoryStream(bytes);
64                var eventsAsJson = (string)serializer.ReadObject(memoryStream);
65
66                var events = _serializer.FromJson<List<EventHolder>>(eventsAsJson);
67                Events.Clear();
68                foreach (var @event in events)
69                    Events.Add(@event);
70            };
71
72            
73            var source = Application.Current.Host.Source;
74            var url = string.Format("{0}://{1}{2}/Events/GetAllAsJsonString",
75                source.Scheme,
76                source.Host,
77                source.Port == 80 ? string.Empty : ":" + source.Port);
78
79            webClient.DownloadStringAsync(new Uri(url));
80        }
81
82        public void Reload()
83        {
84            Load();
85        }
86	}
87}