using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xamarin.Forms; using InABox.Core; using InABox.Clients; using Comal.Classes; using Xamarin.Forms.Xaml; using comal.timesheets.Tasks; namespace comal.timesheets { public delegate void NotificationsClosedEvent(int numberOfNotifications); [XamlCompilation(XamlCompilationOptions.Compile)] public partial class NotificationList { public event NotificationsClosedEvent NotificationsClosed; List notificationShells = new List(); #region Constructor + Loading public NotificationList() { InitializeComponent(); App.Data.Notifications.Load(() => { Dispatcher.BeginInvokeOnMainThread(() => { notificationListView.ItemsSource = App.Data.Notifications; }); }); } protected override void OnDisappearing() { NotificationsClosed?.Invoke(notificationShells.Count()); base.OnDisappearing(); } #endregion #region Buttons / Taps private async void NotificationListView_Tapped(object sender, EventArgs e) { NotificationShell shell = notificationListView.SelectedItem as NotificationShell; string extra = CreateOption(shell); string chosenOption = await DisplayActionSheet(shell.Description, "Cancel", null, "View / Reply Message", "Dismiss Message", extra); ProcessOption(chosenOption, shell); } private void ProcessOption(string chosenOption, NotificationShell shell) { switch (chosenOption) { case "Cancel": return; case "View / Reply Message": ReplyNotification(shell); break; case "Dismiss Message": DismissNotification(shell); break; case VIEWTASK: ViewTask(shell); break; case VIEWLEAVE: OpenLeaveList(); break; case VIEWLEAVEFORM: ViewRequestForm(shell); break; case VIEWDELIVERY: ViewDelivery(shell); break; default: return; } } private void ViewDelivery(NotificationShell shell) { DeliveryDetails page = new DeliveryDetails() { DeliveryID = shell.EntityID }; Navigation.PushAsync(page); } private void ViewRequestForm(NotificationShell shell) { DigitalFormHostModel model = new DigitalFormHostModel(); LeaveRequest request = new LeaveRequest(); request.ID = shell.EntityID; LeaveRequestForm form = new Client().Query(new Filter(x => x.Parent.ID).IsEqualTo(shell.EntityID)).Rows.FirstOrDefault().ToObject(); DigitalFormLayout digitalFormLayout = new Client().Query(new Filter(x => x.Form.ID).IsEqualTo(form.Form.ID).And(x => x.Type).IsEqualTo(DFLayoutType.Mobile)).Rows.FirstOrDefault().ToObject(); model.LoadItems(request, form, digitalFormLayout); DigitalFormHost host = new DigitalFormHost(model); Navigation.PushAsync(host); } private void ReplyNotification(NotificationShell shell) { NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID); Navigation.PushAsync(newReplyNotification); } private void ViewTask(NotificationShell shell) { AddEditTask taskPage = new AddEditTask(shell.EntityID); Navigation.PushAsync(taskPage); } private void OpenLeaveList() { LeaveRequestList leaveRequestList = new LeaveRequestList(); Navigation.PushAsync(leaveRequestList); } const string VIEWTASK = "View Task"; const string VIEWLEAVE = "View Leave"; const string VIEWLEAVEFORM = "View Leave Request Form"; const string VIEWDELIVERY = "View Delivery"; private string CreateOption(NotificationShell shell) { if (string.IsNullOrWhiteSpace(shell.EntityType)) return ""; if (shell.EntityType == "Comal.Classes.Kanban") return VIEWTASK; if (shell.EntityType == "Comal.Classes.LeaveRequest") return VIEWLEAVE; if (shell.EntityType == "Comal.Classes.LeaveRequestLink") return VIEWLEAVEFORM; if (shell.EntityType == "Comal.Classes.Delivery") return VIEWDELIVERY; else return ""; } 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 notifications = new List(); foreach (NotificationShell shell in notificationShells) { Notification notification = new Notification { ID = shell.ID }; notification.Closed = DateTime.Now; notifications.Add(notification); } new Client().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().Save(notification, "Dismissed on mobile device"); }); } void RefreshListOnMainThread() { Device.BeginInvokeOnMainThread(() => { notificationListView.ItemsSource = null; notificationListView.ItemsSource = notificationShells; numberOfItemsLbl.Text = "Number of items: " + notificationShells.Count; }); } #endregion } }