QualificationList.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace comal.timesheets
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class QualificationList
  15. {
  16. List<EmployeeQualificationShell> shells = new List<EmployeeQualificationShell>();
  17. public QualificationList()
  18. {
  19. InitializeComponent();
  20. NavigationPage.SetHasBackButton(this, false);
  21. }
  22. protected override void OnAppearing()
  23. {
  24. LoadList();
  25. base.OnAppearing();
  26. }
  27. async void LoadList()
  28. {
  29. try
  30. {
  31. await Task.Run(() =>
  32. {
  33. shells.Clear();
  34. CoreTable table = new Client<EmployeeQualification>().Query
  35. (
  36. new Filter<EmployeeQualification>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID),
  37. new Columns<EmployeeQualification>
  38. (
  39. x => x.ID, //0
  40. x => x.Qualification.Description, //1
  41. x => x.Qualified, //2
  42. x => x.Expiry, //3
  43. x => x.Verified, //4
  44. x => x.FrontPhoto.ID, //5
  45. x => x.BackPhoto.ID, //6
  46. x => x.Qualification.Renewal, //7
  47. x => x.QualificationNumber //8
  48. ),
  49. new SortOrder<EmployeeQualification>(x => x.Qualification.Description)
  50. );
  51. if (table.Rows.Any())
  52. {
  53. foreach (CoreRow row in table.Rows)
  54. {
  55. List<object> list = row.Values;
  56. if (list[0] == null) { list[0] = Guid.Empty; } //0
  57. if (list[1] == null) { list[1] = ""; } //1
  58. if (list[2] == null) { list[2] = DateTime.MinValue; } //2
  59. if (list[3] == null) { list[3] = DateTime.MinValue; } //3
  60. if (list[4] == null) { list[4] = DateTime.MinValue; } //4
  61. if (list[5] == null) { list[5] = Guid.Empty; } //5
  62. if (list[6] == null) { list[6] = Guid.Empty; } //6
  63. if (list[8] == null) { list[8] = ""; } //7
  64. EmployeeQualificationShell shell = new EmployeeQualificationShell
  65. {
  66. ID = Guid.Parse(list[0].ToString()),
  67. Description = list[1].ToString(),
  68. Qualified = DateTime.Parse(list[2].ToString()),
  69. Expiry = DateTime.Parse(list[3].ToString()),
  70. Verified = DateTime.Parse(list[4].ToString()),
  71. FrontPhotoID = Guid.Parse(list[5].ToString()),
  72. BackPhotoID = Guid.Parse(list[6].ToString()),
  73. Renewal = list[7].ToString(),
  74. Number = "Number: " + list[8].ToString(),
  75. };
  76. shell = SetDisplayProperties(shell);
  77. shells.Add(shell);
  78. }
  79. Device.BeginInvokeOnMainThread(() =>
  80. {
  81. qualificationsDisplayList.ItemsSource = null;
  82. qualificationsDisplayList.ItemsSource = shells;
  83. if (!string.IsNullOrWhiteSpace(searchEnt.Text))
  84. {
  85. RunSearch();
  86. }
  87. });
  88. }
  89. });
  90. }
  91. catch { }
  92. }
  93. EmployeeQualificationShell SetDisplayProperties(EmployeeQualificationShell shell)
  94. {
  95. if (shell.Verified == DateTime.MinValue)
  96. {
  97. shell.DisplayVerified = "Unverified";
  98. shell.VerifiedColor = Color.Orange;
  99. }
  100. else
  101. {
  102. shell.DisplayVerified = "Verified: " + shell.Verified.ToString("dd MMM yy");
  103. }
  104. if (shell.Expiry == DateTime.MinValue)
  105. {
  106. shell.DisplayExpiry = "Expiry: ";
  107. }
  108. else
  109. {
  110. shell.DisplayExpiry = "Expiry: " + shell.Expiry.ToString("dd MMM yy");
  111. }
  112. if (shell.Renewal == "Permanent")
  113. {
  114. shell.DisplayExpiry = "Does not expire";
  115. }
  116. if (shell.Qualified == DateTime.MinValue)
  117. {
  118. shell.DisplayQualified = "Qualified: ";
  119. }
  120. else
  121. {
  122. shell.DisplayQualified = "Qualified: " + shell.Qualified.ToString("dd MMM yy");
  123. }
  124. int count = 0;
  125. if (shell.FrontPhotoID != Guid.Empty)
  126. {
  127. count++;
  128. }
  129. if (shell.BackPhotoID != Guid.Empty)
  130. {
  131. count++;
  132. }
  133. shell.DisplayPhotoCount = "Photos: " + count;
  134. if (count == 0)
  135. shell.PhotoCountColor = Color.FromHex("#f08080"); //light coral / red
  136. if (shell.Expiry != DateTime.MinValue)
  137. {
  138. if (shell.Expiry >= DateTime.Today)
  139. {
  140. if (shell.Expiry <= DateTime.Today.AddDays(30))
  141. {
  142. shell.ExpiryColor = Color.Orange;
  143. }
  144. else
  145. {
  146. shell.ExpiryColor = Color.Default;
  147. }
  148. }
  149. else
  150. {
  151. shell.ExpiryColor = Color.FromHex("#f08080"); //light coral / red
  152. }
  153. }
  154. return shell;
  155. }
  156. void Qualification_Tapped(object sender, EventArgs e)
  157. {
  158. EmployeeQualificationShell shell = qualificationsDisplayList.SelectedItem as EmployeeQualificationShell;
  159. AddEditQualification page = new AddEditQualification(shell.ID);
  160. Navigation.PushAsync(page);
  161. }
  162. void ExitBtn_Clicked(object sender, EventArgs e)
  163. {
  164. Navigation.PopAsync();
  165. }
  166. void AddBtn_Clicked(object sender, EventArgs e)
  167. {
  168. AddEditQualification page = new AddEditQualification(Guid.Empty);
  169. Navigation.PushAsync(page);
  170. }
  171. void SearchEnt_Changed(object sender, EventArgs e)
  172. {
  173. RunSearch();
  174. }
  175. void RunSearch()
  176. {
  177. if (string.IsNullOrEmpty(searchEnt.Text))
  178. {
  179. qualificationsDisplayList.ItemsSource = shells;
  180. }
  181. else
  182. {
  183. qualificationsDisplayList.ItemsSource = shells.Where(x =>
  184. x.Description.Contains(searchEnt.Text) || x.Description.Contains(UpperCaseFirst(searchEnt.Text))
  185. || x.Description.Contains(searchEnt.Text.ToUpper()) || x.Description.Contains(searchEnt.Text.ToLower())
  186. );
  187. }
  188. }
  189. static String UpperCaseFirst(string s)
  190. {
  191. char[] a = s.ToCharArray();
  192. a[0] = char.ToUpper(a[0]);
  193. return new string(a);
  194. }
  195. }
  196. public class EmployeeQualificationShell
  197. {
  198. public Guid ID { get; set; }
  199. public String Description { get; set; }
  200. public DateTime Qualified { get; set; }
  201. public string DisplayQualified { get; set; }
  202. public DateTime Expiry { get; set; }
  203. public string DisplayExpiry { get; set; }
  204. public Color ExpiryColor { get; set; }
  205. public DateTime Verified { get; set; }
  206. public Color VerifiedColor { get; set; }
  207. public string DisplayVerified { get; set; }
  208. public string DisplayPhotoCount { get; set; }
  209. public Color PhotoCountColor { get; set; }
  210. public Guid FrontPhotoID { get; set; }
  211. public Guid BackPhotoID { get; set; }
  212. public Guid QualificationID { get; set; }
  213. public String Renewal { get; set; }
  214. public string Number { get; set; }
  215. public EmployeeQualificationShell()
  216. {
  217. ID = Guid.Empty;
  218. Description = "";
  219. Qualified = DateTime.MinValue;
  220. DisplayQualified = "";
  221. Expiry = DateTime.MinValue;
  222. DisplayExpiry = "";
  223. ExpiryColor = Color.Default;
  224. Verified = DateTime.MinValue;
  225. DisplayVerified = "";
  226. DisplayPhotoCount = "";
  227. PhotoCountColor = Color.Default;
  228. FrontPhotoID = Guid.Empty;
  229. BackPhotoID = Guid.Empty;
  230. VerifiedColor = Color.Default;
  231. QualificationID = Guid.Empty;
  232. Renewal = "";
  233. Number = "";
  234. }
  235. }
  236. }