Util.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Windows.Data.Xml.Dom;
  7. using Windows.Devices.Geolocation;
  8. using Windows.Services.Maps;
  9. using Windows.UI;
  10. using Windows.UI.Notifications;
  11. using Windows.UI.Popups;
  12. using Windows.UI.Xaml.Controls.Maps;
  13. namespace YJMPD_UWP.Helpers
  14. {
  15. class Util
  16. {
  17. public enum DialogType { YESNO, OKCANCEL }
  18. public enum RandomType { ALPHA, NUMERIC, ALPHANUMERIC }
  19. private static string AlphaSource = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  20. private static string NumericSource = "0123456789";
  21. public static double Now { get { return (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } }
  22. public static string Random(int length, RandomType type)
  23. {
  24. string str = "";
  25. string source = "";
  26. switch(type)
  27. {
  28. case RandomType.ALPHA:
  29. source = AlphaSource;
  30. break;
  31. case RandomType.NUMERIC:
  32. source = NumericSource;
  33. break;
  34. default:
  35. case RandomType.ALPHANUMERIC:
  36. source = AlphaSource + NumericSource;
  37. break;
  38. }
  39. Random rand = new Random();
  40. for(int i = 0; i<length; i++)
  41. {
  42. str += source.ElementAt(rand.Next(0,source.Length-1));
  43. }
  44. return str;
  45. }
  46. public static string MillisecondsToTime(double millis)
  47. {
  48. DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  49. string timestr = time.AddMilliseconds(millis) + "";
  50. return timestr;
  51. }
  52. public static string Serialize<E>(E o)
  53. {
  54. return JsonConvert.SerializeObject(o);
  55. }
  56. public static E Deserialize<E>(string s)
  57. {
  58. return JsonConvert.DeserializeObject<E>(s);
  59. }
  60. //GPS
  61. public static async Task<Geopoint> FindLocation(string location, Geopoint reference)
  62. {
  63. MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync(location, reference);
  64. MapLocation from = result.Locations.FirstOrDefault();
  65. Geopoint p = from.Point;
  66. return p;
  67. }
  68. public static async Task<MapRoute> FindWalkingRoute(Geopoint from, Geopoint to)
  69. {
  70. MapRouteFinderResult routeResult = await MapRouteFinder.GetWalkingRouteAsync(from, to);
  71. MapRoute b = routeResult.Route;
  72. return b;
  73. }
  74. public static async Task<MapRoute> FindWalkingRoute(List<Geopoint> points)
  75. {
  76. MapRouteFinderResult routeResult = await MapRouteFinder.GetWalkingRouteFromWaypointsAsync(points);
  77. MapRoute b = routeResult.Route;
  78. return b;
  79. }
  80. public static async Task<MapRoute> FindWalkingRoute(string from, string to, Geopoint reference)
  81. {
  82. Geopoint f = await FindLocation(from, reference);
  83. Geopoint t = await FindLocation(to, reference);
  84. MapRoute m = await FindWalkingRoute(f, t);
  85. return m;
  86. }
  87. public static async Task<String> FindAddress(Geopoint p)
  88. {
  89. // Reverse geocode the specified geographic location.
  90. MapLocationFinderResult result =
  91. await MapLocationFinder.FindLocationsAtAsync(p);
  92. string returnstring = "";
  93. // If the query returns results, display the name of the town
  94. // contained in the address of the first result.
  95. if (result.Status == MapLocationFinderStatus.Success)
  96. {
  97. MapAddress address = result.Locations[0].Address;
  98. //returnstring = address.Street + " " + address.StreetNumber + ", " + address.Town;
  99. returnstring += (address.BuildingName == "" ? "" : address.BuildingName + ", ");
  100. returnstring += (address.Street == "" ? "" : address.Street + (address.StreetNumber == "" ? ", " : " " + address.StreetNumber + ", "));
  101. returnstring += address.Town;
  102. }
  103. return returnstring;
  104. }
  105. public static async Task<String> FindAddress(double latitude, double longitude)
  106. {
  107. Geopoint p = new Geopoint(new BasicGeoposition() { Latitude = latitude, Longitude = longitude });
  108. string address = await FindAddress(p);
  109. return address;
  110. }
  111. public static void ShowToastNotification(string title, string text)
  112. {
  113. ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
  114. XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
  115. XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
  116. toastTextElements[0].AppendChild(toastXml.CreateTextNode(title));
  117. toastTextElements[1].AppendChild(toastXml.CreateTextNode(text));
  118. IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
  119. XmlElement audio = toastXml.CreateElement("audio");
  120. audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
  121. toastNode.AppendChild(audio);
  122. ToastNotification toast = new ToastNotification(toastXml);
  123. ToastNotificationManager.CreateToastNotifier().Show(toast);
  124. }
  125. public static async Task<bool> ShowConfirmDialog(string title, string content, DialogType type)
  126. {
  127. MessageDialog dlg = new MessageDialog(content, title);
  128. if (type == DialogType.YESNO)
  129. {
  130. dlg.Commands.Add(new UICommand("Yes") { Id = 0 });
  131. dlg.Commands.Add(new UICommand("No") { Id = 1 });
  132. }
  133. else if (type == DialogType.OKCANCEL)
  134. {
  135. dlg.Commands.Add(new UICommand("Ok") { Id = 0 });
  136. dlg.Commands.Add(new UICommand("Cancel") { Id = 1 });
  137. }
  138. dlg.DefaultCommandIndex = 0;
  139. dlg.CancelCommandIndex = 1;
  140. var result = await dlg.ShowAsync();
  141. if ((int)result.Id == 0)
  142. return true;
  143. else
  144. return false;
  145. }
  146. }
  147. }