|
@@ -0,0 +1,122 @@
|
|
|
+using Comal.Classes;
|
|
|
+using InABox.Clients;
|
|
|
+using InABox.Core;
|
|
|
+using InABox.DynamicGrid;
|
|
|
+using InABox.WPF;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+
|
|
|
+namespace PRS.Shared;
|
|
|
+
|
|
|
+public class EventGrid : DynamicDataGrid<Event>
|
|
|
+{
|
|
|
+ private readonly BitmapImage _tick = InABox.Wpf.Resources.Bullet_Tick.AsBitmapImage();
|
|
|
+
|
|
|
+ private HashSet<Guid> _subscribedSet = new();
|
|
|
+
|
|
|
+ public Guid EmployeeID { get; set; }
|
|
|
+
|
|
|
+ protected override void Init()
|
|
|
+ {
|
|
|
+ base.Init();
|
|
|
+
|
|
|
+ ActionColumns.Add(new DynamicImageColumn(Subscribed_Image) { ToolTip = Subscribed_ToolTip });
|
|
|
+ ActionColumns.Add(new DynamicMenuColumn(BuildMenu));
|
|
|
+ }
|
|
|
+
|
|
|
+ private FrameworkElement? Subscribed_ToolTip(DynamicActionColumn column, CoreRow? row)
|
|
|
+ {
|
|
|
+ if(row is null)
|
|
|
+ {
|
|
|
+ return column.TextToolTip("Are you subscribed to this event?");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return _subscribedSet.Contains(row.Get<Event, Guid>(x => x.ID))
|
|
|
+ ? column.TextToolTip("You are subscribed to this event.")
|
|
|
+ : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private BitmapImage? Subscribed_Image(CoreRow? row)
|
|
|
+ {
|
|
|
+ if(row is null)
|
|
|
+ {
|
|
|
+ return _tick;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return _subscribedSet.Contains(row.Get<Event, Guid>(x => x.ID)) ? _tick : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
|
|
|
+ {
|
|
|
+ if (row is null) return;
|
|
|
+
|
|
|
+ var eventID = row.Get<Event, Guid>(x => x.ID);
|
|
|
+
|
|
|
+ var menu = column.GetMenu();
|
|
|
+ if(_subscribedSet.Contains(eventID))
|
|
|
+ {
|
|
|
+ menu.AddItem("Unsubscribe from event", null, row, Unsubscribe_Click);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ menu.AddItem("Subscribe to event", null, row, Subscribe_Click);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Unsubscribe_Click(CoreRow row)
|
|
|
+ {
|
|
|
+ var eventID = row.Get<Event, Guid>(x => x.ID);
|
|
|
+
|
|
|
+ var subscriber = Client.Query(
|
|
|
+ new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
|
|
|
+ .And(x => x.Event.ID).IsEqualTo(eventID),
|
|
|
+ Columns.None<EventSubscriber>().Add(x => x.ID))
|
|
|
+ .ToObjects<EventSubscriber>().FirstOrDefault();
|
|
|
+ if(subscriber is not null)
|
|
|
+ {
|
|
|
+ Client.Delete(subscriber, "");
|
|
|
+ }
|
|
|
+
|
|
|
+ _subscribedSet.Remove(eventID);
|
|
|
+
|
|
|
+ InvalidateRow(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Subscribe_Click(CoreRow row)
|
|
|
+ {
|
|
|
+ var ev = row.ToObject<Event>();
|
|
|
+ var subscriber = new EventSubscriber();
|
|
|
+ subscriber.Event.CopyFrom(ev);
|
|
|
+ subscriber.Employee.ID = EmployeeID;
|
|
|
+ Client.Save(subscriber, "");
|
|
|
+ _subscribedSet.Add(ev.ID);
|
|
|
+
|
|
|
+ InvalidateRow(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void Reload(Filters<Event> criteria, Columns<Event> columns, ref SortOrder<Event>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
|
|
|
+ {
|
|
|
+ base.Reload(criteria, columns, ref sort, token, (data, error) =>
|
|
|
+ {
|
|
|
+ if(data is not null)
|
|
|
+ {
|
|
|
+ var ids = data.ExtractValues<Event, Guid>(x => x.ID);
|
|
|
+ _subscribedSet = Client.Query(
|
|
|
+ new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
|
|
|
+ .And(x => x.Event.ID).InList(ids),
|
|
|
+ Columns.None<EventSubscriber>().Add(x => x.Event.ID))
|
|
|
+ .ToObjects<EventSubscriber>().Select(x => x.Event.ID).ToHashSet();
|
|
|
+ }
|
|
|
+ action(data, error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|