MainViewModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright © 2011-2012 Nokia Corporation. All rights reserved.
  3. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. * Other product and company names mentioned herein may be trademarks
  5. * or trade names of their respective owners.
  6. * See LICENSE.TXT for license information.
  7. */
  8. using System;
  9. using System.Collections.ObjectModel;
  10. using System.Globalization;
  11. using System.IO.IsolatedStorage;
  12. using System.Linq;
  13. using System.Runtime.Serialization;
  14. using System.Windows;
  15. using System.Windows.Resources;
  16. using System.Xml.Linq;
  17. namespace wprestaurantapp
  18. {
  19. /// <summary>
  20. /// Main model class of the application
  21. /// </summary>
  22. public class MainViewModel
  23. {
  24. /// <summary>
  25. /// Constructor
  26. /// </summary>
  27. public MainViewModel()
  28. {
  29. this.Restaurant = new RestaurantData();
  30. }
  31. /// <summary>
  32. /// Property for restaurant data
  33. /// </summary>
  34. public RestaurantData Restaurant { get; private set; }
  35. /// <summary>
  36. /// Property is true if application data has been loaded, otherwise false
  37. /// </summary>
  38. public bool IsDataLoaded
  39. {
  40. get;
  41. private set;
  42. }
  43. /// <summary>
  44. /// Method for loading application data
  45. /// </summary>
  46. public void LoadData()
  47. {
  48. try
  49. {
  50. // Load constant restaurant data from XML file
  51. LoadXML();
  52. }
  53. catch (Exception /*e*/)
  54. {
  55. MessageBox.Show( wprestaurantapp.AppResources.ErrorRestaurantData );
  56. }
  57. // Load non-constant reservation data from persistent storage
  58. LoadReservations();
  59. this.IsDataLoaded = true;
  60. }
  61. /// <summary>
  62. /// Method for saving application data
  63. /// </summary>
  64. public void SaveData()
  65. {
  66. // Save reservation data to persistent storage
  67. SaveReservations();
  68. }
  69. /// <summary>
  70. /// Method for loading restaurant description data from XML file
  71. /// </summary>
  72. private void LoadXML()
  73. {
  74. Uri uri = new Uri("/wprestaurantapp;component/content/restaurant.xml", UriKind.Relative);
  75. StreamResourceInfo xml = App.GetResourceStream(uri);
  76. XDocument doc = XDocument.Load(xml.Stream);
  77. XElement info = doc.Descendants("info").First();
  78. Restaurant.Name = info.Element("name").Value;
  79. Restaurant.LogoURI = info.Element("logo").Value;
  80. XElement address = info.Element("address");
  81. Restaurant.StreetAddress = address.Element("street").Value;
  82. Restaurant.City = address.Element("city").Value;
  83. Restaurant.Country = address.Element("country").Value;
  84. XElement coordinates = address.Element("coordinates");
  85. // Make sure that latitude&longitude values are parsed with US locale. This is because some locales
  86. // use '.' as decimal point, and some use ','. XML file is formatted according to US locale which uses
  87. // '.', so we must explicitly set it, otherwise parsing would fail in some locales.
  88. IFormatProvider format = new CultureInfo("en-US");
  89. Restaurant.Location.Latitude = double.Parse(coordinates.Element("latitude").Value, format);
  90. Restaurant.Location.Longitude = double.Parse(coordinates.Element("longitude").Value, format);
  91. Restaurant.Telephone = info.Element("telephone").Value;
  92. Restaurant.URL = info.Element("url").Value;
  93. Restaurant.Description = info.Element("description").Value;
  94. XElement menu = doc.Descendants("menu").First();
  95. Restaurant.Categories = (from category in menu.Descendants("category")
  96. select new Category()
  97. {
  98. Id = category.Attribute("id").Value,
  99. Name = category.Attribute("name").Value,
  100. IconURI = category.Attribute("icon").Value,
  101. Dishes = (from dish in category.Descendants("dish")
  102. select new Dish()
  103. {
  104. Name = dish.Attribute("name").Value,
  105. Text = dish.Value
  106. }).ToList<Dish>()
  107. }).ToList<Category>();
  108. }
  109. /// <summary>
  110. /// Method for loading reservations from isolated storage
  111. /// </summary>
  112. private void LoadReservations()
  113. {
  114. try
  115. {
  116. using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
  117. {
  118. using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("reservations.dat", System.IO.FileMode.OpenOrCreate, file))
  119. {
  120. if (stream.Length > 0)
  121. {
  122. DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<Reservation>));
  123. Restaurant.Reservations = serializer.ReadObject(stream) as ObservableCollection<Reservation>;
  124. }
  125. }
  126. }
  127. }
  128. catch (IsolatedStorageException e)
  129. {
  130. Console.WriteLine( "Exception occurred while trying to load reservations from isolated storage: " + e.Message);
  131. }
  132. }
  133. /// <summary>
  134. /// Method for saving reservations to isolated storage
  135. /// </summary>
  136. private void SaveReservations()
  137. {
  138. try
  139. {
  140. using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
  141. {
  142. using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("reservations.dat", System.IO.FileMode.Create, file))
  143. {
  144. DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<Reservation>));
  145. serializer.WriteObject(stream, Restaurant.Reservations);
  146. }
  147. }
  148. }
  149. catch (IsolatedStorageException e)
  150. {
  151. Console.WriteLine( "Exception occurred while trying to save reservations to isolated storage: " + e.Message);
  152. }
  153. }
  154. }
  155. }