NotificationList.xaml.cs 12 KB

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