Util.cs 6.2 KB

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