EquipmentSelector.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. BitmapImage img = (image != null && image.Data != null && image.Data.Length > 0)
  143. ? ImageUtils.LoadImage(image.Data)
  144. : PRSDesktop.Resources.truck.AsBitmapImage();
  145. var grp = _groups.FirstOrDefault(x => x.Key == row.Get<EquipmentGroup, Guid>(c => c.Thumbnail.ID));
  146. if (grp != null)
  147. grp.Image = img;
  148. }
  149. ObservableCollection<ListEntry> groupdata = new() { new ListEntry(Guid.Empty, "All Equipment", PRSDesktop.Resources.truck.AsBitmapImage()) };
  150. foreach (var group in _groups)
  151. groupdata.Add(group);
  152. groupdata.Add(new ListEntry(CoreUtils.FullGuid, "Multiple Groups", PRSDesktop.Resources.truck.AsBitmapImage()));
  153. Groups.ItemsSource = groupdata;
  154. _equipmentgroups = _equipment.Rows.Select(
  155. row => new Tuple<Guid, Guid, String, BitmapImage?>(
  156. row.Get<Equipment, Guid>(c => c.GroupLink.ID),
  157. row.Get<Equipment, Guid>(c => c.ID),
  158. row.Get<Equipment, String>(c => c.Description),
  159. _groups.FirstOrDefault(x=>x.Key == row.Get<Equipment, Guid>(c => c.GroupLink.ID))?.Image
  160. )
  161. ).Union(
  162. _equipment.Rows.Select(
  163. row => new Tuple<Guid,Guid,String, BitmapImage?>(
  164. Guid.Empty,
  165. row.Get<Equipment, Guid>(c => c.ID),
  166. row.Get<Equipment, String>(c => c.Description),
  167. PRSDesktop.Resources.truck.AsBitmapImage()
  168. )
  169. )
  170. ).ToArray();
  171. Groups.SelectedValue = Guid.Empty;
  172. }
  173. }
  174. private void UpdateSettings()
  175. {
  176. bool changed = false;
  177. var groups = GetSelectedGroups();
  178. if (groups.Contains(CoreUtils.FullGuid) && (Math.Abs(Settings.SplitPosition - GroupListRow.Height.Value) >= 1.0F))
  179. {
  180. changed = true;
  181. Settings.SplitPosition = GroupListRow.Height.Value;
  182. }
  183. if (changed && (!EventSuppressor.IsSet(Suppress.This)))
  184. SettingsChanged?.Invoke(this, new EquipmentSelectorSettingsChangedArgs(Settings));
  185. }
  186. private Guid[] GetSelectedGroups()
  187. {
  188. Guid groupid = Groups.SelectedValue != null ? (Guid)Groups.SelectedValue : Guid.Empty;
  189. var teams = (groupid == CoreUtils.FullGuid)
  190. ? GroupList.SelectedItems
  191. .OfType<ListEntry>()
  192. .Select(x => x.Key)
  193. .Prepend(groupid)
  194. .ToArray()
  195. : new Guid[] { groupid };
  196. return teams;
  197. }
  198. private Guid[] GetSelectedEquipment()
  199. {
  200. var emps = (SelectedEquipment.SelectedItems.Count == SelectedEquipment.Items.Count)
  201. ? new Guid[] { CoreUtils.FullGuid }
  202. : SelectedEquipment.SelectedItems
  203. .OfType<ListEntry>()
  204. .Select(x => x.Key)
  205. .ToArray();
  206. return emps;
  207. }
  208. public EquipmentSelectorData Selection
  209. {
  210. get => GetSelectionData();
  211. set => SelectEquipment(value);
  212. }
  213. private EquipmentSelectorData GetSelectionData()
  214. {
  215. return new EquipmentSelectorData(
  216. GetSelectedGroups(),
  217. GetSelectedEquipment()
  218. );
  219. }
  220. private void SelectEquipment(EquipmentSelectorData selection)
  221. {
  222. using (new EventSuppressor(Suppress.This))
  223. {
  224. Groups.SelectedValue = selection.Groups.Contains(CoreUtils.FullGuid)
  225. ? CoreUtils.FullGuid
  226. : selection.Groups.FirstOrDefault();
  227. LoadGroups(selection.Groups);
  228. var equipment = SelectedEquipment.ItemsSource as ObservableCollection<ListEntry>;
  229. var pairs = equipment.Where(x => selection.Equipment.Contains(CoreUtils.FullGuid) || selection.Equipment.Contains(x.Key));
  230. SelectedEquipment.SelectedItems.Clear();
  231. foreach (var pair in pairs)
  232. SelectedEquipment.SelectedItems.Add(pair);
  233. }
  234. }
  235. private void LoadGroups(Guid[] groupids)
  236. {
  237. using (new EventSuppressor(Suppress.This))
  238. {
  239. Guid groupid = groupids.FirstOrDefault();
  240. if (Guid.Equals(groupid, CoreUtils.FullGuid))
  241. {
  242. GroupListRow.Height = new GridLength(Settings.SplitPosition, GridUnitType.Pixel);
  243. GroupListSplitterRow.Height = new GridLength(4, GridUnitType.Pixel);
  244. var selected = new ObservableCollection<object>(_groups.Where(x => groupids.Contains(x.Key)));
  245. GroupList.SelectedItems = selected;
  246. LoadEquipment(groupids.Skip(1).ToArray());
  247. }
  248. else
  249. {
  250. GroupListRow.Height = new GridLength(0, GridUnitType.Pixel);
  251. GroupListSplitterRow.Height = new GridLength(0, GridUnitType.Pixel);
  252. LoadEquipment(new Guid[] { groupid });
  253. }
  254. }
  255. }
  256. private void LoadEquipment(Guid[] teamids)
  257. {
  258. using (new EventSuppressor(Suppress.This))
  259. {
  260. var eqs =_equipmentgroups
  261. .OrderBy(x => x.Item3)
  262. .Where(x => teamids.Contains(x.Item1))
  263. .Select(x => new ListEntry(x.Item2, x.Item3))
  264. .Distinct()
  265. .ToArray();
  266. SelectedEquipment.ItemsSource = new ObservableCollection<ListEntry>(eqs);
  267. SelectedEquipment.SelectedItems = new ObservableCollection<object>(eqs);
  268. }
  269. }
  270. public void SelectEmployee(Guid employeeid)
  271. {
  272. using (new EventSuppressor(Suppress.This))
  273. {
  274. LoadGroups(new Guid[] { Guid.Empty });
  275. if (SelectedEquipment.ItemsSource is ObservableCollection<ListEntry> employees)
  276. {
  277. var emp = employees.FirstOrDefault(x => x.Key == employeeid);
  278. if (emp != null)
  279. SelectedEquipment.SelectedItems = new ObservableCollection<object>() { emp };
  280. }
  281. }
  282. }
  283. private void TeamsSelectionChanged(object sender, SelectionChangedEventArgs e)
  284. {
  285. if (EventSuppressor.IsSet(Suppress.This))
  286. return;
  287. using (new EventSuppressor(Suppress.This))
  288. LoadGroups(GetSelectedGroups());
  289. UpdateSettings();
  290. SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
  291. }
  292. private void SelectedGroups_ItemChecked(object? sender, ItemCheckedEventArgs e)
  293. {
  294. if (EventSuppressor.IsSet(Suppress.This))
  295. return;
  296. using (new EventSuppressor(Suppress.This))
  297. LoadEquipment(GetSelectedGroups());
  298. UpdateSettings();
  299. SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
  300. }
  301. private void SelectedEquipment_OnItemChecked(object? sender, ItemCheckedEventArgs e)
  302. {
  303. if (EventSuppressor.IsSet(Suppress.This))
  304. return;
  305. UpdateSettings();
  306. SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
  307. }
  308. private void SelectedGroups_SizeChanged(object sender, SizeChangedEventArgs e)
  309. {
  310. if (EventSuppressor.IsSet(Suppress.This))
  311. return;
  312. UpdateSettings();
  313. }
  314. }
  315. }