EmployeeQualificationList.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using System;
  4. using System.Linq;
  5. using InABox.Mobile;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. namespace PRS.Mobile
  9. {
  10. public class QualificationExpiryConverter : AbstractConverter<EmployeeQualificationShell, String>
  11. {
  12. protected override string Convert(EmployeeQualificationShell value, object? parameter = null)
  13. {
  14. return value?.Qualified.IsEmpty() == true
  15. ? ""
  16. : value.Expiry.IsEmpty()
  17. ? "Permanent"
  18. : $"Expires {value.Expiry:dd MMM yy}";
  19. }
  20. }
  21. public class QualificationNumberConverter : AbstractConverter<EmployeeQualificationShell, String>
  22. {
  23. protected override string Convert(EmployeeQualificationShell value, object? parameter = null)
  24. {
  25. return String.IsNullOrWhiteSpace(value?.Number)
  26. ? "Not Provided"
  27. : $"ID: {value.Number}";
  28. }
  29. }
  30. public class QualificationColorConverter : AbstractConverter<EmployeeQualificationShell, Color>
  31. {
  32. protected override Color Convert(EmployeeQualificationShell value, object? parameter = null)
  33. {
  34. return value?.Verified.IsEmpty() == true
  35. ? Color.LightYellow
  36. : !value.Expiry.IsEmpty() && value.Expiry <= DateTime.Today
  37. ? Color.LightSalmon
  38. : Color.LightGreen;
  39. }
  40. }
  41. [XamlCompilation(XamlCompilationOptions.Compile)]
  42. public partial class EmployeeQualificationList
  43. {
  44. // private readonly QualificationModel _lookups = new QualificationModel(App.Data,
  45. // LookupFactory.DefineFilter<Qualification>
  46. // );
  47. public EmployeeQualificationList()
  48. {
  49. InitializeComponent();
  50. // _lookups.Refresh(true, () =>
  51. // {
  52. // Dispatcher.BeginInvokeOnMainThread(() =>
  53. // {
  54. // foreach (var lookup in _lookups.Items)
  55. // {
  56. // MobileMenuItem item = new MobileMenuItem
  57. // {
  58. // BindingContext = lookup,
  59. // Text = lookup.Description
  60. // };
  61. // item.Clicked += AddQualification;
  62. // _add.Items.Add(item);
  63. // }
  64. //
  65. // _add.IsVisible = _lookups.Any();
  66. // });
  67. // });
  68. RefreshData(false,true);
  69. }
  70. private void _qualification_Refresh(object sender, MobileListRefreshEventArgs args)
  71. {
  72. RefreshData(true,false);
  73. }
  74. private void RefreshData(bool force, bool async)
  75. {
  76. if (async)
  77. App.Data.EmployeeQualifications.Refresh(force,() => Device.BeginInvokeOnMainThread(RefreshList));
  78. else
  79. {
  80. App.Data.EmployeeQualifications.Refresh(force);
  81. RefreshList();
  82. }
  83. }
  84. private void RefreshList()
  85. {
  86. _qualifications.LastUpdated = App.Data.EmployeeQualifications.LastUpdated;
  87. App.Data.EmployeeQualifications.Search(FilterShell);
  88. _qualifications.ItemsSource = null;
  89. _qualifications.ItemsSource ??= App.Data.EmployeeQualifications.Items.ToList();
  90. }
  91. private bool FilterShell(EmployeeQualificationShell shell)
  92. {
  93. var bOK = String.IsNullOrWhiteSpace(_currentfilter)
  94. || shell.Description.ToUpper().Contains(_currentfilter)
  95. || shell.Number.ToUpper().Contains(_currentfilter);
  96. return bOK;
  97. }
  98. private String _currentfilter = "";
  99. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  100. {
  101. _currentfilter = args.Text?.ToUpper() ?? string.Empty;
  102. RefreshList();
  103. }
  104. #region Add / Edit Qualifications
  105. private EmployeeQualificationShell _newshell;
  106. protected override void OnAppearing()
  107. {
  108. base.OnAppearing();
  109. if ((_newshell != null) && (_newshell.ID != Guid.Empty))
  110. {
  111. App.Data.EmployeeQualifications.CommitItem(_newshell);
  112. RefreshList();
  113. }
  114. _newshell = null;
  115. }
  116. // private void AddQualification(object sender, EventArgs e)
  117. // {
  118. // if ((sender as MobileMenuItem)?.BindingContext is QualificationShell shell)
  119. // {
  120. // _newshell = App.Data.EmployeeQualifications.CreateItem();
  121. // _newshell.QualificationID = shell.ID;
  122. // _newshell.EmployeeID = App.Data.Me.ID;
  123. // _newshell.Description = shell.Description;
  124. // EmployeeQualificationEditPage page = new EmployeeQualificationEditPage() { Item = _newshell };
  125. // Navigation.PushAsync(page);
  126. // }
  127. // }
  128. private void _qualification_Tapped(object sender, EventArgs e)
  129. {
  130. EmployeeQualificationShell shell = (sender as Frame)?.BindingContext as EmployeeQualificationShell;
  131. if (shell == null)
  132. return;
  133. EmployeeQualificationEditPage page = new EmployeeQualificationEditPage() { Item = shell };
  134. Navigation.PushAsync(page);
  135. }
  136. #endregion
  137. private void _add_OnClicked(object sender, MobileMenuButtonClickedEventArgs args)
  138. {
  139. ShowPopup(() => CreateQualificationSelection("Select Qualification", (shell) =>
  140. {
  141. _newshell = App.Data.EmployeeQualifications.CreateItem();
  142. _newshell.QualificationID = shell.ID;
  143. _newshell.EmployeeID = App.Data.Me.ID;
  144. _newshell.Description = shell.Description;
  145. EmployeeQualificationEditPage page = new EmployeeQualificationEditPage() { Item = _newshell };
  146. Navigation.PushAsync(page);
  147. }));
  148. }
  149. private View CreateQualificationSelection(String caption, Action<QualificationShell> selected)
  150. {
  151. QualificationModel _lookups = new QualificationModel(App.Data,
  152. LookupFactory.DefineFilter<Qualification>
  153. );
  154. SelectionView selection = new SelectionView
  155. {
  156. VerticalOptions = LayoutOptions.Fill,
  157. HorizontalOptions = LayoutOptions.Fill,
  158. CanSearch = true
  159. };
  160. selection.Configure += (sender, args) =>
  161. {
  162. args.Columns
  163. .BeginUpdate()
  164. .Clear()
  165. .Add(new MobileGridTextColumn<QualificationShell>() { Column = x=>x.Description, Width = GridLength.Star, Caption = caption, Alignment = TextAlignment.Center})
  166. .EndUpdate();
  167. args.Filters.AddRange(_lookups.AvailableFilters().Select(x=>x.Name));
  168. };
  169. selection.Refresh += (sender, args) => _lookups.Refresh(false);
  170. selection.SelectionChanged += (sender, args) =>
  171. {
  172. selected(args.SelectedItems.FirstOrDefault() as QualificationShell);
  173. DismissPopup();
  174. };
  175. selection.Load();
  176. return selection;
  177. }
  178. }
  179. }