NotificationList.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 InABox.Mobile;
  10. using Xamarin.CommunityToolkit.UI.Views;
  11. using Xamarin.Forms.Xaml;
  12. namespace PRS.Mobile
  13. {
  14. //public delegate void NotificationsClosedEvent(int numberOfNotifications);
  15. public class NotificationTypeConverter : UtilityConverter<String,ImageSource>
  16. {
  17. private readonly Dictionary<String, ImageSource> _images = new Dictionary<string, ImageSource>()
  18. {
  19. { "", ImageSource.FromFile("notification") },
  20. { typeof(Delivery).EntityName(), ImageSource.FromFile("delivery") },
  21. { typeof(Kanban).EntityName(), ImageSource.FromFile("task") },
  22. { typeof(KanbanForm).EntityName(), ImageSource.FromFile("task") },
  23. { typeof(LeaveRequest).EntityName(), ImageSource.FromFile("holiday") },
  24. { typeof(LeaveRequestForm).EntityName(), ImageSource.FromFile("holiday") },
  25. { typeof(EmployeeForm).EntityName(), ImageSource.FromFile("person") },
  26. { typeof(EmployeeQualification).EntityName(), ImageSource.FromFile("badge") },
  27. { typeof(JobForm).EntityName(), ImageSource.FromFile("construction") },
  28. };
  29. protected override ImageSource Convert(string value)
  30. {
  31. if (!_images.TryGetValue(value ?? "", out ImageSource result))
  32. result = _images[""];
  33. return result;
  34. }
  35. }
  36. [XamlCompilation(XamlCompilationOptions.Compile)]
  37. public partial class NotificationList
  38. {
  39. //public event NotificationsClosedEvent NotificationsClosed;
  40. //List<NotificationShell> notificationShells = new List<NotificationShell>();
  41. public NotificationList()
  42. {
  43. InitializeComponent();
  44. RefreshData(false, true);
  45. // App.Data.Notifications.Load(() =>
  46. // {
  47. // Dispatcher.BeginInvokeOnMainThread(() =>
  48. // {
  49. // notificationListView.ItemsSource = App.Data.Notifications;
  50. // });
  51. // });
  52. }
  53. private void RefreshData(bool force, bool async)
  54. {
  55. if (async)
  56. App.Data.Notifications.Refresh(force, () => Dispatcher.BeginInvokeOnMainThread(Refresh));
  57. else
  58. {
  59. App.Data.Notifications.Refresh(force);
  60. Refresh();
  61. }
  62. }
  63. private void Refresh()
  64. {
  65. foreach (var shell in App.Data.Notifications.Items.ToArray())
  66. shell.Selected = false;
  67. App.Data.Notifications.Search(FilterShell);
  68. _notifications.ItemsSource = App.Data.Notifications.Items;
  69. }
  70. private String _currentFilter = "";
  71. private bool FilterShell(NotificationShell shell)
  72. {
  73. bool bOK = shell.Closed.IsEmpty() == (_view.SelectedItem.Index == 0);
  74. bOK =
  75. bOK &&
  76. (
  77. String.IsNullOrWhiteSpace(_currentFilter)
  78. || shell.Title.ToUpper().Contains(_currentFilter)
  79. || shell.Description.ToUpper().Contains(_currentFilter)
  80. || shell.Sender.ToUpper().Contains(_currentFilter)
  81. );
  82. return bOK;
  83. }
  84. private void _search_Changed(object sender, MobileSearchBarTextChangedArgs args)
  85. {
  86. _currentFilter = args.Text.ToUpper();
  87. }
  88. private void _notifications_RefreshRequested(object sender, MobileListRefreshEventArgs args)
  89. {
  90. RefreshData(true,false);
  91. }
  92. protected override void OnDisappearing()
  93. {
  94. //NotificationsClosed?.Invoke(notificationShells.Count());
  95. base.OnDisappearing();
  96. }
  97. #region Buttons / Taps
  98. // private async void NotificationListView_Tapped(object sender, EventArgs e)
  99. // {
  100. // NotificationShell shell = notificationListView.SelectedItem as NotificationShell;
  101. // string extra = CreateOption(shell);
  102. // string chosenOption = await DisplayActionSheet(shell.Description, "Cancel", null, "View / Reply Message", "Dismiss Message", extra);
  103. // ProcessOption(chosenOption, shell);
  104. // }
  105. //
  106. // private void ProcessOption(string chosenOption, NotificationShell shell)
  107. // {
  108. // switch (chosenOption)
  109. // {
  110. // case "Cancel":
  111. // return;
  112. // case "View / Reply Message":
  113. // ReplyNotification(shell);
  114. // break;
  115. // case "Dismiss Message":
  116. // DismissNotification(shell);
  117. // break;
  118. // case VIEWTASK:
  119. // ViewTask(shell);
  120. // break;
  121. // case VIEWLEAVE:
  122. // OpenLeaveList();
  123. // break;
  124. // case VIEWLEAVEFORM:
  125. // ViewRequestForm(shell);
  126. // break;
  127. // case VIEWDELIVERY:
  128. // ViewDelivery(shell);
  129. // break;
  130. // default:
  131. // return;
  132. // }
  133. // }
  134. //
  135. // private void ViewDelivery(NotificationShell shell)
  136. // {
  137. // Task.Run(async () =>
  138. // {
  139. // DeliveryEdit page = await DeliveryEdit.Load(shell.EntityID);
  140. // Navigation.PushAsync(page);
  141. // });
  142. //
  143. // }
  144. //
  145. // private void ViewRequestForm(NotificationShell shell)
  146. // {
  147. // DigitalFormHostModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm> model = new DigitalFormHostModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm>();
  148. // LeaveRequest request = new LeaveRequest();
  149. // request.ID = shell.EntityID;
  150. // LeaveRequestForm form = new Client<LeaveRequestForm>().Query(new Filter<LeaveRequestForm>(x => x.Parent.ID).IsEqualTo(shell.EntityID)).Rows.FirstOrDefault().ToObject<LeaveRequestForm>();
  151. // 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>();
  152. // model.LoadItems(request, form, digitalFormLayout);
  153. // DigitalFormHost host = new DigitalFormHost(model);
  154. // Navigation.PushAsync(host);
  155. // }
  156. //
  157. // private void ReplyNotification(NotificationShell shell)
  158. // {
  159. // NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID);
  160. // Navigation.PushAsync(newReplyNotification);
  161. // }
  162. //
  163. // private void ViewTask(NotificationShell shell)
  164. // {
  165. // AddEditTask taskPage = new AddEditTask(shell.EntityID);
  166. // Navigation.PushAsync(taskPage);
  167. // }
  168. //
  169. // private void OpenLeaveList()
  170. // {
  171. // LeaveRequestList leaveRequestList = new LeaveRequestList();
  172. // Navigation.PushAsync(leaveRequestList);
  173. // }
  174. //
  175. // const string VIEWTASK = "View Task";
  176. // const string VIEWLEAVE = "View Leave";
  177. // const string VIEWLEAVEFORM = "View Leave Request Form";
  178. // const string VIEWDELIVERY = "View Delivery";
  179. //
  180. // private string CreateOption(NotificationShell shell)
  181. // {
  182. // if (string.IsNullOrWhiteSpace(shell.EntityType))
  183. // return "";
  184. // if (shell.EntityType == "Comal.Classes.Kanban")
  185. // return VIEWTASK;
  186. // if (shell.EntityType == "Comal.Classes.LeaveRequest")
  187. // return VIEWLEAVE;
  188. // if (shell.EntityType == "Comal.Classes.LeaveRequestLink")
  189. // return VIEWLEAVEFORM;
  190. // if (shell.EntityType == "Comal.Classes.Delivery")
  191. // return VIEWDELIVERY;
  192. // else
  193. // return "";
  194. // }
  195. //
  196. // void New_Clicked(object sender, EventArgs e)
  197. // {
  198. // NewReplyNotification newReplyNotification = new NewReplyNotification();
  199. // Navigation.PushAsync(newReplyNotification);
  200. // }
  201. //
  202. #endregion
  203. private void UpdateStatus(DateTime timestamp)
  204. {
  205. ProgressVisible = true;
  206. var task = Task.Run(() =>
  207. {
  208. foreach (var shell in App.Data.Notifications.Items.Where(x => x.Selected).ToArray())
  209. {
  210. shell.Closed = timestamp;
  211. shell.Save("Updated by Mobile Device");
  212. }
  213. });
  214. task.Wait();
  215. RefreshData(true,false);
  216. ProgressVisible = false;
  217. }
  218. private void _markAsRead_Clicked(object sender, EventArgs e)
  219. {
  220. UpdateStatus(DateTime.Now);
  221. }
  222. private void _markAsUnread_Clicked(object sender, EventArgs e)
  223. {
  224. UpdateStatus(DateTime.MinValue);
  225. }
  226. private void _selectAll_Clicked(object sender, EventArgs e)
  227. {
  228. foreach (var shell in App.Data.Notifications.Items.ToArray())
  229. shell.Selected = true;
  230. App.Data.Notifications.Search();
  231. }
  232. private void _selectNone_Clicked(object sender, EventArgs e)
  233. {
  234. foreach (var shell in App.Data.Notifications.Items.ToArray())
  235. shell.Selected = false;
  236. App.Data.Notifications.Search();
  237. }
  238. private void _newmessage_OnClicked(object sender, EventArgs e)
  239. {
  240. NewReplyNotification newReplyNotification = new NewReplyNotification();
  241. Navigation.PushAsync(newReplyNotification);
  242. }
  243. private void _options_OnAppearing(object sender, EventArgs e)
  244. {
  245. _markAsRead.IsVisible = (_view.SelectedItem.Index == 0) && App.Data.Notifications.Any(x => x.Selected);
  246. _markAsUnread.IsVisible = (_view.SelectedItem.Index == 1) && App.Data.Notifications.Any(x => x.Selected);
  247. _selectAll.IsVisible = App.Data.Notifications.Any(x => !x.Selected);
  248. _selectNone.IsVisible = App.Data.Notifications.Any(x => x.Selected);
  249. _separator.IsVisible = (_markAsRead.IsVisible || _markAsUnread.IsVisible) && (_selectAll.IsVisible || _selectNone.IsVisible);
  250. }
  251. private void _view_OnSelectionChanged(object sender, EventArgs e)
  252. {
  253. Refresh();
  254. }
  255. private void _notification_Tapped(object sender, EventArgs e)
  256. {
  257. if ((sender as Frame)?.BindingContext is NotificationShell shell)
  258. {
  259. // if (String.Equals(shell.EntityType, typeof(Delivery).EntityName()))
  260. // {
  261. //
  262. // }
  263. // else if (String.Equals(shell.EntityType, typeof(Kanban).EntityName()))
  264. // {
  265. //
  266. // }
  267. // else if (String.Equals(shell.EntityType, typeof(KanbanForm).EntityName()))
  268. // {
  269. //
  270. // }
  271. // else if (String.Equals(shell.EntityType, typeof(LeaveRequest).EntityName()))
  272. // {
  273. //
  274. // }
  275. // else if (String.Equals(shell.EntityType, typeof(LeaveRequestForm).EntityName()))
  276. // {
  277. //
  278. // }
  279. // else if (String.Equals(shell.EntityType, typeof(EmployeeForm).EntityName()))
  280. // {
  281. //
  282. // }
  283. // else if (String.Equals(shell.EntityType, typeof(EmployeeQualification).EntityName()))
  284. // {
  285. //
  286. // }
  287. // else if (String.Equals(shell.EntityType, typeof(JobForm).EntityName()))
  288. // {
  289. //
  290. // }
  291. // else
  292. {
  293. NewReplyNotification newReplyNotification = new NewReplyNotification(shell.ID);
  294. Navigation.PushAsync(newReplyNotification);
  295. }
  296. }
  297. }
  298. }
  299. }