EventGrid.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using PRS.Shared.Events;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. namespace PRS.Shared;
  18. public class EventGrid : DynamicDataGrid<Event>
  19. {
  20. private readonly BitmapImage _tick = InABox.Wpf.Resources.tick.AsBitmapImage();
  21. private readonly BitmapImage _disabled = InABox.Wpf.Resources.disabled.AsBitmapImage();
  22. private Button? EnableButton = null;
  23. private HashSet<Guid> _subscribedSet = new();
  24. public Guid EmployeeID { get; set; }
  25. protected override void Init()
  26. {
  27. base.Init();
  28. HiddenColumns.Add(x => x.Data);
  29. HiddenColumns.Add(x => x.Enabled);
  30. HiddenColumns.Add(x => x.Visible);
  31. ActionColumns.Add(new DynamicImageColumn(Subscribed_Image, Subscribed_Click) { ToolTip = Subscribed_ToolTip });
  32. if (Security.IsAllowed<CanManageEvents>())
  33. {
  34. EnableButton = AddButton("Disable", null, Enable_Click);
  35. EnableButton.Visibility = Visibility.Collapsed;
  36. }
  37. }
  38. protected override void SelectItems(CoreRow[]? rows)
  39. {
  40. base.SelectItems(rows);
  41. if(EnableButton is not null)
  42. {
  43. if(rows is not null && rows.Length > 0)
  44. {
  45. EnableButton.Visibility = Visibility.Visible;
  46. EnableButton.Content = rows.Any(x => !x.Get<Event, bool>(x => x.Enabled))
  47. ? "Enable" : "Disable";
  48. }
  49. else
  50. {
  51. EnableButton.Visibility = Visibility.Collapsed;
  52. }
  53. }
  54. }
  55. private bool Enable_Click(Button button, CoreRow[] rows)
  56. {
  57. var items = LoadItems(rows);
  58. if(items.Any(x => !x.Enabled))
  59. {
  60. foreach(var item in items)
  61. {
  62. item.Enabled = true;
  63. }
  64. Client.Save(items, "Event enabled.");
  65. return true;
  66. }
  67. else
  68. {
  69. foreach(var item in items)
  70. {
  71. item.Enabled = false;
  72. }
  73. Client.Save(items, "Event disabled.");
  74. return true;
  75. }
  76. }
  77. #region UIComponent
  78. private class UIComponent : DynamicGridGridUIComponent<Event>
  79. {
  80. protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column)
  81. {
  82. if(row.Get<Event, bool>(x => x.Enabled))
  83. {
  84. return null;
  85. }
  86. else
  87. {
  88. return Colors.Gainsboro.ToBrush(0.5);
  89. }
  90. }
  91. }
  92. protected override IDynamicGridUIComponent CreateUIComponent()
  93. {
  94. return new UIComponent { Parent = this };
  95. }
  96. #endregion
  97. protected override void DoReconfigure(DynamicGridOptions options)
  98. {
  99. base.DoReconfigure(options);
  100. if (Security.IsAllowed<CanManageEvents>())
  101. {
  102. options.AddRows = true;
  103. options.EditRows = true;
  104. options.DeleteRows = true;
  105. }
  106. else
  107. {
  108. options.AddRows = false;
  109. options.EditRows = false;
  110. options.DeleteRows = false;
  111. }
  112. }
  113. #region Action Columns
  114. private FrameworkElement? Subscribed_ToolTip(DynamicActionColumn column, CoreRow? row)
  115. {
  116. if(row is null)
  117. {
  118. return column.TextToolTip("Are you subscribed to this event?");
  119. }
  120. else
  121. {
  122. return _subscribedSet.Contains(row.Get<Event, Guid>(x => x.ID))
  123. ? column.TextToolTip("You are subscribed to this event.")
  124. : null;
  125. }
  126. }
  127. private BitmapImage? Subscribed_Image(CoreRow? row)
  128. {
  129. if(row is null)
  130. {
  131. return _tick;
  132. }
  133. else
  134. {
  135. return _subscribedSet.Contains(row.Get<Event, Guid>(x => x.ID)) ? _tick : _disabled;
  136. }
  137. }
  138. private bool Subscribed_Click(CoreRow? row)
  139. {
  140. if (row is null) return false;
  141. var eventID = row.Get<Event, Guid>(x => x.ID);
  142. if(_subscribedSet.Contains(eventID))
  143. {
  144. Unsubscribe(row);
  145. }
  146. else
  147. {
  148. Subscribe(row);
  149. }
  150. return true;
  151. }
  152. private void Unsubscribe(CoreRow row)
  153. {
  154. var eventID = row.Get<Event, Guid>(x => x.ID);
  155. var subscriber = Client.Query(
  156. new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
  157. .And(x => x.Event.ID).IsEqualTo(eventID),
  158. Columns.None<EventSubscriber>().Add(x => x.ID))
  159. .ToObjects<EventSubscriber>().FirstOrDefault();
  160. if(subscriber is not null)
  161. {
  162. Client.Delete(subscriber, "");
  163. }
  164. _subscribedSet.Remove(eventID);
  165. }
  166. private void Subscribe(CoreRow row)
  167. {
  168. var ev = row.ToObject<Event>();
  169. var subscriber = new EventSubscriber();
  170. subscriber.Event.CopyFrom(ev);
  171. subscriber.Employee.ID = EmployeeID;
  172. Client.Save(subscriber, "");
  173. _subscribedSet.Add(ev.ID);
  174. }
  175. #endregion
  176. private readonly Column<Event> EventTypeColumn = new(x => x.EventType);
  177. private readonly Column<Event> NotificationExpressionColumn = new(x => x.NotificationExpression);
  178. private IEventData? EventData;
  179. protected override void BeforeLoad(IDynamicEditorForm form, Event[] items)
  180. {
  181. base.BeforeLoad(form, items);
  182. form.ReadOnly = !Security.IsAllowed<CanManageEvents>();
  183. var ev = items.First();
  184. IEventData? data = null;
  185. if(ev.Data is not null && ev.Data.Length != 0)
  186. {
  187. data = EventUtils.Deserialize(ev.Data);
  188. }
  189. EventData = data;
  190. }
  191. protected override void CustomiseEditor(IDynamicEditorForm form, Event[] items, DynamicGridColumn column, BaseEditor editor)
  192. {
  193. base.CustomiseEditor(form, items, column, editor);
  194. if(EventTypeColumn.IsEqualTo(column.ColumnName) && editor is EnumLookupEditor enumEditor)
  195. {
  196. var ev = items.First();
  197. var editButton = new EditorButton(ev, "Edit Event", 100, (e, i) => EditEvent_Click(form, e, i), false);
  198. enumEditor.Buttons = [editButton];
  199. }
  200. else if(NotificationExpressionColumn.IsEqualTo(column.ColumnName) && editor is ExpressionEditor exprEditor)
  201. {
  202. exprEditor.OnGetVariables += ExprEditor_OnGetVariables;
  203. }
  204. }
  205. private IEnumerable<string> ExprEditor_OnGetVariables()
  206. {
  207. return EventData?.Event.DataModelDefinition().GetVariables().Select(x => x.Name) ?? [];
  208. }
  209. private void EditEvent_Click(IDynamicEditorForm form, object editor, object? item)
  210. {
  211. if (item is not Event ev) return;
  212. var type = ev.EventType;
  213. var data = EventData;
  214. if(EventEditor.Run(ref type, ref data))
  215. {
  216. EventData = data;
  217. ev.EventType = type;
  218. form.SetEditorValue(nameof(Event.EventType), type);
  219. if(data is not null)
  220. {
  221. ev.Data = EventUtils.Serialize(data);
  222. }
  223. else
  224. {
  225. ev.Data = "";
  226. }
  227. }
  228. else
  229. {
  230. // We have to 'cancel' by just re-deserialising.
  231. if(ev.Data is not null && ev.Data.Length != 0)
  232. {
  233. EventData = EventUtils.Deserialize(ev.Data);
  234. }
  235. }
  236. }
  237. protected override void Reload(Filters<Event> criteria, Columns<Event> columns, ref SortOrder<Event>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
  238. {
  239. if (!Security.IsAllowed<CanManageEvents>())
  240. {
  241. criteria.Add(new Filter<Event>(x => x.Visible).IsEqualTo(true));
  242. }
  243. base.Reload(criteria, columns, ref sort, token, (data, error) =>
  244. {
  245. if(data is not null)
  246. {
  247. var ids = data.ExtractValues<Event, Guid>(x => x.ID);
  248. _subscribedSet = Client.Query(
  249. new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
  250. .And(x => x.Event.ID).InList(ids),
  251. Columns.None<EventSubscriber>().Add(x => x.Event.ID))
  252. .ToObjects<EventSubscriber>().Select(x => x.Event.ID).ToHashSet();
  253. }
  254. action(data, error);
  255. });
  256. }
  257. }