EmployeeFormPicker.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using comal.timesheets.Tasks;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using Xamarin.Forms;
  14. using Xamarin.Forms.Xaml;
  15. using XF.Material.Forms.UI;
  16. using XF.Material.Forms.UI.Dialogs;
  17. namespace comal.timesheets.QAForms
  18. {
  19. [XamlCompilation(XamlCompilationOptions.Compile)]
  20. public partial class EmployeeFormPicker : ContentPage
  21. {
  22. List<EmployeeFormShell> employeeFormShells = new List<EmployeeFormShell>();
  23. Guid empID = Guid.Empty;
  24. List<string> roles = new List<string>();
  25. bool firstLoad = true;
  26. Employee employee = new Employee();
  27. public EmployeeFormPicker()
  28. {
  29. InitializeComponent();
  30. empID = GlobalVariables.EmpID;
  31. filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
  32. roles.Add("All");
  33. LoadEmployee();
  34. NavigationPage.SetHasBackButton(this, false);
  35. }
  36. private void LoadEmployee()
  37. {
  38. Task.Run(() =>
  39. {
  40. employee = new Client<Employee>().Query(new Filter<Employee>(x => x.ID).IsEqualTo(empID)).Rows.FirstOrDefault().ToObject<Employee>();
  41. });
  42. }
  43. private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
  44. {
  45. if (filterOption == filterOptionsControl.CurrentOption)
  46. return;
  47. filterOptionsControl.CurrentOption = filterOption;
  48. if (filterOption == "All")
  49. {
  50. employeeFormDisplayList.ItemsSource = employeeFormShells;
  51. }
  52. else
  53. {
  54. employeeFormDisplayList.ItemsSource = employeeFormShells.Where(x => x.Role.Equals(filterOption));
  55. }
  56. }
  57. void ExitBtn_Clicked(object sender, EventArgs e)
  58. {
  59. Navigation.PopAsync();
  60. }
  61. protected async override void OnAppearing()
  62. {
  63. LoadData();
  64. base.OnAppearing();
  65. }
  66. async void LoadData()
  67. {
  68. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Refreshing"))
  69. {
  70. Title = "Loading";
  71. await Task.Run(() =>
  72. {
  73. employeeFormShells.Clear();
  74. string emptyString = "";
  75. CoreTable table = new Client<EmployeeForm>().Query(
  76. new Filter<EmployeeForm>(x => x.Parent.ID).IsEqualTo(empID),
  77. new Columns<EmployeeForm>(
  78. x => x.ID, //0
  79. x => x.Form.Description, //1
  80. x => x.FormCompleted, //2
  81. x => x.FormData, //3
  82. x => x.Created, //4
  83. x => x.Form.ID //5
  84. ),
  85. new SortOrder<EmployeeForm>(x => x.Created, SortDirection.Descending)
  86. );
  87. foreach (CoreRow row in table.Rows)
  88. {
  89. List<object> list = row.Values;
  90. if (list[0] == null) { list[0] = Guid.Empty; } //0
  91. if (list[1] == null) { list[1] = emptyString; } //1
  92. if (list[2] == null) { list[2] = DateTime.MinValue; } //2
  93. if (list[3] == null) { list[3] = emptyString; } //3
  94. if (list[4] == null) { list[4] = DateTime.MinValue; } //4
  95. if (list[5] == null) { list[5] = Guid.Empty; } //5
  96. EmployeeFormShell shell = new EmployeeFormShell();
  97. shell.ID = Guid.Parse(list[0].ToString());
  98. shell.Description = list[1].ToString();
  99. shell.Completed = DateTime.Parse(list[2].ToString());
  100. shell.Created = "Created: " + DateTime.Parse(list[4].ToString()).ToString("dd MMM yy");
  101. if (shell.Completed != DateTime.MinValue)
  102. {
  103. shell.Color = Color.FromHex("#15C7C1"); //light blue
  104. shell.Status = "Status: Completed " + shell.Completed.ToString("dd-MMMM-yy");
  105. }
  106. else if (!string.IsNullOrWhiteSpace(list[3].ToString()))
  107. {
  108. shell.Color = Color.Orange;
  109. shell.Status = "Status: In Progress";
  110. }
  111. else
  112. {
  113. shell.Color = Color.FromHex("#f08080"); //light coral / red
  114. shell.Status = "Status: Not started";
  115. }
  116. CoreTable table2 = new Client<RoleForm>().Query(new Filter<RoleForm>(x => x.Form.ID).IsEqualTo(Guid.Parse(list[5].ToString())),
  117. new Columns<RoleForm>(x => x.Role.Name));
  118. if (table2.Rows.Any())
  119. {
  120. List<object> list2 = table2.Rows.FirstOrDefault().Values;
  121. if (list2[0] == null) { list2[0] = ""; } //0
  122. string role = list2[0].ToString();
  123. shell.Role = role;
  124. if (!roles.Contains(role))
  125. {
  126. roles.Add(role);
  127. }
  128. }
  129. employeeFormShells.Add(shell);
  130. }
  131. Device.BeginInvokeOnMainThread(() =>
  132. {
  133. if (firstLoad)
  134. {
  135. filterOptionsControl.Options = roles;
  136. filterOptionsControl.CreateRadioButtonsAndSetDefault(roles.First());
  137. firstLoad = false;
  138. }
  139. employeeFormDisplayList.ItemsSource = null;
  140. employeeFormDisplayList.ItemsSource = employeeFormShells;
  141. Title = "My Employee Forms";
  142. });
  143. });
  144. }
  145. }
  146. async void LayoutsList_Tapped(object sender, EventArgs e)
  147. {
  148. try
  149. {
  150. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  151. {
  152. bool readOnly = false;
  153. EmployeeForm empForm = new EmployeeForm();
  154. EmployeeFormShell shell = employeeFormDisplayList.SelectedItem as EmployeeFormShell;
  155. CoreTable table0 = new Client<EmployeeForm>().Query(
  156. new Filter<EmployeeForm>(x => x.ID).IsEqualTo(shell.ID),
  157. new Columns<EmployeeForm>(
  158. x => x.ID, //0
  159. x => x.Form.Description, //1
  160. x => x.Form.AppliesTo, //2
  161. x => x.FormCompleted, //3
  162. x => x.FormCompletedBy.ID, //4
  163. x => x.FormData, //5
  164. x => x.Form.ID, //6
  165. x => x.FormStarted, //7
  166. x => x.FormOpen, //8
  167. x => x.Parent.ID,
  168. x => x.BlobData
  169. )
  170. );
  171. if (table0.Rows.Any())
  172. {
  173. empForm = table0.Rows.First().ToObject<EmployeeForm>();
  174. if (empForm.FormCompleted != DateTime.MinValue)
  175. {
  176. readOnly = true;
  177. }
  178. DigitalFormLayout layout = new DigitalFormLayout();
  179. CoreTable table = new Client<DigitalFormLayout>().Query(
  180. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(empForm.Form.ID)
  181. );
  182. if (table.Rows.Any())
  183. {
  184. layout = table.Rows.FirstOrDefault().ToObject<DigitalFormLayout>();
  185. }
  186. var model = new DigitalFormHostModel<Employee, EmployeeLink, EmployeeForm>();
  187. model.LoadItems(employee, empForm, layout);
  188. DigitalFormHost host = new DigitalFormHost(model);
  189. Navigation.PushAsync(host);
  190. }
  191. }
  192. }
  193. catch { }
  194. }
  195. }
  196. public class EmployeeFormShell
  197. {
  198. public Guid ID { get; set; }
  199. public string Description { get; set; }
  200. public Color Color { get; set; }
  201. public DateTime Completed { get; set; }
  202. public string Status { get; set; }
  203. public string Created { get; set; }
  204. public string Role { get; set; }
  205. public EmployeeFormShell()
  206. {
  207. ID = Guid.Empty;
  208. Description = "";
  209. Color = Color.Default;
  210. Completed = DateTime.MinValue;
  211. Status = "";
  212. Created = "";
  213. Role = "";
  214. }
  215. }
  216. }