AddressEditor.cs 11 KB

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