NotificationList.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading.Tasks;
  9. using System.Threading;
  10. using System.Linq;
  11. using Xamarin.Forms;
  12. using XF.Material.Forms.UI;
  13. using XF.Material.Forms.UI.Dialogs;
  14. using InABox.Core;
  15. using InABox.Clients;
  16. using Comal.Classes;
  17. using System.IO;
  18. using Plugin.Media;
  19. using InABox.Mobile;
  20. using System.Linq.Expressions;
  21. using Xamarin.Forms;
  22. using Xamarin.Forms.Xaml;
  23. using comal.timesheets.CustomControls;
  24. using comal.timesheets.Tasks;
  25. namespace comal.timesheets
  26. {
  27. public delegate void NotificationsClosedEvent(int numberOfNotifications);
  28. [XamlCompilation(XamlCompilationOptions.Compile)]
  29. public partial class NotificationList : ContentPage
  30. {
  31. public event NotificationsClosedEvent NotificationsClosed;
  32. List<NotificationShell> notificationShells = new List<NotificationShell>();
  33. #region Constructor + Loading
  34. public NotificationList()
  35. {
  36. InitializeComponent();
  37. }
  38. protected override void OnAppearing()
  39. {
  40. LoadData();
  41. base.OnAppearing();
  42. }
  43. private async void LoadData()
  44. {
  45. await Task.Run(() =>
  46. {
  47. notificationShells.Clear();
  48. var table = new Client<Notification>().Query(
  49. new Filter<Notification>(x => x.Employee.ID).IsEqualTo(GlobalVariables.EmpID).And(X => X.Closed).IsEqualTo(DateTime.MinValue),
  50. new Columns<Notification>(
  51. x => x.ID, //0
  52. x => x.Sender.Name, //1
  53. x => x.Title, //2
  54. x => x.Created, //3
  55. x => x.Description, //4
  56. x => x.EntityType, //5
  57. x => x.EntityID //6
  58. ),
  59. new SortOrder<Notification>(x => x.Created, SortDirection.Descending)
  60. );
  61. string emptyString = "";
  62. if (table.Rows.Count != 0)
  63. {
  64. foreach (var row in table.Rows)
  65. {
  66. List<object> list = row.Values;
  67. if (list[0] == null) { list[0] = Guid.Empty; }
  68. if (list[1] == null) { list[1] = emptyString; }
  69. if (list[2] == null) { list[2] = emptyString; }
  70. if (list[3] == null) { list[3] = DateTime.MinValue; }
  71. if (list[4] == null) { list[4] = emptyString; }
  72. if (list[5] == null) { list[5] = emptyString; }
  73. if (list[6] == null) { list[6] = Guid.Empty; }
  74. NotificationShell notificationShell = new NotificationShell();
  75. notificationShell.ID = Guid.Parse(list[0].ToString());
  76. notificationShell.Sender = list[1].ToString();
  77. notificationShell.Title = list[2].ToString();
  78. notificationShell.Created = DateTime.Parse(list[3].ToString());
  79. notificationShell.Description = CoreUtils.StripHTML(list[4].ToString());
  80. notificationShell.EntityType = list[5].ToString();
  81. notificationShell.EntityID = Guid.Parse(list[6].ToString());
  82. notificationShells.Add(notificationShell);
  83. }
  84. LoadPaperClips();
  85. RefreshListOnMainThread();
  86. }
  87. });
  88. }
  89. void LoadPaperClips()
  90. {
  91. Task.Run(() =>
  92. {
  93. bool imagesPresent = false;
  94. foreach (NotificationShell shell in notificationShells)
  95. {
  96. CoreTable table = new Client<NotificationDocument>().Query(new Filter<NotificationDocument>(x => x.EntityLink.ID).IsEqualTo(shell.ID),
  97. new Columns<NotificationDocument>(x => x.ID));
  98. if (table.Rows.Any())
  99. {
  100. shell.ImageColumnWidth = 30;
  101. imagesPresent = true;
  102. shell.ImageVisible = true;
  103. shell.CreatedColumnSpan = 2;
  104. }
  105. }
  106. if (imagesPresent)
  107. {
  108. RefreshListOnMainThread();
  109. }
  110. });
  111. }
  112. #endregion
  113. #region Buttons / Taps
  114. async void NotificationListView_Tapped(object sender, EventArgs e)
  115. {
  116. NotificationShell shell = notificationListView.SelectedItem as NotificationShell;
  117. string extra = "";
  118. if (!string.IsNullOrWhiteSpace(shell.EntityType))
  119. {
  120. if (shell.EntityType == "Comal.Classes.Kanban")
  121. extra = "View Task";
  122. if (shell.EntityType == "Comal.Classes.LeaveRequest")
  123. extra = "View Leave";
  124. }
  125. string chosenOption = await DisplayActionSheet(shell.Description, "Cancel", null, "View / Reply Message", "Dismiss Message", extra);
  126. switch (chosenOption)
  127. {
  128. case "Cancel":
  129. return;
  130. case "View / Reply Message":
  131. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  132. {
  133. NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID);
  134. Navigation.PushAsync(newReplyNotification);
  135. }
  136. break;
  137. case "Dismiss Message":
  138. DismissNotification(shell);
  139. break;
  140. case "View Task":
  141. AddEditTask taskPage = new AddEditTask(shell.EntityID);
  142. Navigation.PushAsync(taskPage);
  143. break;
  144. case "View Leave":
  145. LeaveRequestList leaveRequestList = new LeaveRequestList();
  146. Navigation.PushAsync(leaveRequestList);
  147. break;
  148. default:
  149. return;
  150. }
  151. }
  152. protected override void OnDisappearing()
  153. {
  154. NotificationsClosed?.Invoke(notificationShells.Count());
  155. base.OnDisappearing();
  156. }
  157. void New_Clicked(object sender, EventArgs e)
  158. {
  159. NewReplyNotification newReplyNotification = new NewReplyNotification();
  160. Navigation.PushAsync(newReplyNotification);
  161. }
  162. void DismissAll_Clicked(object sender, EventArgs e)
  163. {
  164. Device.BeginInvokeOnMainThread(() =>
  165. {
  166. notificationListView.ItemsSource = null;
  167. numberOfItemsLbl.Text = "Number of items: 0";
  168. });
  169. Task.Run(() =>
  170. {
  171. List<Notification> notifications = new List<Notification>();
  172. foreach (NotificationShell shell in notificationShells)
  173. {
  174. Notification notification = new Notification { ID = shell.ID };
  175. notification.Closed = DateTime.Now;
  176. notifications.Add(notification);
  177. }
  178. new Client<Notification>().Save(notifications, "Dismissed on mobile device");
  179. });
  180. }
  181. #endregion
  182. #region Utils
  183. void DismissNotification(NotificationShell shell)
  184. {
  185. Task.Run(() =>
  186. {
  187. Notification notification = new Notification { ID = shell.ID };
  188. notificationShells.Remove(shell);
  189. RefreshListOnMainThread();
  190. notification.Closed = DateTime.Now;
  191. new Client<Notification>().Save(notification, "Dismissed on mobile device");
  192. });
  193. }
  194. void RefreshListOnMainThread()
  195. {
  196. Device.BeginInvokeOnMainThread(() =>
  197. {
  198. notificationListView.ItemsSource = null;
  199. notificationListView.ItemsSource = notificationShells;
  200. numberOfItemsLbl.Text = "Number of items: " + notificationShells.Count;
  201. });
  202. }
  203. #endregion
  204. }
  205. public class NotificationShell
  206. {
  207. public Guid ID { get; set; }
  208. public DateTime Created { get; set; }
  209. public string Sender { get; set; }
  210. public string Title { get; set; }
  211. public string Description { get; set; }
  212. public string EntityType { get; set; }
  213. public Guid EntityID { get; set; }
  214. public double ImageColumnWidth { get; set; }
  215. public bool ImageVisible { get; set; }
  216. public double CreatedColumnSpan { get; set; }
  217. public NotificationShell()
  218. {
  219. ID = Guid.Empty;
  220. Created = DateTime.MinValue;
  221. Sender = "";
  222. Title = "";
  223. Description = "";
  224. EntityType = "";
  225. EntityID = Guid.Empty;
  226. ImageColumnWidth = 0;
  227. ImageVisible = false;
  228. CreatedColumnSpan = 1;
  229. }
  230. }
  231. }