EquipmentSelector.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.WPF;
  13. using NPOI.HSSF.Util;
  14. using Syncfusion.Windows.Tools.Controls;
  15. namespace PRSDesktop
  16. {
  17. public class EquipmentSelectorData
  18. {
  19. // if first == FullGuid, remainder are selected
  20. // Otherwise, ignore the rest
  21. public Guid[] Groups { get; set; }
  22. // if first == FullGuid, select all
  23. public Guid[] Equipment { get; set; }
  24. public EquipmentSelectorData( Guid[] groups, Guid[] equipment)
  25. {
  26. Groups = groups;
  27. Equipment = equipment;
  28. }
  29. public EquipmentSelectorData() : this(new[] { Guid.Empty }, new[] { CoreUtils.FullGuid })
  30. {
  31. }
  32. }
  33. public class EquipmentSelectorSettings
  34. {
  35. public double SplitPosition { get; set; }
  36. public EquipmentSelectorSettings()
  37. {
  38. SplitPosition = 200.0F;
  39. }
  40. }
  41. public class EquipmentSelectorSettingsChangedArgs : EventArgs
  42. {
  43. public EquipmentSelectorSettings Settings { get; private set; }
  44. public EquipmentSelectorSettingsChangedArgs(EquipmentSelectorSettings settings)
  45. {
  46. Settings = settings;
  47. }
  48. }
  49. public delegate void EquipmentSelectorSettingsChanged(object sender, EquipmentSelectorSettingsChangedArgs args);
  50. public class EquipmentSelectorSelectionChangedArgs : EventArgs
  51. {
  52. public EquipmentSelectorData Selection { get; private set; }
  53. public EquipmentSelectorSelectionChangedArgs(EquipmentSelectorData selection)
  54. {
  55. Selection = selection;
  56. }
  57. }
  58. public delegate void EquipmentSelectorSelectionChanged(object sender, EquipmentSelectorSelectionChangedArgs args);
  59. public partial class EquipmentSelector : UserControl
  60. {
  61. private enum Suppress
  62. {
  63. This
  64. }
  65. private class ListEntry
  66. {
  67. public Guid Key { get; set; }
  68. public String Value { get; set; }
  69. public BitmapImage Image { get; set; }
  70. public ListEntry(Guid key, String value, BitmapImage image = null)
  71. {
  72. Key = key;
  73. Value = value;
  74. Image = image;
  75. }
  76. }
  77. public EquipmentSelectorSettings Settings { get; set; }
  78. public event EquipmentSelectorSettingsChanged SettingsChanged;
  79. public event EquipmentSelectorSelectionChanged SelectionChanged;
  80. private CoreTable _equipment;
  81. private CoreTable _activities;
  82. private ListEntry[] _groups;
  83. private Tuple<Guid,Guid,String, BitmapImage?>[] _equipmentgroups;
  84. public T[] GetEquipmentData<T>(Func<CoreRow, BitmapImage?, EquipmentActivity[], T> transform)
  85. {
  86. List<T> result = new List<T>();
  87. var selgrps = GetSelectedGroups();
  88. var seleq = GetSelectedEquipment();
  89. var eqids = _equipmentgroups
  90. .Where(x => selgrps.Contains(x.Item1) && seleq.Contains(CoreUtils.FullGuid) || seleq.Contains(x.Item2))
  91. .Select(x => x.Item2)
  92. .Distinct()
  93. .ToArray();
  94. var rows = _equipment.Rows.Where(r => eqids.Contains(r.Get<Equipment, Guid>(c => c.ID)));
  95. foreach (var row in rows)
  96. {
  97. var activities = _activities.Rows
  98. .Where(r => r.Get<EquipmentActivity, Guid>(c => c.Equipment.ID) ==
  99. row.Get<Equipment, Guid>(c => c.ID))
  100. .Select(r=>r.ToObject<EquipmentActivity>())
  101. .ToArray();
  102. result.Add(
  103. transform.Invoke(
  104. row,
  105. _groups.FirstOrDefault(x => x.Key == row.Get<Equipment, Guid>(c => c.GroupLink.ID))?.Image,
  106. activities
  107. )
  108. );
  109. }
  110. return result.ToArray();
  111. }
  112. public EquipmentSelector()
  113. {
  114. Settings = new EquipmentSelectorSettings();
  115. using (new EventSuppressor(Suppress.This))
  116. InitializeComponent();
  117. }
  118. public void Setup()
  119. {
  120. using (new EventSuppressor(Suppress.This))
  121. {
  122. var results = Client.QueryMultiple(
  123. new KeyedQueryDef<Equipment>(LookupFactory.DefineFilter<Equipment>()),
  124. new KeyedQueryDef<EquipmentActivity>(),
  125. new KeyedQueryDef<EquipmentGroup>(LookupFactory.DefineFilter<EquipmentGroup>())
  126. );
  127. _equipment = results.Get<Equipment>();
  128. _activities = results.Get<EquipmentActivity>();
  129. var groups = results.Get<EquipmentGroup>();
  130. _groups = groups.Rows.Select(r => new ListEntry(
  131. r.Get<EquipmentGroup,Guid>(c=>c.ID),
  132. r.Get<Team,String>(c=>c.Name)
  133. )).ToArray();
  134. GroupList.ItemsSource = _groups;
  135. var ids = groups.Rows.Select(r => r.Get<EquipmentGroup,Guid>(c=>c.Thumbnail.ID)).Distinct().Where(x => x != Guid.Empty).ToArray();
  136. var Images = ids.Any()
  137. ? new Client<Document>().Load(new Filter<Document>(x=>x.ID).InList(ids))
  138. : new Document[] { };
  139. foreach (var row in groups.Rows)
  140. {
  141. var image = Images.FirstOrDefault(x => x.ID.Equals(row.Get<EquipmentGroup,Guid>(c=>c.Thumbnail.ID)));
  142. var grp = _groups.FirstOrDefault(x => x.Key == row.Get<EquipmentGroup, Guid>(c => c.Thumbnail.ID));
  143. if (grp != null)
  144. grp.Image = ImageUtils.LoadImage(image.Data) ?? PRSDesktop.Resources.truck.AsBitmapImage();
  145. }
  146. ObservableCollection<ListEntry> groupdata = new() { new ListEntry(Guid.Empty, "All Equipment", PRSDesktop.Resources.truck.AsBitmapImage()) };
  147. foreach (var group in _groups)
  148. groupdata.Add(group);
  149. groupdata.Add(new ListEntry(CoreUtils.FullGuid, "Multiple Groups", PRSDesktop.Resources.truck.AsBitmapImage()));
  150. Groups.ItemsSource = groupdata;
  151. _equipmentgroups = _equipment.Rows.Select(
  152. row => new Tuple<Guid, Guid, String, BitmapImage?>(
  153. row.Get<Equipment, Guid>(c => c.GroupLink.ID),
  154. row.Get<Equipment, Guid>(c => c.ID),
  155. row.Get<Equipment, String>(c => c.Description),
  156. _groups.FirstOrDefault(x=>x.Key == row.Get<Equipment, Guid>(c => c.GroupLink.ID))?.Image
  157. )
  158. ).Union(
  159. _equipment.Rows.Select(
  160. row => new Tuple<Guid,Guid,String, BitmapImage?>(
  161. Guid.Empty,
  162. row.Get<Equipment, Guid>(c => c.ID),
  163. row.Get<Equipment, String>(c => c.Description),
  164. PRSDesktop.Resources.truck.AsBitmapImage()
  165. )
  166. )
  167. ).ToArray();
  168. Groups.SelectedValue = Guid.Empty;
  169. }
  170. }
  171. private void UpdateSettings()
  172. {
  173. bool changed = false;
  174. var groups = GetSelectedGroups();
  175. if (groups.Contains(CoreUtils.FullGuid) && (Math.Abs(Settings.SplitPosition - GroupListRow.Height.Value) >= 1.0F))
  176. {
  177. changed = true;
  178. Settings.SplitPosition = GroupListRow.Height.Value;
  179. }
  180. if (changed && (!EventSuppressor.IsSet(Suppress.This)))
  181. SettingsChanged?.Invoke(this, new EquipmentSelectorSettingsChangedArgs(Settings));
  182. }
  183. private Guid[] GetSelectedGroups()
  184. {
  185. Guid groupid = Groups.SelectedValue != null ? (Guid)Groups.SelectedValue : Guid.Empty;
  186. var teams = (groupid == CoreUtils.FullGuid)
  187. ? GroupList.SelectedItems
  188. .OfType<ListEntry>()
  189. .Select(x => x.Key)
  190. .Prepend(groupid)
  191. .ToArray()
  192. : new Guid[] { groupid };
  193. return teams;
  194. }
  195. private Guid[] GetSelectedEquipment()
  196. {
  197. var emps = (SelectedEquipment.SelectedItems.Count == SelectedEquipment.Items.Count)
  198. ? new Guid[] { CoreUtils.FullGuid }
  199. : SelectedEquipment.SelectedItems
  200. .OfType<ListEntry>()
  201. .Select(x => x.Key)
  202. .ToArray();
  203. return emps;
  204. }
  205. public EquipmentSelectorData Selection
  206. {
  207. get => GetSelectionData();
  208. set => SelectEquipment(value);
  209. }
  210. private EquipmentSelectorData GetSelectionData()
  211. {
  212. return new EquipmentSelectorData(
  213. GetSelectedGroups(),
  214. GetSelectedEquipment()
  215. );
  216. }
  217. private void SelectEquipment(EquipmentSelectorData selection)
  218. {
  219. using (new EventSuppressor(Suppress.This))
  220. {
  221. Groups.SelectedValue = selection.Groups.Contains(CoreUtils.FullGuid)
  222. ? CoreUtils.FullGuid
  223. : selection.Groups.FirstOrDefault();
  224. LoadGroups(selection.Groups);
  225. var equipment = SelectedEquipment.ItemsSource as ObservableCollection<ListEntry>;
  226. var pairs = equipment.Where(x => selection.Equipment.Contains(CoreUtils.FullGuid) || selection.Equipment.Contains(x.Key));
  227. SelectedEquipment.SelectedItems.Clear();
  228. foreach (var pair in pairs)
  229. SelectedEquipment.SelectedItems.Add(pair);
  230. }
  231. }
  232. private void LoadGroups(Guid[] groupids)
  233. {
  234. using (new EventSuppressor(Suppress.This))
  235. {
  236. Guid groupid = groupids.FirstOrDefault();
  237. if (Guid.Equals(groupid, CoreUtils.FullGuid))
  238. {
  239. GroupListRow.Height = new GridLength(Settings.SplitPosition, GridUnitType.Pixel);
  240. GroupListSplitterRow.Height = new GridLength(4, GridUnitType.Pixel);
  241. var selected = new ObservableCollection<object>(_groups.Where(x => groupids.Contains(x.Key)));
  242. GroupList.SelectedItems = selected;
  243. LoadEquipment(groupids.Skip(1).ToArray());
  244. }
  245. else
  246. {
  247. GroupListRow.Height = new GridLength(0, GridUnitType.Pixel);
  248. GroupListSplitterRow.Height = new GridLength(0, GridUnitType.Pixel);
  249. LoadEquipment(new Guid[] { groupid });
  250. }
  251. }
  252. }
  253. private void LoadEquipment(Guid[] teamids)
  254. {
  255. using (new EventSuppressor(Suppress.This))
  256. {
  257. var eqs =_equipmentgroups
  258. .OrderBy(x => x.Item3)
  259. .Where(x => teamids.Contains(x.Item1))
  260. .Select(x => new ListEntry(x.Item2, x.Item3))
  261. .Distinct()
  262. .ToArray();
  263. SelectedEquipment.ItemsSource = new ObservableCollection<ListEntry>(eqs);
  264. SelectedEquipment.SelectedItems = new ObservableCollection<object>(eqs);
  265. }
  266. }
  267. public void SelectEmployee(Guid employeeid)
  268. {
  269. using (new EventSuppressor(Suppress.This))
  270. {
  271. LoadGroups(new Guid[] { Guid.Empty });
  272. if (SelectedEquipment.ItemsSource is ObservableCollection<ListEntry> employees)
  273. {
  274. var emp = employees.FirstOrDefault(x => x.Key == employeeid);
  275. if (emp != null)
  276. SelectedEquipment.SelectedItems = new ObservableCollection<object>() { emp };
  277. }
  278. }
  279. }
  280. private void TeamsSelectionChanged(object sender, SelectionChangedEventArgs e)
  281. {
  282. if (EventSuppressor.IsSet(Suppress.This))
  283. return;
  284. using (new EventSuppressor(Suppress.This))
  285. LoadGroups(GetSelectedGroups());
  286. UpdateSettings();
  287. SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
  288. }
  289. private void SelectedGroups_ItemChecked(object? sender, ItemCheckedEventArgs e)
  290. {
  291. if (EventSuppressor.IsSet(Suppress.This))
  292. return;
  293. using (new EventSuppressor(Suppress.This))
  294. LoadEquipment(GetSelectedGroups());
  295. UpdateSettings();
  296. SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
  297. }
  298. private void SelectedEquipment_OnItemChecked(object? sender, ItemCheckedEventArgs e)
  299. {
  300. if (EventSuppressor.IsSet(Suppress.This))
  301. return;
  302. UpdateSettings();
  303. SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
  304. }
  305. private void SelectedGroups_SizeChanged(object sender, SizeChangedEventArgs e)
  306. {
  307. if (EventSuppressor.IsSet(Suppress.This))
  308. return;
  309. UpdateSettings();
  310. }
  311. }
  312. }