NotificationList.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 : ContentPage
  16. {
  17. public event NotificationsClosedEvent NotificationsClosed;
  18. List<NotificationShell> notificationShells = new List<NotificationShell>();
  19. #region Constructor + Loading
  20. public NotificationList()
  21. {
  22. InitializeComponent();
  23. }
  24. protected override void OnAppearing()
  25. {
  26. LoadData();
  27. base.OnAppearing();
  28. }
  29. private void LoadData()
  30. {
  31. Task.Run(() =>
  32. {
  33. notificationShells.Clear();
  34. var table = new Client<Notification>().Query(
  35. new Filter<Notification>(x => x.Employee.ID).IsEqualTo(GlobalVariables.EmpID).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
  36. new Columns<Notification>(
  37. x => x.ID, //0
  38. x => x.Sender.Name, //1
  39. x => x.Title, //2
  40. x => x.Created, //3
  41. x => x.Description, //4
  42. x => x.EntityType, //5
  43. x => x.EntityID //6
  44. ),
  45. new SortOrder<Notification>(x => x.Created, SortDirection.Descending)
  46. );
  47. string emptyString = "";
  48. if (table.Rows.Count != 0)
  49. {
  50. foreach (var row in table.Rows)
  51. {
  52. List<object> list = row.Values;
  53. if (list[0] == null) { list[0] = Guid.Empty; }
  54. if (list[1] == null) { list[1] = emptyString; }
  55. if (list[2] == null) { list[2] = emptyString; }
  56. if (list[3] == null) { list[3] = DateTime.MinValue; }
  57. if (list[4] == null) { list[4] = emptyString; }
  58. if (list[5] == null) { list[5] = emptyString; }
  59. if (list[6] == null) { list[6] = Guid.Empty; }
  60. NotificationShell notificationShell = new NotificationShell();
  61. notificationShell.ID = Guid.Parse(list[0].ToString());
  62. notificationShell.Sender = list[1].ToString();
  63. notificationShell.Title = list[2].ToString();
  64. notificationShell.Created = DateTime.Parse(list[3].ToString());
  65. notificationShell.Description = CoreUtils.StripHTML(list[4].ToString());
  66. notificationShell.EntityType = list[5].ToString();
  67. notificationShell.EntityID = Guid.Parse(list[6].ToString());
  68. notificationShells.Add(notificationShell);
  69. }
  70. LoadPaperClips();
  71. RefreshListOnMainThread();
  72. }
  73. });
  74. }
  75. private void LoadPaperClips()
  76. {
  77. Task.Run(() =>
  78. {
  79. bool imagesPresent = false;
  80. foreach (NotificationShell shell in notificationShells)
  81. {
  82. CoreTable table = new Client<NotificationDocument>().Query(new Filter<NotificationDocument>(x => x.EntityLink.ID).IsEqualTo(shell.ID),
  83. new Columns<NotificationDocument>(x => x.ID));
  84. if (table.Rows.Any())
  85. {
  86. shell.ImageColumnWidth = 30;
  87. imagesPresent = true;
  88. shell.ImageVisible = true;
  89. shell.CreatedColumnSpan = 2;
  90. }
  91. }
  92. if (imagesPresent)
  93. {
  94. RefreshListOnMainThread();
  95. }
  96. });
  97. }
  98. #endregion
  99. #region Buttons / Taps
  100. private async void NotificationListView_Tapped(object sender, EventArgs e)
  101. {
  102. NotificationShell shell = notificationListView.SelectedItem as NotificationShell;
  103. string extra = CreateOption(shell);
  104. string chosenOption = await DisplayActionSheet(shell.Description, "Cancel", null, "View / Reply Message", "Dismiss Message", extra);
  105. ProcessOption(chosenOption, shell);
  106. }
  107. private void ProcessOption(string chosenOption, NotificationShell shell)
  108. {
  109. switch (chosenOption)
  110. {
  111. case "Cancel":
  112. return;
  113. case "View / Reply Message":
  114. ReplyNotification(shell);
  115. break;
  116. case "Dismiss Message":
  117. DismissNotification(shell);
  118. break;
  119. case VIEWTASK:
  120. ViewTask(shell);
  121. break;
  122. case VIEWLEAVE:
  123. OpenLeaveList();
  124. break;
  125. case VIEWLEAVEFORM:
  126. ViewRequestForm(shell);
  127. break;
  128. case VIEWDELIVERY:
  129. ViewDelivery(shell);
  130. break;
  131. default:
  132. return;
  133. }
  134. }
  135. private void ViewDelivery(NotificationShell shell)
  136. {
  137. DeliveryDetails page = new DeliveryDetails(shell.EntityID);
  138. Navigation.PushAsync(page);
  139. }
  140. private void ViewRequestForm(NotificationShell shell)
  141. {
  142. DigitalFormHostModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm> model = new DigitalFormHostModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm>();
  143. LeaveRequest request = new LeaveRequest();
  144. request.ID = shell.EntityID;
  145. LeaveRequestForm form = new Client<LeaveRequestForm>().Query(new Filter<LeaveRequestForm>(x => x.Parent.ID).IsEqualTo(shell.EntityID)).Rows.FirstOrDefault().ToObject<LeaveRequestForm>();
  146. 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>();
  147. model.LoadItems(request, form, digitalFormLayout);
  148. DigitalFormHost host = new DigitalFormHost(model);
  149. Navigation.PushAsync(host);
  150. }
  151. private void ReplyNotification(NotificationShell shell)
  152. {
  153. NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID);
  154. Navigation.PushAsync(newReplyNotification);
  155. }
  156. private void ViewTask(NotificationShell shell)
  157. {
  158. AddEditTask taskPage = new AddEditTask(shell.EntityID);
  159. Navigation.PushAsync(taskPage);
  160. }
  161. private void OpenLeaveList()
  162. {
  163. LeaveRequestList leaveRequestList = new LeaveRequestList();
  164. Navigation.PushAsync(leaveRequestList);
  165. }
  166. const string VIEWTASK = "View Task";
  167. const string VIEWLEAVE = "View Leave";
  168. const string VIEWLEAVEFORM = "View Leave Request Form";
  169. const string VIEWDELIVERY = "View Delivery";
  170. private string CreateOption(NotificationShell shell)
  171. {
  172. if (string.IsNullOrWhiteSpace(shell.EntityType))
  173. return "";
  174. if (shell.EntityType == "Comal.Classes.Kanban")
  175. return VIEWTASK;
  176. if (shell.EntityType == "Comal.Classes.LeaveRequest")
  177. return VIEWLEAVE;
  178. if (shell.EntityType == "Comal.Classes.LeaveRequestLink")
  179. return VIEWLEAVEFORM;
  180. if (shell.EntityType == "Comal.Classes.Delivery")
  181. return VIEWDELIVERY;
  182. else
  183. return "";
  184. }
  185. protected override void OnDisappearing()
  186. {
  187. NotificationsClosed?.Invoke(notificationShells.Count());
  188. base.OnDisappearing();
  189. }
  190. void New_Clicked(object sender, EventArgs e)
  191. {
  192. NewReplyNotification newReplyNotification = new NewReplyNotification();
  193. Navigation.PushAsync(newReplyNotification);
  194. }
  195. void DismissAll_Clicked(object sender, EventArgs e)
  196. {
  197. Device.BeginInvokeOnMainThread(() =>
  198. {
  199. notificationListView.ItemsSource = null;
  200. numberOfItemsLbl.Text = "Number of items: 0";
  201. });
  202. Task.Run(() =>
  203. {
  204. List<Notification> notifications = new List<Notification>();
  205. foreach (NotificationShell shell in notificationShells)
  206. {
  207. Notification notification = new Notification { ID = shell.ID };
  208. notification.Closed = DateTime.Now;
  209. notifications.Add(notification);
  210. }
  211. new Client<Notification>().Save(notifications, "Dismissed on mobile device");
  212. });
  213. }
  214. #endregion
  215. #region Utils
  216. void DismissNotification(NotificationShell shell)
  217. {
  218. Task.Run(() =>
  219. {
  220. Notification notification = new Notification { ID = shell.ID };
  221. notificationShells.Remove(shell);
  222. RefreshListOnMainThread();
  223. notification.Closed = DateTime.Now;
  224. new Client<Notification>().Save(notification, "Dismissed on mobile device");
  225. });
  226. }
  227. void RefreshListOnMainThread()
  228. {
  229. Device.BeginInvokeOnMainThread(() =>
  230. {
  231. notificationListView.ItemsSource = null;
  232. notificationListView.ItemsSource = notificationShells;
  233. numberOfItemsLbl.Text = "Number of items: " + notificationShells.Count;
  234. });
  235. }
  236. #endregion
  237. }
  238. public class NotificationShell
  239. {
  240. public Guid ID { get; set; }
  241. public DateTime Created { get; set; }
  242. public string Sender { get; set; }
  243. public string Title { get; set; }
  244. public string Description { get; set; }
  245. public string EntityType { get; set; }
  246. public Guid EntityID { get; set; }
  247. public double ImageColumnWidth { get; set; }
  248. public bool ImageVisible { get; set; }
  249. public double CreatedColumnSpan { get; set; }
  250. public NotificationShell()
  251. {
  252. ID = Guid.Empty;
  253. Created = DateTime.MinValue;
  254. Sender = "";
  255. Title = "";
  256. Description = "";
  257. EntityType = "";
  258. EntityID = Guid.Empty;
  259. ImageColumnWidth = 0;
  260. ImageVisible = false;
  261. CreatedColumnSpan = 1;
  262. }
  263. }
  264. }