NotificationList.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Xamarin.Forms;
  6. using InABox.Core;
  7. using InABox.Clients;
  8. using Comal.Classes;
  9. using Xamarin.Forms.Xaml;
  10. using comal.timesheets.Tasks;
  11. namespace comal.timesheets
  12. {
  13. public delegate void NotificationsClosedEvent(int numberOfNotifications);
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class NotificationList
  16. {
  17. public event NotificationsClosedEvent NotificationsClosed;
  18. List<NotificationShell> notificationShells = new List<NotificationShell>();
  19. #region Constructor + Loading
  20. public NotificationList()
  21. {
  22. InitializeComponent();
  23. App.Data.Notifications.Load(() =>
  24. {
  25. Dispatcher.BeginInvokeOnMainThread(() =>
  26. {
  27. notificationListView.ItemsSource = App.Data.Notifications;
  28. });
  29. });
  30. }
  31. protected override void OnDisappearing()
  32. {
  33. NotificationsClosed?.Invoke(notificationShells.Count());
  34. base.OnDisappearing();
  35. }
  36. #endregion
  37. #region Buttons / Taps
  38. private async void NotificationListView_Tapped(object sender, EventArgs e)
  39. {
  40. NotificationShell shell = notificationListView.SelectedItem as NotificationShell;
  41. string extra = CreateOption(shell);
  42. string chosenOption = await DisplayActionSheet(shell.Description, "Cancel", null, "View / Reply Message", "Dismiss Message", extra);
  43. ProcessOption(chosenOption, shell);
  44. }
  45. private void ProcessOption(string chosenOption, NotificationShell shell)
  46. {
  47. switch (chosenOption)
  48. {
  49. case "Cancel":
  50. return;
  51. case "View / Reply Message":
  52. ReplyNotification(shell);
  53. break;
  54. case "Dismiss Message":
  55. DismissNotification(shell);
  56. break;
  57. case VIEWTASK:
  58. ViewTask(shell);
  59. break;
  60. case VIEWLEAVE:
  61. OpenLeaveList();
  62. break;
  63. case VIEWLEAVEFORM:
  64. ViewRequestForm(shell);
  65. break;
  66. case VIEWDELIVERY:
  67. ViewDelivery(shell);
  68. break;
  69. default:
  70. return;
  71. }
  72. }
  73. private void ViewDelivery(NotificationShell shell)
  74. {
  75. DeliveryDetails page = new DeliveryDetails() { DeliveryID = shell.EntityID };
  76. Navigation.PushAsync(page);
  77. }
  78. private void ViewRequestForm(NotificationShell shell)
  79. {
  80. DigitalFormHostModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm> model = new DigitalFormHostModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm>();
  81. LeaveRequest request = new LeaveRequest();
  82. request.ID = shell.EntityID;
  83. LeaveRequestForm form = new Client<LeaveRequestForm>().Query(new Filter<LeaveRequestForm>(x => x.Parent.ID).IsEqualTo(shell.EntityID)).Rows.FirstOrDefault().ToObject<LeaveRequestForm>();
  84. DigitalFormLayout digitalFormLayout = new Client<DigitalFormLayout>().Query(new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(form.Form.ID).And(x => x.Type).IsEqualTo(DFLayoutType.Mobile)).Rows.FirstOrDefault().ToObject<DigitalFormLayout>();
  85. model.LoadItems(request, form, digitalFormLayout);
  86. DigitalFormHost host = new DigitalFormHost(model);
  87. Navigation.PushAsync(host);
  88. }
  89. private void ReplyNotification(NotificationShell shell)
  90. {
  91. NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID);
  92. Navigation.PushAsync(newReplyNotification);
  93. }
  94. private void ViewTask(NotificationShell shell)
  95. {
  96. AddEditTask taskPage = new AddEditTask(shell.EntityID);
  97. Navigation.PushAsync(taskPage);
  98. }
  99. private void OpenLeaveList()
  100. {
  101. LeaveRequestList leaveRequestList = new LeaveRequestList();
  102. Navigation.PushAsync(leaveRequestList);
  103. }
  104. const string VIEWTASK = "View Task";
  105. const string VIEWLEAVE = "View Leave";
  106. const string VIEWLEAVEFORM = "View Leave Request Form";
  107. const string VIEWDELIVERY = "View Delivery";
  108. private string CreateOption(NotificationShell shell)
  109. {
  110. if (string.IsNullOrWhiteSpace(shell.EntityType))
  111. return "";
  112. if (shell.EntityType == "Comal.Classes.Kanban")
  113. return VIEWTASK;
  114. if (shell.EntityType == "Comal.Classes.LeaveRequest")
  115. return VIEWLEAVE;
  116. if (shell.EntityType == "Comal.Classes.LeaveRequestLink")
  117. return VIEWLEAVEFORM;
  118. if (shell.EntityType == "Comal.Classes.Delivery")
  119. return VIEWDELIVERY;
  120. else
  121. return "";
  122. }
  123. void New_Clicked(object sender, EventArgs e)
  124. {
  125. NewReplyNotification newReplyNotification = new NewReplyNotification();
  126. Navigation.PushAsync(newReplyNotification);
  127. }
  128. void DismissAll_Clicked(object sender, EventArgs e)
  129. {
  130. Device.BeginInvokeOnMainThread(() =>
  131. {
  132. notificationListView.ItemsSource = null;
  133. numberOfItemsLbl.Text = "Number of items: 0";
  134. });
  135. Task.Run(() =>
  136. {
  137. List<Notification> notifications = new List<Notification>();
  138. foreach (NotificationShell shell in notificationShells)
  139. {
  140. Notification notification = new Notification { ID = shell.ID };
  141. notification.Closed = DateTime.Now;
  142. notifications.Add(notification);
  143. }
  144. new Client<Notification>().Save(notifications, "Dismissed on mobile device");
  145. });
  146. }
  147. #endregion
  148. #region Utils
  149. void DismissNotification(NotificationShell shell)
  150. {
  151. Task.Run(() =>
  152. {
  153. Notification notification = new Notification { ID = shell.ID };
  154. notificationShells.Remove(shell);
  155. RefreshListOnMainThread();
  156. notification.Closed = DateTime.Now;
  157. new Client<Notification>().Save(notification, "Dismissed on mobile device");
  158. });
  159. }
  160. void RefreshListOnMainThread()
  161. {
  162. Device.BeginInvokeOnMainThread(() =>
  163. {
  164. notificationListView.ItemsSource = null;
  165. notificationListView.ItemsSource = notificationShells;
  166. numberOfItemsLbl.Text = "Number of items: " + notificationShells.Count;
  167. });
  168. }
  169. #endregion
  170. }
  171. }