AssignmentEditDetailsView.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Linq;
  3. using Comal.Classes;
  4. using InABox.Core;
  5. using InABox.Mobile;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. using XF.Material.Forms.UI.Dialogs;
  9. using PRSSecurity = InABox.Core.Security;
  10. namespace PRS.Mobile
  11. {
  12. [XamlCompilation(XamlCompilationOptions.Compile)]
  13. public partial class AssignmentEditDetailsView
  14. {
  15. public AssignmentEditDetailsView()
  16. {
  17. InitializeComponent();
  18. }
  19. public override void Refresh()
  20. {
  21. bool open = ViewModel.Item.Completed.IsEmpty();
  22. Completed.Text = open
  23. ? "Complete"
  24. : Security.CanEdit<Assignment>() ? "Re-Open" : $"Completed {ViewModel.Item.Completed:dd/MM/yy}";
  25. Subject.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
  26. BookedStart.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentBookings>();
  27. BookedFinish.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentBookings>();
  28. ActualStart.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentActualTimes>();
  29. ActualFinish.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentActualTimes>();
  30. //AsBookedBtn.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentActualTimes>();
  31. Job.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
  32. Task.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
  33. Activity.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
  34. Description.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
  35. Completed.IsEnabled = open || PRSSecurity.CanEdit<Assignment>();
  36. }
  37. private void Activity_Clicked(object sender, EventArgs e)
  38. {
  39. ShowPopup(() => CreateActivitySelection("Select Activity", (activity) =>
  40. {
  41. ViewModel.Item.ActivityID = activity.ID;
  42. ViewModel.Item.ActivityCode = activity.Code;
  43. ViewModel.Item.ActivityDescription = activity.Description;
  44. DoChanged("ActivityID");
  45. }));
  46. }
  47. private View CreateActivitySelection(String caption, Action<ActivityShell> selected)
  48. {
  49. SelectionView selection = new SelectionView
  50. {
  51. VerticalOptions = LayoutOptions.Fill,
  52. HorizontalOptions = LayoutOptions.Fill
  53. };
  54. selection.Configure += (sender, args) =>
  55. {
  56. args.Columns
  57. .BeginUpdate()
  58. .Clear()
  59. .Add(new MobileGridTextColumn<ActivityShell>() { Column = x => x.Code, Width = GridLength.Auto })
  60. .Add(new MobileGridTextColumn<ActivityShell>()
  61. {
  62. Column = x => x.Description, Width = GridLength.Star, Caption = caption,
  63. Alignment = TextAlignment.Start
  64. })
  65. .EndUpdate();
  66. };
  67. selection.Refresh += (sender, args) =>
  68. {
  69. App.Data.Activities.Refresh(false);
  70. return App.Data.Activities.Search((x=>x.IsLeave == false));
  71. };
  72. selection.SelectionChanged += (sender, args) =>
  73. {
  74. selected(args.SelectedItems.FirstOrDefault() as ActivityShell);
  75. DismissPopup();
  76. };
  77. selection.Load();
  78. return selection;
  79. }
  80. private void SelectJob_Clicked(object sender, EventArgs e)
  81. {
  82. ShowPopup(
  83. () => CreateJobSelection("Select Job", (job) =>
  84. {
  85. ViewModel.Item.JobID = job.ID;
  86. ViewModel.Item.JobNumber = job.JobNumber;
  87. ViewModel.Item.JobName = job.Name;
  88. ViewModel.Item.Latitude = job.Location.Latitude;
  89. ViewModel.Item.Longitude = job.Location.Longitude;
  90. DoChanged("JobID");
  91. })
  92. );
  93. }
  94. private View CreateJobSelection(String caption, Action<JobShell> selected)
  95. {
  96. SelectionView selection = new SelectionView
  97. {
  98. VerticalOptions = LayoutOptions.Fill,
  99. HorizontalOptions = LayoutOptions.Fill,
  100. CanSearch = true
  101. };
  102. selection.Configure += (sender, args) =>
  103. {
  104. args.Columns
  105. .BeginUpdate()
  106. .Clear()
  107. .Add(new MobileGridTextColumn<JobShell>() { Column = x=>x.JobNumber, Width = GridLength.Auto, Caption = "#"})
  108. .Add(new MobileGridTextColumn<JobShell>() { Column = x=>x.Name, Width = GridLength.Star, Caption = caption, Alignment = TextAlignment.Start})
  109. .EndUpdate();
  110. args.Filters.AddRange(App.Data.Jobs.AvailableFilters().Where(x=> x != null).Select(x=>x.Name));
  111. };
  112. selection.Refresh += (sender, args) =>
  113. {
  114. var result = App.Data.Jobs.Refresh(args.Force);
  115. args.LastUpdated = App.Data.Jobs.LastUpdated;
  116. return result;
  117. };
  118. selection.SelectionChanged += (sender, args) =>
  119. {
  120. selected(args.SelectedItems.FirstOrDefault() as JobShell);
  121. DismissPopup();
  122. };
  123. selection.Load();
  124. return selection;
  125. }
  126. private void Task_Clicked(object sender, EventArgs e)
  127. {
  128. ShowPopup(() => CreateTaskSelection("Select Task", (kanban) =>
  129. {
  130. ViewModel.Item.TaskID = kanban.ID;
  131. ViewModel.Item.TaskNumber = kanban.Number;
  132. ViewModel.Item.TaskName = kanban.Title;
  133. DoChanged("TaskID");
  134. }));
  135. }
  136. private View CreateTaskSelection(String caption, Action<KanbanShell> selected)
  137. {
  138. SelectionView selection = new SelectionView
  139. {
  140. VerticalOptions = LayoutOptions.Fill,
  141. HorizontalOptions = LayoutOptions.Fill
  142. };
  143. selection.Configure += (sender, args) =>
  144. {
  145. args.Columns
  146. .BeginUpdate()
  147. .Clear()
  148. .Add(new MobileGridIntegerColumn<KanbanShell>() { Column = x=>x.Number, Width = GridLength.Auto, Caption = "#"})
  149. .Add(new MobileGridTextColumn<KanbanShell>() { Column = x=>x.Title, Width = GridLength.Star, Caption = caption, Alignment = TextAlignment.Start})
  150. .EndUpdate();
  151. };
  152. selection.Refresh += (sender, args) => App.Data.Kanbans.Refresh(false);
  153. selection.SelectionChanged += (sender, args) =>
  154. {
  155. selected(args.SelectedItems.FirstOrDefault() as KanbanShell);
  156. DismissPopup();
  157. };
  158. selection.Load();
  159. return selection;
  160. }
  161. private async void Complete_Clicked(object sender, EventArgs e)
  162. {
  163. if (!ViewModel.Item.Completed.IsEmpty())
  164. {
  165. ViewModel.Item.Completed = DateTime.MinValue;
  166. ViewModel.Item.Save("Re-opened on Mobile Device");
  167. Refresh();
  168. return;
  169. }
  170. bool bOpenForms = ViewModel.Forms.Any(x => x.Completed.IsEmpty());
  171. if (bOpenForms)
  172. {
  173. await MaterialDialog.Instance.AlertAsync("Please finish all open forms before completing this assignment", "Open Forms Found");
  174. return;
  175. }
  176. bool bConfirm = await MaterialDialog.Instance.ConfirmAsync(message: "Complete Assignment?",
  177. confirmingText: "Yes",
  178. dismissiveText: "No") == true;
  179. if (bConfirm)
  180. {
  181. ViewModel.Item.Completed = DateTime.Now;
  182. ViewModel.Item.Save("Completed on Mobile Device");
  183. Navigation.PopAsync();
  184. }
  185. }
  186. private void Description_OnTextChanged(object sender, TextChangedEventArgs e)
  187. {
  188. if (Description.IsFocused)
  189. DoChanged("Description");
  190. }
  191. private void Subject_OnTextChanged(object sender, TextChangedEventArgs e)
  192. {
  193. if (Subject.IsFocused)
  194. DoChanged("Subject");
  195. }
  196. private void BookedStart_OnChanged(object sender, TimeButtonChangedArgs args)
  197. {
  198. DoChanged("BookedStart");
  199. if (ViewModel.Item.BookedStart > ViewModel.Item.BookedFinish)
  200. {
  201. ViewModel.Item.BookedFinish = ViewModel.Item.BookedStart;
  202. DoChanged("BookedFinish");
  203. }
  204. }
  205. private void ActualStart_OnChanged(object sender, TimeButtonChangedArgs args)
  206. {
  207. DoChanged("ActualStart");
  208. if (ViewModel.Item.ActualStart > ViewModel.Item.ActualFinish)
  209. {
  210. ViewModel.Item.ActualFinish = ViewModel.Item.ActualStart;
  211. DoChanged("ActualFinish");
  212. }
  213. }
  214. private void BookedFinish_OnChanged(object sender, TimeButtonChangedArgs args)
  215. {
  216. if (ViewModel.Item.BookedStart > ViewModel.Item.BookedFinish)
  217. ViewModel.Item.BookedFinish = ViewModel.Item.BookedStart;
  218. DoChanged("BookedFinish");
  219. }
  220. private void ActualFinish_OnChanged(object sender, TimeButtonChangedArgs args)
  221. {
  222. if (ViewModel.Item.ActualStart > ViewModel.Item.ActualFinish)
  223. ViewModel.Item.ActualFinish = ViewModel.Item.ActualStart;
  224. DoChanged("ActualFinish");
  225. }
  226. }
  227. }