PageRenderTime 40ms CodeModel.GetById 21ms 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
Possible License(s): CC-BY-SA-3.0
  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. using System;
  23. using System.Collections.Generic;
  24. using System.Collections.ObjectModel;
  25. using System.IO;
  26. using System.Net;
  27. using System.Runtime.Serialization;
  28. using Bifrost.Events;
  29. using Bifrost.Interaction;
  30. using System.Windows.Input;
  31. using Bifrost.Serialization;
  32. using System.Windows;
  33. namespace Bifrost.Mimir.Features.General.Pivot
  34. {
  35. public class ViewModel
  36. {
  37. ISerializer _serializer;
  38. public ViewModel(ISerializer serializer)
  39. {
  40. Events = new ObservableCollection<EventHolder>();
  41. _serializer = serializer;
  42. ReloadCommand = DelegateCommand.Create(Reload);
  43. Load();
  44. }
  45. public virtual ObservableCollection<EventHolder> Events { get; private set; }
  46. public virtual ICommand ReloadCommand { get; private set; }
  47. public void Load()
  48. {
  49. var webClient = new WebClient();
  50. webClient.DownloadStringCompleted += (s, e) =>
  51. {
  52. var serializer = new DataContractSerializer(typeof(string));
  53. var bytes = System.Text.Encoding.UTF8.GetBytes(e.Result);
  54. var memoryStream = new MemoryStream(bytes);
  55. var eventsAsJson = (string)serializer.ReadObject(memoryStream);
  56. var events = _serializer.FromJson<List<EventHolder>>(eventsAsJson);
  57. Events.Clear();
  58. foreach (var @event in events)
  59. Events.Add(@event);
  60. };
  61. var source = Application.Current.Host.Source;
  62. var url = string.Format("{0}://{1}{2}/Events/GetAllAsJsonString",
  63. source.Scheme,
  64. source.Host,
  65. source.Port == 80 ? string.Empty : ":" + source.Port);
  66. webClient.DownloadStringAsync(new Uri(url));
  67. }
  68. public void Reload()
  69. {
  70. Load();
  71. }
  72. }
  73. }