| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Linq;
- using Xamarin.Forms;
- using XF.Material.Forms.UI;
- using XF.Material.Forms.UI.Dialogs;
- using InABox.Core;
- using InABox.Clients;
- using Comal.Classes;
- using System.IO;
- using Plugin.Media;
- using InABox.Mobile;
- using System.Linq.Expressions;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using comal.timesheets.CustomControls;
- using comal.timesheets.Tasks;
- namespace comal.timesheets
- {
- public delegate void NotificationsClosedEvent(int numberOfNotifications);
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class NotificationList : ContentPage
- {
- public event NotificationsClosedEvent NotificationsClosed;
- List<NotificationShell> notificationShells = new List<NotificationShell>();
- #region Constructor + Loading
- public NotificationList()
- {
- InitializeComponent();
- }
- protected override void OnAppearing()
- {
- LoadData();
- base.OnAppearing();
- }
- private async void LoadData()
- {
- await Task.Run(() =>
- {
- notificationShells.Clear();
- var table = new Client<Notification>().Query(
- new Filter<Notification>(x => x.Employee.ID).IsEqualTo(GlobalVariables.EmpID).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
- new Columns<Notification>(
- x => x.ID, //0
- x => x.Sender.Name, //1
- x => x.Title, //2
- x => x.Created, //3
- x => x.Description, //4
- x => x.EntityType, //5
- x => x.EntityID //6
- ),
- new SortOrder<Notification>(x => x.Created, SortDirection.Descending)
- );
- string emptyString = "";
- if (table.Rows.Count != 0)
- {
- foreach (var row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) { list[0] = Guid.Empty; }
- if (list[1] == null) { list[1] = emptyString; }
- if (list[2] == null) { list[2] = emptyString; }
- if (list[3] == null) { list[3] = DateTime.MinValue; }
- if (list[4] == null) { list[4] = emptyString; }
- if (list[5] == null) { list[5] = emptyString; }
- if (list[6] == null) { list[6] = Guid.Empty; }
- NotificationShell notificationShell = new NotificationShell();
- notificationShell.ID = Guid.Parse(list[0].ToString());
- notificationShell.Sender = list[1].ToString();
- notificationShell.Title = list[2].ToString();
- notificationShell.Created = DateTime.Parse(list[3].ToString());
- notificationShell.Description = CoreUtils.StripHTML(list[4].ToString());
- notificationShell.EntityType = list[5].ToString();
- notificationShell.EntityID = Guid.Parse(list[6].ToString());
- notificationShells.Add(notificationShell);
- }
- LoadPaperClips();
- RefreshListOnMainThread();
- }
- });
- }
- void LoadPaperClips()
- {
- Task.Run(() =>
- {
- bool imagesPresent = false;
- foreach (NotificationShell shell in notificationShells)
- {
- CoreTable table = new Client<NotificationDocument>().Query(new Filter<NotificationDocument>(x => x.EntityLink.ID).IsEqualTo(shell.ID),
- new Columns<NotificationDocument>(x => x.ID));
- if (table.Rows.Any())
- {
- shell.ImageColumnWidth = 30;
- imagesPresent = true;
- shell.ImageVisible = true;
- shell.CreatedColumnSpan = 2;
- }
- }
- if (imagesPresent)
- {
- RefreshListOnMainThread();
- }
- });
- }
- #endregion
- #region Buttons / Taps
- async void NotificationListView_Tapped(object sender, EventArgs e)
- {
- NotificationShell shell = notificationListView.SelectedItem as NotificationShell;
- string extra = "";
- if (!string.IsNullOrWhiteSpace(shell.EntityType))
- {
- if (shell.EntityType == "Comal.Classes.Kanban")
- extra = "View Task";
- if (shell.EntityType == "Comal.Classes.LeaveRequest")
- extra = "View Leave";
- }
- string chosenOption = await DisplayActionSheet(shell.Description, "Cancel", null, "View / Reply Message", "Dismiss Message", extra);
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "View / Reply Message":
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
- {
- NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID);
- Navigation.PushAsync(newReplyNotification);
- }
- break;
- case "Dismiss Message":
- DismissNotification(shell);
- break;
- case "View Task":
- AddEditTask taskPage = new AddEditTask(shell.EntityID);
- Navigation.PushAsync(taskPage);
- break;
- case "View Leave":
- LeaveRequestList leaveRequestList = new LeaveRequestList();
- Navigation.PushAsync(leaveRequestList);
- break;
- default:
- return;
- }
- }
- protected override void OnDisappearing()
- {
- NotificationsClosed?.Invoke(notificationShells.Count());
- base.OnDisappearing();
- }
- void New_Clicked(object sender, EventArgs e)
- {
- NewReplyNotification newReplyNotification = new NewReplyNotification();
- Navigation.PushAsync(newReplyNotification);
- }
- void DismissAll_Clicked(object sender, EventArgs e)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- notificationListView.ItemsSource = null;
- numberOfItemsLbl.Text = "Number of items: 0";
- });
- Task.Run(() =>
- {
- List<Notification> notifications = new List<Notification>();
- foreach (NotificationShell shell in notificationShells)
- {
- Notification notification = new Notification { ID = shell.ID };
- notification.Closed = DateTime.Now;
- notifications.Add(notification);
- }
- new Client<Notification>().Save(notifications, "Dismissed on mobile device");
- });
- }
- #endregion
- #region Utils
- void DismissNotification(NotificationShell shell)
- {
- Task.Run(() =>
- {
- Notification notification = new Notification { ID = shell.ID };
- notificationShells.Remove(shell);
- RefreshListOnMainThread();
- notification.Closed = DateTime.Now;
- new Client<Notification>().Save(notification, "Dismissed on mobile device");
- });
- }
- void RefreshListOnMainThread()
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- notificationListView.ItemsSource = null;
- notificationListView.ItemsSource = notificationShells;
- numberOfItemsLbl.Text = "Number of items: " + notificationShells.Count;
- });
- }
- #endregion
- }
- public class NotificationShell
- {
- public Guid ID { get; set; }
- public DateTime Created { get; set; }
- public string Sender { get; set; }
- public string Title { get; set; }
- public string Description { get; set; }
- public string EntityType { get; set; }
- public Guid EntityID { get; set; }
- public double ImageColumnWidth { get; set; }
- public bool ImageVisible { get; set; }
- public double CreatedColumnSpan { get; set; }
- public NotificationShell()
- {
- ID = Guid.Empty;
- Created = DateTime.MinValue;
- Sender = "";
- Title = "";
- Description = "";
- EntityType = "";
- EntityID = Guid.Empty;
- ImageColumnWidth = 0;
- ImageVisible = false;
- CreatedColumnSpan = 1;
- }
- }
- }
|