123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- using System;
- using System.Collections.ObjectModel;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Image = System.Windows.Controls.Image;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- namespace PRSDesktop
- {
- public delegate void NotificationsChangedEvent(object sender);
- /// <summary>
- /// Interaction logic for NotificationsDock.xaml
- /// </summary>
- public partial class NotificationsDock : UserControl
- {
- public NotificationsDock()
- {
- InitializeComponent();
- NotificationsList.ItemsSource = Notifications;
- }
- private ObservableCollection<Notification> Notifications = new();
- public bool IsActive => Notifications.Any();
- public event NotificationsChangedEvent Changed;
- public void AddNotification(Notification notification)
- {
- Dispatcher.Invoke(() =>
- {
- if (!Notifications.Any(x => x.ID == notification.ID))
- {
- Notifications.Add(notification);
- }
- MessageHeader.Content = string.Format("{0} Notification{1}", Notifications.Count, Notifications.Count == 1 ? "" : "s");
- Changed?.Invoke(this);
- });
- }
- public void Refresh()
- {
- new Client<Notification>().Query(
- new Filter<Notification>(x => x.Employee.ID).IsEqualTo(App.EmployeeID).And(x => x.Closed)
- .IsEqualTo(DateTime.MinValue),
- new Columns<Notification>(
- x => x.ID,
- x => x.Title,
- //x => x.Description,
- x => x.Created,
- x => x.Sender.ID,
- x => x.Sender.Name,
- x => x.Job.ID,
- x => x.Job.Deleted,
- x => x.Job.JobNumber,
- //x => x.Kanban.ID,
- //x => x.Setout.ID,
- //x => x.Requisition.ID,
- //x => x.Delivery.ID,
- x => x.Employee.ID,
- x => x.EntityType,
- x => x.EntityID,
- x => x.Closed
- ),
- null, //new SortOrder<Notification>(x => x.Created, InABox.Core.SortDirection.Descending),
- (n, ex) =>
- {
- Dispatcher.Invoke(() =>
- {
- if (n != null)
- {
- MessageHeader.Content = string.Format("{0} Notification{1}", n.Rows.Count, n.Rows.Count == 1 ? "" : "s");
- Notifications.Clear();
- foreach(var notification in n.Rows.Select(r => r.ToObject<Notification>()))
- {
- Notifications.Add(notification);
- }
- Changed?.Invoke(this);
- }
- else
- {
- Changed?.Invoke(this);
- }
- });
- }
- );
- }
- private void Notification_Toolbar_Loaded(object sender, RoutedEventArgs e)
- {
- var toolBar = sender as ToolBar;
- var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
- if (overflowGrid != null)
- overflowGrid.Visibility = Visibility.Collapsed;
- var mainPanelBorder = toolBar.Template.FindName("MainPanelBorder", toolBar) as FrameworkElement;
- if (mainPanelBorder != null)
- mainPanelBorder.Margin = new Thickness();
- }
- private void LoadButton(ToolBar toolbar, Notification notification, int index, bool visible, Bitmap image, string tooltip)
- {
- var btn = toolbar.Items[index] as Button;
- btn.Tag = notification;
- btn.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
- var img = btn.Content as Image;
- img.Source = image.AsBitmapImage();
- btn.ToolTip = tooltip;
- }
- private void Expander_Expanded(object sender, RoutedEventArgs e)
- {
- var expander = sender as Expander;
- var notification = expander.Tag as Notification;
- var stack = (expander.Content as Border).Child as StackPanel;
- var isJob = notification.Job.IsValid();
- var isForm = NotificationUtils.IsDigitalForm(notification.EntityType);
- var entitytype = NotificationUtils.GetEntityType(notification.EntityType);
- var toolbar = stack.Children[0] as ToolBar;
- LoadButton(toolbar, notification, 0, true, PRSDesktop.Resources.delete, "Dismiss");
- LoadButton(toolbar, notification, 1, true, PRSDesktop.Resources.reply, "Reply");
- LoadButton(toolbar, notification, 2, true, PRSDesktop.Resources.forward, "Forward");
- LoadButton(toolbar, notification, 3, isForm, PRSDesktop.Resources.checklist, "View Form");
- LoadButton(toolbar, notification, 4, entitytype != null, PRSDesktop.Resources.pencil,
- string.Format("View {0}", entitytype?.GetCaption()));
- LoadButton(toolbar, notification, 5, true, PRSDesktop.Resources.project, string.Format("{0} {1}", isJob ? "View" : "Attach To", "Job"));
- LoadButton(toolbar, notification, 6, entitytype != typeof(Kanban), PRSDesktop.Resources.kanban, "Create Task");
- LoadButton(toolbar, notification, 7, entitytype != typeof(Delivery), PRSDesktop.Resources.truck, "Create Delivery");
- LoadButton(toolbar, notification, 8, entitytype != typeof(Requisition), PRSDesktop.Resources.requisition, "Create Requisition");
- LoadButton(toolbar, notification, 9, entitytype != typeof(Setout), PRSDesktop.Resources.factory, "Create Setout");
- var editor = stack.Children[1] as ExtendedRichTextEditor;
- new Client<Notification>().Query(
- new Filter<Notification>(x => x.ID).IsEqualTo(notification.ID),
- new Columns<Notification>(x => x.Description),
- null,
- (table, error) =>
- {
- var drow = table != null ? table.Rows.FirstOrDefault() : null;
- var desc = drow?.Get<Notification, string>(x => x.Description)?.Replace("background:NoColor;", "") ?? "";
- var ms = new MemoryStream(Encoding.ASCII.GetBytes(desc));
- Dispatcher.Invoke(() =>
- {
- notification.Description = desc;
- editor.Text = desc;
- });
- }
- );
- editor.Height = 200;
- ScrollViewer scroller = stack.Children.OfType<ScrollViewer>().FirstOrDefault();
- if (scroller == null)
- PreviewPhotos(notification, stack);
- }
- private void PreviewPhotos(Notification notification, StackPanel stack)
- {
- Task.Run(() =>
- {
- CoreTable table = new Client<NotificationDocument>().Query
- (
- new Filter<NotificationDocument>(x => x.EntityLink.ID).IsEqualTo(notification.ID),
- new Columns<NotificationDocument>(x => x.DocumentLink.ID, x => x.ID)
- );
- if (table.Rows.Any())
- {
- Dispatcher.Invoke(() =>
- {
- List<Image> images = new List<Image>();
- foreach (CoreRow row in table.Rows)
- {
- CoreTable table1 = new Client<Document>().Query
- (
- new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(row.Values[0].ToString())),
- new Columns<Document>(x => x.FileName, x => x.Data, x => x.ID)
- );
- Image img = DataToImage((table1.Rows.FirstOrDefault().Values[1]) as byte[], table1.Rows.FirstOrDefault().Values[0].ToString());
- img.Tag = row.Values[1].ToString();
- images.Add(img);
- }
- ScrollViewer scroller = new ScrollViewer() { VerticalScrollBarVisibility = ScrollBarVisibility.Disabled, HorizontalScrollBarVisibility = ScrollBarVisibility.Visible };
- StackPanel photoPanel = new StackPanel();
- photoPanel.Height = 50;
- photoPanel.Orientation = Orientation.Horizontal;
- foreach (Image img in images)
- {
- photoPanel.Children.Add(img);
- }
- scroller.Content = photoPanel;
- stack.Children.Add(scroller);
- });
- }
- });
- }
- private void Notification_ReplyButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.ReplyOrForward(new[] { notification.ID }, "RE: ", true, false))
- Refresh();
- }
- private void Notification_ForwardButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.ReplyOrForward(new[] { notification.ID }, "FW: ", false, false))
- Refresh();
- }
- private void Notification_FormButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- NotificationUtils.ViewForm(new[] { notification.ID });
- }
- private void Notification_EntityButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.ViewEntity(new[] { notification.ID }))
- Refresh();
- }
- private void Notification_JobButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (notification.Job.IsValid())
- {
- NotificationUtils.ViewJob(new[] { notification.Job.ID });
- }
- else
- {
- if (NotificationUtils.AttachToJob(new[] { notification.ID }))
- Refresh();
- }
- }
- private void Notification_TaskButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.CreateTask(new[] { notification.ID }, App.EmployeeID))
- Refresh();
- }
- private void Notification_DeliveryButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.CreateDelivery(new[] { notification.ID }))
- Refresh();
- }
- private void Notification_RequisitionButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.CreateRequi(new[] { notification.ID }, App.EmployeeID))
- Refresh();
- }
- private void Notification_SetoutButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.CreateSetout(new[] { notification.ID }))
- Refresh();
- }
- private void Notification_DismissButton_Click(object sender, RoutedEventArgs e)
- {
- var notification = (sender as Button).Tag as Notification;
- if (NotificationUtils.Archive(new[] { notification.ID }))
- {
- var list = NotificationsList.ItemsSource as ObservableCollection<Notification>;
- if (list.Contains(notification))
- list.Remove(notification);
- MessageHeader.Content = string.Format("{0} Notification{1}", list.Count, list.Count == 1 ? "" : "s");
- Changed?.Invoke(this);
- }
- }
- private Image DataToImage(byte[] data, string filename)
- {
- Image img = new Image();
- img.Height = 50;
- img.Width = 50;
- img.Margin = new Thickness(5);
- img.MouseUp += Img_MouseUp;
- if (filename.EndsWith("png") || filename.EndsWith("jpg") || filename.EndsWith("jpeg") || filename.EndsWith("bmp"))
- {
- img.Source = ImageUtils.LoadImage(data);
- }
- else if (filename.EndsWith("pdf"))
- {
- img.Source = PRSDesktop.Resources.doc_pdf.AsBitmapImage();
- }
- return img;
- }
- private void Img_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
- {
- Image img = sender as Image;
- string str = img.Tag as string;
- Guid id = Guid.Parse(str);
- CoreTable table = new Client<NotificationDocument>().Query(new Filter<NotificationDocument>(x => x.ID).IsEqualTo(id),
- new Columns<NotificationDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName));
- if (table.Rows.Any())
- {
- IEntityDocument doc = table.Rows.First().ToObject<NotificationDocument>();
- IEntityDocument[] docs = new IEntityDocument[] { doc };
- DocumentEditor editor = new DocumentEditor(docs);
- editor.Show();
- }
- }
- }
- }
|