DigitalFormsPicker.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using Comal.Classes;
  2. using comal.timesheets.QAForms;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using comal.timesheets.Data_Classes;
  16. using Xamarin.Forms;
  17. using Xamarin.Forms.Xaml;
  18. using XF.Material.Forms.UI.Dialogs;
  19. namespace comal.timesheets
  20. {
  21. [XamlCompilation(XamlCompilationOptions.Compile)]
  22. public partial class DigitalFormsPicker : ContentPage
  23. {
  24. #region Fields
  25. bool _searching = false;
  26. Kanban addToTaskKanban = new Kanban();
  27. List<DigitalFormLayoutShell> layouts = new List<DigitalFormLayoutShell>();
  28. List<String> types = new List<String>();
  29. private bool firstLoad = true;
  30. private bool incompleteVisible = true;
  31. private bool addingToTask = false;
  32. Guid JobID = Guid.Empty;
  33. List<ExistingFormShell> incompleteForms = new List<ExistingFormShell>();
  34. List<ExistingFormShell> completeForms = new List<ExistingFormShell>();
  35. #endregion
  36. public DigitalFormsPicker(string appliesTo = "Kanban", Guid _jobid = new Guid()) //normal Forms Library - default is kanban type
  37. {
  38. InitializeComponent ();
  39. JobID = _jobid;
  40. NavigationPage.SetHasBackButton(this, false);
  41. LoadScreen(appliesTo);
  42. }
  43. public DigitalFormsPicker(Kanban _kanban) //used for adding forms to a task
  44. {
  45. InitializeComponent();
  46. addingToTask = true;
  47. addToTaskKanban = _kanban;
  48. NavigationPage.SetHasBackButton(this, false);
  49. LoadScreen("Kanban");
  50. }
  51. private void ExitBtn_Clicked(object sender, EventArgs e)
  52. {
  53. Navigation.PopAsync();
  54. }
  55. #region OnAppearing and Loading Screen
  56. protected override void OnAppearing()
  57. {
  58. try
  59. {
  60. _searching = false;
  61. if (RetainedResults.IsFormRetained)
  62. {
  63. DigitalFormHost host = new DigitalFormHost(LoadModel(RetainedResults.LastDigitalFormLayout, CheckType()), JobID);
  64. Navigation.PushAsync(host);
  65. }
  66. LoadExistingForms();
  67. }
  68. catch { }
  69. base.OnAppearing();
  70. }
  71. private async void LoadScreen(string appliesTo)
  72. {
  73. try
  74. {
  75. await Task.Run(() =>
  76. {
  77. types.Add("All");
  78. var forms = new Client<DigitalFormLayout>();
  79. Filter<DigitalFormLayout> filter = new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Secure).IsEqualTo(false)
  80. .And(x => x.Form.Active).IsEqualTo(true);
  81. filter = filter.And(x => x.Form.AppliesTo).IsEqualTo(appliesTo);
  82. Columns<DigitalFormLayout> columns = new Columns<DigitalFormLayout>(
  83. x => x.Description, //0
  84. x => x.ID, //1
  85. x => x.Code, //2
  86. x => x.Form.AppliesTo, //3
  87. x => x.Form.ID, //4
  88. x => x.Layout, //5
  89. x => x.Form.Group.Description //6
  90. );
  91. var table = forms.Query(
  92. filter,
  93. columns
  94. ,
  95. new SortOrder<DigitalFormLayout>(x => x.Description)
  96. );
  97. foreach (CoreRow row in table.Rows)
  98. {
  99. List<object> list = row.Values;
  100. if (list[0] == null) list[0] = ""; //0
  101. if (list[1] == null) list[1] = Guid.Empty; //1
  102. if (list[2] == null) list[2] = ""; //2
  103. if (list[3] == null) list[3] = ""; //3
  104. if (list[4] == null) list[4] = Guid.Empty; //4
  105. if (list[5] == null) list[5] = ""; //5
  106. if (list[6] == null) list[6] = ""; //6
  107. DigitalFormLayoutShell layout = new DigitalFormLayoutShell();
  108. layout.Description = list[0].ToString();
  109. layout.ID = Guid.Parse(list[1].ToString());
  110. layout.Code = list[2].ToString();
  111. layout.AppliesTo = list[3].ToString();
  112. layout.FormID = Guid.Parse(list[4].ToString());
  113. layout.Layout = list[5].ToString();
  114. layout.FormGroupDescription = list[6].ToString();
  115. layouts.Add(layout);
  116. if (!types.Contains(layout.FormGroupDescription) && !string.IsNullOrWhiteSpace(layout.FormGroupDescription))
  117. {
  118. types.Add(layout.FormGroupDescription);
  119. }
  120. }
  121. GetAverages(appliesTo);
  122. Device.BeginInvokeOnMainThread(() =>
  123. {
  124. if (appliesTo == "Kanban")
  125. {
  126. layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals("Factory"));
  127. filterOptionsControl.Options = types;
  128. filterOptionsControl.CreateRadioButtonsAndSetDefault("Factory");
  129. }
  130. else
  131. {
  132. layoutsList.ItemsSource = layouts;
  133. filterOptionsControl.Options = types;
  134. filterOptionsControl.CreateRadioButtonsAndSetDefault(types.First());
  135. }
  136. });
  137. filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
  138. });
  139. firstLoad = false;
  140. }
  141. catch (Exception e)
  142. {
  143. string error = e.Message;
  144. }
  145. }
  146. private void GetAverages(string appliesTo)
  147. {
  148. try
  149. {
  150. Task.Run(() =>
  151. {
  152. foreach (var layout in layouts)
  153. {
  154. TimeSpan span = new TimeSpan();
  155. CoreTable table = new Client<KanbanForm>().Query
  156. (
  157. new Filter<KanbanForm>(x => x.Form.ID).IsEqualTo(layout.FormID).And(x => x.FormOpen).IsNotEqualTo(null),
  158. new Columns<KanbanForm>(x => x.FormOpen)
  159. );
  160. if (table.Rows.Any())
  161. {
  162. foreach (CoreRow row in table.Rows)
  163. {
  164. List<object> list = row.Values;
  165. TimeSpan timespan = TimeSpan.Parse(list[0].ToString());
  166. span = span + timespan;
  167. }
  168. TimeSpan average = span / table.Rows.Count();
  169. layout.AverageTime = "Average time to complete: " + average.Minutes.ToString() + "m " + average.Seconds.ToString() + "s";
  170. layout.AverageTimeRow = 30;
  171. layout.ImageRowSpan = 2;
  172. }
  173. }
  174. Device.BeginInvokeOnMainThread(() =>
  175. {
  176. layoutsList.ItemsSource = null;
  177. if (filterOptionsControl.CurrentOption == "All")
  178. {
  179. layoutsList.ItemsSource = layouts;
  180. }
  181. else
  182. {
  183. layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals(filterOptionsControl.CurrentOption));
  184. }
  185. });
  186. });
  187. }
  188. catch { }
  189. }
  190. private void LoadExistingForms()
  191. {
  192. //Task.Run(() =>
  193. //{
  194. try
  195. {
  196. List<IFormPickerQueryLoader> loaderList = new List<IFormPickerQueryLoader>()
  197. {
  198. new FormPickerQueryLoader<Kanban, KanbanLink, KanbanForm>(),
  199. new FormPickerQueryLoader<Job, JobLink, JobForm>()
  200. };
  201. incompleteForms.Clear();
  202. completeForms.Clear();
  203. foreach (var loader in loaderList)
  204. {
  205. List<ExistingFormShell> incomplete = loader.QueryIncomplete();
  206. foreach (var v in incomplete)
  207. {
  208. incompleteForms.Add(v);
  209. }
  210. List<ExistingFormShell> complete = loader.QueryComplete();
  211. foreach (var v in complete)
  212. {
  213. completeForms.Add(v);
  214. }
  215. }
  216. Device.BeginInvokeOnMainThread(() =>
  217. {
  218. ShowOrHideIncompleteFormsNotifications();
  219. RefreshMyForms();
  220. });
  221. }
  222. catch { }
  223. //});
  224. }
  225. private void ShowOrHideIncompleteFormsNotifications()
  226. {
  227. if (incompleteForms.Count > 0)
  228. {
  229. notificationFrame.IsVisible = true;
  230. notificationColumn.Width = 40;
  231. numberOfIncompleteFormsLbl.Text = incompleteForms.Count.ToString();
  232. }
  233. else
  234. {
  235. notificationFrame.IsVisible = false;
  236. notificationColumn.Width = 0;
  237. }
  238. }
  239. private void RefreshMyForms()
  240. {
  241. incompleteFormsList.ItemsSource = null;
  242. completeFormsList.ItemsSource = null;
  243. incompleteFormsList.ItemsSource = incompleteForms;
  244. completeFormsList.ItemsSource = completeForms;
  245. incompleteBtn.Text = "Incomplete (" + incompleteForms.Count + ")";
  246. completeBtn.Text = "Complete (" + completeForms.Count + ")";
  247. }
  248. #endregion
  249. #region User Interaction
  250. #region New Forms Section
  251. private void NewButton_Clicked(object sender, EventArgs e)
  252. {
  253. templatesColumn.Width = GridLength.Star;
  254. formsColumn.Width = 0;
  255. existingFormsGrid.IsVisible = false;
  256. templatesGrid.IsVisible = true;
  257. newButton.BackgroundColor = Color.FromHex("#15C7C1");
  258. myFormsButton.BackgroundColor = Color.Default;
  259. }
  260. private void MyFormsButton_Clicked(object sender, EventArgs e)
  261. {
  262. templatesColumn.Width = 0;
  263. formsColumn.Width = GridLength.Star;
  264. existingFormsGrid.IsVisible = true;
  265. templatesGrid.IsVisible = false;
  266. newButton.BackgroundColor = Color.Default;
  267. myFormsButton.BackgroundColor = Color.FromHex("#15C7C1");
  268. }
  269. private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
  270. {
  271. if (filterOption == filterOptionsControl.CurrentOption)
  272. return;
  273. filterOptionsControl.CurrentOption = filterOption;
  274. if (filterOption == "All")
  275. {
  276. layoutsList.ItemsSource = layouts;
  277. }
  278. else
  279. {
  280. layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals(filterOption));
  281. }
  282. }
  283. #endregion
  284. #region My Forms Section
  285. private async void IncompleteFormsList_Tapped(object sender, EventArgs e)
  286. {
  287. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  288. {
  289. var form = incompleteFormsList.SelectedItem as ExistingFormShell;
  290. DigitalFormLayout layout = new Client<DigitalFormLayout>().Query(
  291. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(form.FormID)
  292. ).Rows.FirstOrDefault().ToObject<DigitalFormLayout>();
  293. if (form.Type == typeof(JobForm))
  294. JobID = form.ParentID;
  295. else
  296. JobID = Guid.Empty;
  297. DigitalFormHost host = new DigitalFormHost(LoadModel(layout, form.Type, form), JobID);
  298. Navigation.PushAsync(host);
  299. }
  300. }
  301. private async void CompleteFormsList_Tapped(object sender, EventArgs e)
  302. {
  303. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  304. {
  305. var form = completeFormsList.SelectedItem as ExistingFormShell;
  306. DigitalFormLayout layout = new Client<DigitalFormLayout>().Query(
  307. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(form.FormID)
  308. ).Rows.FirstOrDefault().ToObject<DigitalFormLayout>();
  309. if (form.Type == typeof(JobForm))
  310. JobID = form.ParentID;
  311. else
  312. JobID = Guid.Empty;
  313. DigitalFormHost host = new DigitalFormHost(LoadModel(layout, form.Type, form), JobID);
  314. Navigation.PushAsync(host);
  315. }
  316. }
  317. private void Incomplete_Tapped(object sender, EventArgs e)
  318. {
  319. incompleteFormsColumn.Width = GridLength.Star;
  320. completeFormsColumn.Width = 0;
  321. incompleteFormsList.IsVisible = true;
  322. completeFormsList.IsVisible = false;
  323. incompleteBtn.BackgroundColor = Color.FromHex("#15C7C1");
  324. completeBtn.BackgroundColor = Color.Default;
  325. incompleteVisible = true;
  326. searchEnt.Text = "";
  327. }
  328. private void Complete_Tapped(object sender, EventArgs e)
  329. {
  330. completeFormsColumn.Width = GridLength.Star;
  331. incompleteFormsColumn.Width = 0;
  332. completeFormsList.IsVisible = true;
  333. incompleteFormsList.IsVisible = false;
  334. incompleteBtn.BackgroundColor = Color.Default;
  335. completeBtn.BackgroundColor = Color.FromHex("#15C7C1");
  336. incompleteVisible = false;
  337. searchEnt.Text = "";
  338. }
  339. #region Loading From History Section
  340. private void LayoutsList_Tapped(object sender, EventArgs e)
  341. {
  342. if (_searching)
  343. return;
  344. else
  345. LoadHost();
  346. }
  347. private async void LoadHost()
  348. {
  349. try
  350. {
  351. var digitalFormLayoutShell = layoutsList.SelectedItem as DigitalFormLayoutShell;
  352. DigitalFormLayout digitalFormLayout = new DigitalFormLayout();
  353. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  354. {
  355. _searching = true;
  356. digitalFormLayout.ID = digitalFormLayoutShell.ID;
  357. digitalFormLayout.Description = digitalFormLayoutShell.Description;
  358. digitalFormLayout.Code = digitalFormLayoutShell.Code;
  359. digitalFormLayout.Form.AppliesTo = digitalFormLayoutShell.AppliesTo;
  360. digitalFormLayout.Form.ID = digitalFormLayoutShell.FormID;
  361. digitalFormLayout.Layout = digitalFormLayoutShell.Layout;
  362. digitalFormLayout.Form.Group.Description = digitalFormLayoutShell.FormGroupDescription;
  363. RetainedResults.LastDigitalFormLayout = digitalFormLayout;
  364. }
  365. DigitalFormHost host = new DigitalFormHost(LoadModel(digitalFormLayout, CheckType()), JobID);
  366. Navigation.PushAsync(host);
  367. }
  368. catch { }
  369. }
  370. private Type CheckType()
  371. {
  372. if (JobID != Guid.Empty)
  373. return typeof(JobForm);
  374. else
  375. return typeof(KanbanForm);
  376. }
  377. private IDigitalFormHostModel LoadModel(DigitalFormLayout layout, Type type, ExistingFormShell form = null)
  378. {
  379. if (type == typeof(JobForm))
  380. {
  381. var model = new DigitalFormHostModel<Job, JobLink, JobForm>();
  382. var job = new Job();
  383. var jobForm = new JobForm();
  384. jobForm.Form.ID = layout.Form.ID;
  385. if (form == null)
  386. {
  387. job.ID = JobID;
  388. }
  389. else
  390. {
  391. jobForm.ID = form.ID;
  392. job.ID = form.ParentID;
  393. }
  394. model.LoadItems(job, jobForm, layout);
  395. return model;
  396. }
  397. else
  398. {
  399. var model = new DigitalFormHostModel<Kanban, KanbanLink, KanbanForm>();
  400. var kanban = new Kanban();
  401. var kanbanForm = new KanbanForm();
  402. kanbanForm.Form.ID = layout.Form.ID;
  403. if (form != null)
  404. {
  405. kanbanForm.ID = form.ID;
  406. kanban.ID = form.ParentID;
  407. }
  408. if (addingToTask)
  409. {
  410. kanbanForm.Parent.ID = addToTaskKanban.ID;
  411. kanban.ID = addToTaskKanban.ID;
  412. }
  413. model.LoadItems(kanban, kanbanForm, layout);
  414. return model;
  415. }
  416. }
  417. #endregion
  418. #region Searching
  419. private void SearchEnt_Changed(object sender, EventArgs e)
  420. {
  421. if (CheckEmptySearch())
  422. return;
  423. else
  424. {
  425. RunSearch();
  426. }
  427. }
  428. private bool CheckEmptySearch()
  429. {
  430. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  431. {
  432. incompleteFormsList.ItemsSource = incompleteForms;
  433. completeFormsList.ItemsSource = completeForms;
  434. return true;
  435. }
  436. else
  437. return false;
  438. }
  439. private void RunSearch()
  440. {
  441. try
  442. {
  443. if (incompleteVisible)
  444. RunSearchOnIncomplete();
  445. else
  446. RunSearchOnHistory();
  447. }
  448. catch (Exception ex)
  449. {
  450. string message = ex.Message;
  451. }
  452. }
  453. private void RunSearchOnIncomplete()
  454. {
  455. incompleteFormsList.ItemsSource = incompleteForms.Where(x =>
  456. x.Description.Contains(searchEnt.Text) || x.Description.Contains(searchEnt.Text.ToUpper())
  457. || x.Description.Contains(searchEnt.Text.ToLower()) || x.Description.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text))
  458. );
  459. }
  460. private void RunSearchOnHistory()
  461. {
  462. completeFormsList.ItemsSource = completeForms.Where(x =>
  463. x.Description.Contains(searchEnt.Text) || x.Description.Contains(searchEnt.Text.ToUpper())
  464. || x.Description.Contains(searchEnt.Text.ToLower()) || x.Description.Contains(SearchUtils.UpperCaseFirst(searchEnt.Text))
  465. );
  466. }
  467. #endregion
  468. #endregion
  469. #endregion
  470. }
  471. }