AddressEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using InABox.Core;
  3. using Syncfusion.Windows.Shared;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using Geocoding;
  11. using Geocoding.Google;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. using InABox.Wpf.DynamicGrid;
  15. using Address = InABox.Core.Address;
  16. using Location = InABox.Core.Location;
  17. namespace InABox.DynamicGrid;
  18. public class AddressEditorControl : DynamicEnclosedEditorControl<Address, AddressEditor>
  19. {
  20. private Grid Grid = null!; // Late-initialized in CreateEditor
  21. private TextBox StreetBox = null!; // Late-initialized in CreateEditor
  22. private TextBox CityBox = null!; // Late-initialized in CreateEditor
  23. private TextBox StateBox = null!; // Late-initialized in CreateEditor
  24. private TextBox PostcodeBox = null!; // Late-initialized in CreateEditor
  25. private Button MapButton = null!; // Late-initialized in CreateEditor
  26. public override int DesiredHeight()
  27. {
  28. return int.MaxValue;
  29. }
  30. public override int DesiredWidth()
  31. {
  32. return int.MaxValue;
  33. }
  34. public override void SetColor(Color color)
  35. {
  36. StreetBox.Background = new SolidColorBrush(color);
  37. CityBox.Background = new SolidColorBrush(color);
  38. StateBox.Background = new SolidColorBrush(color);
  39. PostcodeBox.Background = new SolidColorBrush(color);
  40. }
  41. public override void SetFocus()
  42. {
  43. StreetBox.Focus();
  44. }
  45. public override void Configure()
  46. {
  47. }
  48. protected override FrameworkElement CreateEditor()
  49. {
  50. Grid = new Grid
  51. {
  52. VerticalAlignment = VerticalAlignment.Stretch,
  53. HorizontalAlignment = HorizontalAlignment.Stretch
  54. };
  55. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  56. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  57. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  58. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  59. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  60. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
  61. StreetBox = CreateBox(0, 0, 4, 0, 0.0, 0.0, nameof(Address.Street), "Street");
  62. CityBox = CreateBox(1, 0, 1, 0, 0.0, 5.0, nameof(Address.City),"City");
  63. StateBox = CreateBox(1, 1, 1, 150, 5.0, 5.0, nameof(Address.State),"State");
  64. PostcodeBox = CreateBox(1,2,1,80, 5.0, 5.0, nameof(Address.PostCode),"Postcode");
  65. MapButton = new Button();
  66. MapButton.SetValue(Grid.RowProperty,1);
  67. MapButton.SetValue(Grid.ColumnProperty,3);
  68. MapButton.Width = 25.0;
  69. MapButton.Margin = new Thickness(5.0, 5.0, 0.0, 0.0);
  70. MapButton.BorderThickness = new Thickness(0.75);
  71. MapButton.Background = Brushes.Transparent;
  72. MapButton.Content = new Image() { Source = InABox.Wpf.Resources.map.AsBitmapImage() };
  73. MapButton.Click += (sender, args) => ShowMap();
  74. MapButton.Visibility = !string.IsNullOrWhiteSpace(CoreUtils.GoogleAPIKey) && EditorDefinition.ShowMapButton
  75. ? Visibility.Visible
  76. : Visibility.Collapsed;
  77. Grid.Children.Add(MapButton);
  78. return Grid;
  79. }
  80. private void ShowMap()
  81. {
  82. var values = new Dictionary<string,object?>(GetChildValues());
  83. var address = new Address();
  84. if (values.TryGetValue(nameof(Address.Street), out var street))
  85. address.Street = street?.ToString() ?? "";
  86. if (values.TryGetValue(nameof(Address.City), out var city))
  87. address.City = city?.ToString() ?? "";
  88. if (values.TryGetValue(nameof(Address.State), out var state))
  89. address.State = state?.ToString() ?? "";
  90. if (values.TryGetValue(nameof(Address.PostCode), out var postcode))
  91. address.PostCode = postcode?.ToString() ?? "";
  92. if (values.TryGetValue(nameof(Address.Geofence), out var geofence))
  93. address.Geofence = geofence?.ToString() ?? "";
  94. if (values.TryGetValue($"{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}", out var latitude))
  95. address.Location.Latitude = latitude != null ? (double)latitude : 0.0;
  96. if (values.TryGetValue($"{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}", out var longitude))
  97. address.Location.Longitude = longitude != null ? (double)longitude : 0.0;
  98. var map = new GeofenceEditor(address, true);
  99. map.ShowDialog();
  100. _latitude = address.Location.Latitude;
  101. _longitude = address.Location.Longitude;
  102. _geofence = address.Geofence;
  103. CheckChanged($"{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}");
  104. CheckChanged($"{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}");
  105. CheckChanged(nameof(Address.Geofence));
  106. }
  107. private TextBox CreateBox(int row, int col, int colspan, double minWidth, double leftpad, double toppad, string property, string watermark)
  108. {
  109. var box = new TextBox();
  110. box.SetValue(Grid.RowProperty,row);
  111. box.SetValue(Grid.ColumnProperty,col);
  112. box.SetValue(Grid.ColumnSpanProperty,Math.Max(1,colspan));
  113. box.MinWidth = minWidth;
  114. box.Margin = new Thickness(leftpad, toppad, 0, 0);
  115. box.TextChanged += AddressChanged;
  116. box.LostFocus += CheckChanged;
  117. box.Tag = property;
  118. TextBoxUtils.SetPlaceholder(box, watermark);
  119. Grid.Children.Add(box);
  120. return box;
  121. }
  122. private bool _dirty = false;
  123. private void CheckChanged(object sender, RoutedEventArgs e)
  124. {
  125. if (sender is not TextBox box)
  126. return;
  127. CheckChanged((box.Tag as string)!);
  128. if (_dirty)
  129. {
  130. _latitude = 0.0;
  131. _longitude = 0.0;
  132. _geofence = "";
  133. CheckChanged($"{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}");
  134. CheckChanged($"{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}");
  135. CheckChanged($"{nameof(Address.Geofence)}");
  136. _dirty = false;
  137. }
  138. MapButton.Background = string.IsNullOrWhiteSpace(_geofence) ? Brushes.Red : Brushes.LightGreen;
  139. // CoreUtils.GoogleAPIKey = "AIzaSyAVReknl_sP2VLz5SUD8R-sZh1KDVCcWis";
  140. // IGeocoder geocoder = new GoogleGeocoder(CoreUtils.GoogleAPIKey);
  141. // var address = geocoder.GeocodeAsync($"{StreetBox.Text}, {CityBox.Text} {StateBox.Text} {PostcodeBox.Text} Australia").Result.FirstOrDefault();
  142. // if (address != null)
  143. // {
  144. // var latOffset = new GeoPoint(address.Coordinates.Latitude, address.Coordinates.Longitude).DistanceTo(new GeoPoint(_latitude, address.Coordinates.Longitude));
  145. // if (address.Coordinates.Latitude < _latitude)
  146. // latOffset *= -1.0;
  147. //
  148. // var longOffset = new GeoPoint(address.Coordinates.Latitude, address.Coordinates.Longitude).DistanceTo(new GeoPoint(address.Coordinates.Latitude, _longitude));
  149. // if (address.Coordinates.Longitude < _longitude)
  150. // longOffset *= -1.0;
  151. //
  152. // var fence = Serialization.Deserialize<GeoFence>(_geofence) ?? new GeoFence();
  153. // List<GeoPoint> newPoints = new List<GeoPoint>();
  154. // foreach (var coord in fence.Coordinates)
  155. // newPoints.Add(coord.Move(longOffset * 1000.0, latOffset * 1000.0));
  156. // fence.Coordinates.Clear();
  157. // fence.Coordinates.AddRange(newPoints);
  158. // _latitude = address.Coordinates.Latitude;
  159. // _longitude = address.Coordinates.Longitude;
  160. // _geofence = Serialization.Serialize(fence);
  161. // }
  162. // else
  163. // {
  164. // _latitude = 0.0;
  165. // _longitude = 0.0;
  166. // _geofence = "";
  167. // }
  168. //
  169. // }
  170. }
  171. private void AddressChanged(object sender, TextChangedEventArgs e)
  172. {
  173. _dirty = true;
  174. // Set Latitude and Longitude to Zero
  175. }
  176. public override void SetEnabled(bool enabled)
  177. {
  178. StreetBox.IsEnabled = enabled;
  179. CityBox.IsEnabled = enabled;
  180. StateBox.IsEnabled = enabled;
  181. PostcodeBox.IsEnabled = enabled;
  182. base.SetEnabled(enabled);
  183. }
  184. protected override object? GetChildValue(string property)
  185. {
  186. if (string.Equals(property,nameof(Address.Street)))
  187. return StreetBox.Text;
  188. if (string.Equals(property,nameof(Address.City)))
  189. return CityBox.Text;
  190. if (string.Equals(property,nameof(Address.State)))
  191. return StateBox.Text;
  192. if (string.Equals(property,nameof(Address.PostCode)))
  193. return PostcodeBox.Text;
  194. if (string.Equals(property, $"{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}"))
  195. return _latitude;
  196. if (string.Equals(property, $"{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}"))
  197. return _longitude;
  198. if (string.Equals(property,nameof(Address.Geofence)))
  199. return _geofence;
  200. return null;
  201. }
  202. protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
  203. {
  204. yield return new(nameof(Address.Street), StreetBox.Text);
  205. yield return new(nameof(Address.City), CityBox.Text);
  206. yield return new(nameof(Address.State), StateBox.Text);
  207. yield return new(nameof(Address.PostCode), PostcodeBox.Text);
  208. yield return new($"{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}", _latitude);
  209. yield return new($"{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}", _longitude);
  210. yield return new(nameof(Address.Geofence), _geofence);
  211. }
  212. private double _latitude = 0.0;
  213. private double _longitude = 0.0;
  214. private String _geofence = "";
  215. protected override void SetChildValue(string property, object? value)
  216. {
  217. if (string.Equals(property,nameof(Address.Street)))
  218. StreetBox.Text = value?.ToString() ?? "";
  219. if (string.Equals(property,nameof(Address.City)))
  220. CityBox.Text = value?.ToString() ?? "";
  221. if (string.Equals(property,nameof(Address.State)))
  222. StateBox.Text = value?.ToString() ?? "";
  223. if (string.Equals(property,nameof(Address.PostCode)))
  224. PostcodeBox.Text = value?.ToString() ?? "";
  225. if (string.Equals(property, $"{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}"))
  226. _latitude = (double?)value ?? 0.0;
  227. if (string.Equals(property, $"{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}"))
  228. _longitude = (double?)value ?? 0.0;
  229. if (string.Equals(property, nameof(Address.Geofence)))
  230. {
  231. _geofence = value?.ToString() ?? "";
  232. MapButton.Background = string.IsNullOrWhiteSpace(_geofence) ? Brushes.Red : Brushes.LightGreen;
  233. }
  234. }
  235. }