QAFormViewer.cs 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Threading;
  5. using System.Linq;
  6. using Xamarin.Forms;
  7. using XF.Material.Forms.UI.Dialogs;
  8. using InABox.Core;
  9. using InABox.Clients;
  10. using System.IO;
  11. using InABox.Mobile;
  12. using Syncfusion.XForms.SignaturePad;
  13. using comal.timesheets.Tasks;
  14. using Comal.Classes;
  15. using PRSClasses;
  16. using Newtonsoft.Json;
  17. namespace comal.timesheets.QAForms
  18. {
  19. public class QAFormViewer : Grid, IDFRenderer
  20. {
  21. #region Fields
  22. public Dictionary<DFLayoutField, View> pairs = new Dictionary<DFLayoutField, View>();
  23. public Dictionary<String, String> results = new Dictionary<String, String>();
  24. public Dictionary<String, String> propertyResults = new Dictionary<String, String>();
  25. Dictionary<String, String> loadData = new Dictionary<String, String>();
  26. public Dictionary<DFLayoutLookupField, Dictionary<Guid, string>> dfLayoutLookupFieldLookupOptions = new Dictionary<DFLayoutLookupField, Dictionary<Guid, string>>();
  27. DFLayout dfLayout = null;
  28. public IDigitalFormDataModel model = null;
  29. const string nullOrInvalidType = "Null or Invalid Type";
  30. int rbGroup;
  31. private Guid userID;
  32. bool isRequired = false;
  33. bool readOnly = false;
  34. bool isSecure = false;
  35. bool loadRetainedForm = false;
  36. List<string> useSavedSignatures = new List<string>();
  37. public List<string> errors = new List<string>();
  38. public bool isRequiredEmpty { get; set; }
  39. public string isRequiredMessage { get; set; }
  40. InABox.Core.Location location = new InABox.Core.Location();
  41. Guid digitalFormID = Guid.Empty;
  42. Color isRequiredColor = Color.DarkOrange;
  43. public string FormData = "";
  44. public DateTime FormCompleted = DateTime.MinValue;
  45. public Guid JobID = Guid.Empty;
  46. DateTime timeStarted = DateTime.MinValue;
  47. List<DigitalFormsHeader> headersToCollapse = new List<DigitalFormsHeader>();
  48. int collapsedHeaderCount = 1;
  49. #endregion
  50. #region Constructor
  51. public QAFormViewer(IDigitalFormDataModel _model, DFLayout _dfLayout, bool newForm, bool _readOnly, Guid _jobid = default(Guid))
  52. {
  53. GetLocation();
  54. timeStarted = DateTime.Now;
  55. userID = ClientFactory.UserGuid;
  56. model = _model;
  57. dfLayout = _dfLayout;
  58. dfLayout.Renderer = this;
  59. readOnly = _readOnly;
  60. isRequiredEmpty = false;
  61. loadRetainedForm = false;
  62. isRequiredMessage = "";
  63. JobID = _jobid;
  64. if (RetainedResults.IsFormRetained)
  65. {
  66. loadRetainedForm = true;
  67. }
  68. if (newForm)
  69. {
  70. LoadForm();
  71. }
  72. else if (!newForm)
  73. {
  74. loadData.Clear();
  75. Dictionary<String, Object> data = DigitalForm.DeserializeFormData(model.Instance);
  76. if (data != null)
  77. {
  78. foreach (KeyValuePair<string, object> keyValuePair in data)
  79. {
  80. loadData.Add(keyValuePair.Key, keyValuePair.Value.ToString());
  81. }
  82. }
  83. LoadForm();
  84. }
  85. }
  86. #endregion
  87. #region Location
  88. private async void GetLocation()
  89. {
  90. await Task.Run(() =>
  91. {
  92. LocationServices locationServices = new LocationServices();
  93. locationServices.OnLocationFound += LocationFound;
  94. locationServices.OnLocationError += LocationError;
  95. locationServices.GetLocation();
  96. });
  97. }
  98. private async void LocationFound(LocationServices sender)
  99. {
  100. location.Latitude = sender.Latitude;
  101. location.Longitude = sender.Longitude;
  102. }
  103. private async void LocationError(LocationServices sender, Exception error)
  104. {
  105. errors.Add("Location error: " + error.Message);
  106. }
  107. #endregion Location
  108. #region Load Form Layout
  109. private async void LoadForm()
  110. {
  111. {
  112. LoadRowsAndColumns();
  113. LoadElements();
  114. }
  115. }
  116. private void LoadRowsAndColumns()
  117. {
  118. LoadRows();
  119. LoadColumns();
  120. }
  121. private void LoadRows()
  122. {
  123. foreach (var row in dfLayout.RowHeights)
  124. {
  125. RowDefinition def = new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) };
  126. if (int.TryParse(row, out int rowHeight))
  127. {
  128. def = new RowDefinition { Height = new GridLength(rowHeight, GridUnitType.Absolute) };
  129. if (def.Height.Value < 60)
  130. {
  131. def.SetValue(RowDefinition.HeightProperty, 60);
  132. }
  133. }
  134. else if (row == "*")
  135. {
  136. def = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
  137. }
  138. Device.BeginInvokeOnMainThread(() => { RowDefinitions.Add(def); });
  139. }
  140. }
  141. private void LoadColumns()
  142. {
  143. foreach (var col in dfLayout.ColumnWidths)
  144. {
  145. ColumnDefinition def = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) };
  146. if (int.TryParse(col, out int colWidth))
  147. def = new ColumnDefinition { Width = new GridLength(colWidth, GridUnitType.Absolute) };
  148. else if (col == "*")
  149. {
  150. def = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
  151. }
  152. Device.BeginInvokeOnMainThread(() => { ColumnDefinitions.Add(def); });
  153. }
  154. }
  155. private void LoadElements()
  156. {
  157. rbGroup = 0; //used for customboolean - has to be outside of foreach loop
  158. foreach (DFLayoutControl element in dfLayout.Elements)
  159. {
  160. if (!string.IsNullOrEmpty(element.Description))
  161. {
  162. LoadViewAccordingToElement(element);
  163. }
  164. }
  165. Task.Run(() =>
  166. {
  167. Thread.Sleep(1000);
  168. foreach (var header in headersToCollapse)
  169. {
  170. Device.BeginInvokeOnMainThread(() =>
  171. {
  172. AdjustHeaderSection(header, false);
  173. header.Collapse();
  174. });
  175. }
  176. });
  177. }
  178. #endregion
  179. #region Load Element Methods
  180. private void LoadViewAccordingToElement(DFLayoutControl element)
  181. {
  182. isSecure = false;
  183. if (element is DFLayoutBooleanField)
  184. {
  185. var tuple = LoadCustomBoolean(element);
  186. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  187. }
  188. else if (element is DFLayoutLabel)
  189. {
  190. var tuple = LoadLabel(element);
  191. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  192. }
  193. else if (element is DFLayoutStringField || element is DFLayoutIntegerField || element is DFLayoutDoubleField)
  194. {
  195. var tuple = LoadEditor(element);
  196. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  197. }
  198. else if (element is DFLayoutDateField)
  199. {
  200. var tuple = LoadDatePicker(element);
  201. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  202. }
  203. else if (element is DFLayoutTimeField)
  204. {
  205. var tuple = LoadTimePicker(element);
  206. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  207. }
  208. else if (element is DFLayoutOptionField)
  209. {
  210. var tuple = LoadOptionPicker(element);
  211. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  212. }
  213. else if (element is DFLayoutImage)
  214. {
  215. var tuple = LoadImage(element);
  216. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  217. }
  218. else if (element is DFLayoutLookupField)
  219. {
  220. var tuple = LoadLookupPicker(element);
  221. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  222. }
  223. else if (element is DFLayoutSignaturePad)
  224. {
  225. var tuple = LoadSignaturePad(element);
  226. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  227. }
  228. else if (element is DFLayoutEmbeddedImage)
  229. {
  230. var tuple = LoadEmbeddedImage(element);
  231. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  232. }
  233. else if (element is DFLayoutMultiImage)
  234. {
  235. var tuple = LoadMultiImage(element);
  236. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  237. }
  238. else if (element is DFLayoutMultiSignaturePad)
  239. {
  240. var tuple = LoadMultiSignaturePad(element);
  241. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  242. }
  243. else if (element is DFLayoutAddTaskField)
  244. {
  245. var tuple = LoadAddTaskField(element);
  246. FormatAndAddView(tuple.Item1, element, tuple.Item2);
  247. }
  248. else if (element is DFLayoutHeader)
  249. {
  250. FormatAndAddView(LoadHeader(element), element, false);
  251. }
  252. }
  253. private View LoadHeader(DFLayoutControl element)
  254. {
  255. DFLayoutHeader dFLayoutHeader = (DFLayoutHeader)element;
  256. DigitalFormsHeader header = new DigitalFormsHeader(dFLayoutHeader.Collapsed);
  257. if (dFLayoutHeader.Collapsed)
  258. header = AddNumberToHeader(header);
  259. header.SetHeaderValue(dFLayoutHeader.Header);
  260. header.OnTapped += (bCollapsed) =>
  261. {
  262. AdjustHeaderSection(header, bCollapsed);
  263. };
  264. return header;
  265. }
  266. private DigitalFormsHeader AddNumberToHeader(DigitalFormsHeader header)
  267. {
  268. header.Number = collapsedHeaderCount;
  269. collapsedHeaderCount++;
  270. return header;
  271. }
  272. private void AdjustHeaderSection(DigitalFormsHeader header, bool collapsed)
  273. {
  274. try
  275. {
  276. var thisHeaderRow = GetRow(header);
  277. var headerRows = GetOtherHeaders(thisHeaderRow);
  278. if (headerRows.Any())
  279. AdjustHeightsToNextHeader(headerRows, thisHeaderRow, collapsed);
  280. else
  281. AdjustHeightsBelowHeader(headerRows, thisHeaderRow, collapsed);
  282. }
  283. catch
  284. {
  285. }
  286. }
  287. private List<int> GetOtherHeaders(int thisHeaderRow)
  288. {
  289. List<int> headerRows = new List<int>();
  290. foreach (var child in Children)
  291. {
  292. if (child is DigitalFormsHeader)
  293. {
  294. var dfheader = (DigitalFormsHeader)child;
  295. if (GetRow(dfheader) > thisHeaderRow)
  296. {
  297. headerRows.Add(GetRow(dfheader));
  298. }
  299. }
  300. }
  301. return headerRows;
  302. }
  303. private void AdjustHeightsToNextHeader(List<int> headerRows, int thisHeaderRow, bool collapsed)
  304. {
  305. headerRows.Sort();
  306. int nextHeaderRow = headerRows[0];
  307. for (int i = thisHeaderRow + 1; i < nextHeaderRow; i++)
  308. {
  309. AdjustHeight(i, collapsed);
  310. }
  311. }
  312. private void AdjustHeightsBelowHeader(List<int> headerRows, int thisHeaderRow, bool collapsed)
  313. {
  314. for (int i = thisHeaderRow + 1; i < RowDefinitions.Count; i++)
  315. {
  316. AdjustHeight(i, collapsed);
  317. }
  318. }
  319. private void AdjustHeight(int i, bool collapsed)
  320. {
  321. var rowdef = RowDefinitions[i];
  322. if (collapsed)
  323. {
  324. rowdef.Height = 60;
  325. AnimateChildren(i, 0, 250);
  326. }
  327. else
  328. {
  329. AnimateChildren(i, -60, 500);
  330. rowdef.Height = 0;
  331. }
  332. }
  333. private async void AnimateChildren(int i, int movement, int length)
  334. {
  335. foreach (var child in Children)
  336. {
  337. if (GetRow(child) == i)
  338. {
  339. await child.TranslateTo(0, movement, 250);
  340. }
  341. }
  342. }
  343. private Tuple<View, Boolean> LoadCustomBoolean(DFLayoutControl element)
  344. {
  345. string value = "";
  346. rbGroup++;
  347. CustomBoolean item = new CustomBoolean(rbGroup);
  348. DFLayoutBooleanField dfLayoutBooleanField = element as DFLayoutBooleanField;
  349. if (!string.IsNullOrWhiteSpace(dfLayoutBooleanField.Properties.TrueValue))
  350. item.trueBtn.Content = dfLayoutBooleanField.Properties.TrueValue;
  351. if (!string.IsNullOrWhiteSpace(dfLayoutBooleanField.Properties.FalseValue))
  352. item.falseBtn.Content = dfLayoutBooleanField.Properties.FalseValue;
  353. if (loadData.TryGetValue(dfLayoutBooleanField.Name, out value))
  354. {
  355. item.Value = bool.Parse(value);
  356. if (bool.Parse(value) == false)
  357. item.SetFalseValue();
  358. }
  359. if (loadRetainedForm)
  360. {
  361. if (RetainedResults.Results.TryGetValue(dfLayoutBooleanField.Name, out value))
  362. {
  363. item.Value = bool.Parse(value);
  364. }
  365. }
  366. if (dfLayoutBooleanField.Properties.Required)
  367. {
  368. item.trueBtn.BackgroundColor = isRequiredColor;
  369. item.falseBtn.BackgroundColor = isRequiredColor;
  370. }
  371. item.OnCustomBooleanValueChanged += (boolvalue) =>
  372. {
  373. dfLayout.ChangeField(dfLayoutBooleanField.Name);
  374. };
  375. return new Tuple<View, Boolean>(item, dfLayoutBooleanField.Properties.Required);
  376. }
  377. private Tuple<View, Boolean> LoadLabel(DFLayoutControl element)
  378. {
  379. DFLayoutLabel label = element as DFLayoutLabel;
  380. Label item = new Label();
  381. item.Text = label.Caption;
  382. item.FontSize = Device.GetNamedSize(NamedSize.Medium, item);
  383. item.TextColor = Color.Black;
  384. item.LineBreakMode = LineBreakMode.WordWrap;
  385. item.VerticalOptions = LayoutOptions.FillAndExpand;
  386. item.VerticalTextAlignment = TextAlignment.Center;
  387. item.MinimumHeightRequest = 60;
  388. if (ColumnDefinitions.Count == 1)
  389. {
  390. item.HorizontalOptions = LayoutOptions.CenterAndExpand;
  391. item.HorizontalTextAlignment = TextAlignment.Center;
  392. }
  393. return new Tuple<View, Boolean>(item, false);
  394. }
  395. private Tuple<View, Boolean> LoadEditor(DFLayoutControl element)
  396. {
  397. string value = "";
  398. View view = null;
  399. Editor item = new Editor();
  400. item.AutoSize = EditorAutoSizeOption.TextChanges;
  401. var isrequired = false;
  402. var issecure = false;
  403. if (element is DFLayoutStringField)
  404. {
  405. DFLayoutStringField dfLayoutStringField = element as DFLayoutStringField;
  406. item.TextColor = Color.Black;
  407. item.Placeholder = "Enter answer";
  408. isrequired = dfLayoutStringField.Properties.Required;
  409. issecure = dfLayoutStringField.Properties.Secure;
  410. if (loadData.TryGetValue(dfLayoutStringField.Name, out value))
  411. {
  412. item.Text = value;
  413. }
  414. if (loadRetainedForm)
  415. {
  416. if (RetainedResults.Results.TryGetValue(dfLayoutStringField.Name, out value))
  417. {
  418. item.Text = value;
  419. }
  420. }
  421. item.TextChanged += (object sender, TextChangedEventArgs e) =>
  422. {
  423. dfLayout.ChangeField(dfLayoutStringField.Name);
  424. };
  425. view = item;
  426. }
  427. else if (element is DFLayoutIntegerField)
  428. {
  429. DFLayoutIntegerField dfLayoutIntegerField = element as DFLayoutIntegerField;
  430. item.TextColor = Color.Black;
  431. item.Placeholder = "Enter number";
  432. item.Keyboard = Keyboard.Numeric;
  433. isrequired = dfLayoutIntegerField.Properties.Required;
  434. issecure = dfLayoutIntegerField.Properties.Secure;
  435. if (loadData.TryGetValue(dfLayoutIntegerField.Name, out value))
  436. {
  437. item.Text = value;
  438. }
  439. if (loadRetainedForm)
  440. {
  441. if (RetainedResults.Results.TryGetValue(dfLayoutIntegerField.Name, out value))
  442. {
  443. item.Text = value;
  444. }
  445. }
  446. item.TextChanged += (object sender, TextChangedEventArgs e) =>
  447. {
  448. dfLayout.ChangeField(dfLayoutIntegerField.Name);
  449. };
  450. view = item;
  451. }
  452. else if (element is DFLayoutDoubleField)
  453. {
  454. DFLayoutDoubleField dfLayoutDoubleField = element as DFLayoutDoubleField;
  455. item.TextColor = Color.Black;
  456. item.Placeholder = "Enter number";
  457. item.Keyboard = Keyboard.Numeric;
  458. isrequired = dfLayoutDoubleField.Properties.Required;
  459. issecure = dfLayoutDoubleField.Properties.Secure;
  460. if (loadData.TryGetValue(dfLayoutDoubleField.Name, out value))
  461. {
  462. item.Text = value;
  463. }
  464. if (loadRetainedForm)
  465. {
  466. if (RetainedResults.Results.TryGetValue(dfLayoutDoubleField.Name, out value))
  467. {
  468. item.Text = value;
  469. }
  470. }
  471. item.TextChanged += (object sender, TextChangedEventArgs e) =>
  472. {
  473. dfLayout.ChangeField(dfLayoutDoubleField.Name);
  474. };
  475. view = item;
  476. }
  477. if (isrequired) view.BackgroundColor = isRequiredColor;
  478. if (readOnly || issecure)
  479. {
  480. Label label = new Label();
  481. label.Text = item.Text;
  482. label.FontSize = Device.GetNamedSize(NamedSize.Medium, item);
  483. label.TextColor = Color.DarkGray;
  484. label.LineBreakMode = LineBreakMode.WordWrap;
  485. label.MaxLines = 5;
  486. label.VerticalTextAlignment = TextAlignment.Center;
  487. label.HorizontalTextAlignment = TextAlignment.Center;
  488. label.MinimumHeightRequest = 60;
  489. label.VerticalOptions = LayoutOptions.FillAndExpand;
  490. label.Margin = 9;
  491. view = label;
  492. }
  493. return new Tuple<View, Boolean>(view, isrequired);
  494. }
  495. private Tuple<View, Boolean> LoadDatePicker(DFLayoutControl element)
  496. {
  497. string value = "";
  498. DFLayoutDateField dfLayoutDateField = element as DFLayoutDateField;
  499. DatePicker item = new DatePicker();
  500. item.Format = "dd MMM yyyy";
  501. if (dfLayoutDateField.Properties.Required)
  502. item.BackgroundColor = isRequiredColor;
  503. item.HorizontalOptions = LayoutOptions.CenterAndExpand;
  504. if (loadData.TryGetValue(dfLayoutDateField.Name, out value))
  505. {
  506. item.Date = DateTime.Parse(value);
  507. }
  508. if (loadRetainedForm)
  509. {
  510. if (RetainedResults.Results.TryGetValue(dfLayoutDateField.Name, out value))
  511. {
  512. item.Date = DateTime.Parse(value);
  513. }
  514. }
  515. item.DateSelected += (object sender, DateChangedEventArgs e) =>
  516. {
  517. dfLayout.ChangeField(dfLayoutDateField.Name);
  518. };
  519. return new Tuple<View, Boolean>(item, dfLayoutDateField.Properties.Required);
  520. }
  521. private Tuple<View, Boolean> LoadTimePicker(DFLayoutControl element)
  522. {
  523. string value = "";
  524. DFLayoutTimeField dfLayoutTimeField = element as DFLayoutTimeField;
  525. TimePicker item = new TimePicker();
  526. if (dfLayoutTimeField.Properties.Required)
  527. item.BackgroundColor = isRequiredColor;
  528. item.HorizontalOptions = LayoutOptions.Center;
  529. if (loadData.TryGetValue(dfLayoutTimeField.Name, out value))
  530. {
  531. item.Time = DateTime.Parse(value).TimeOfDay;
  532. }
  533. if (loadRetainedForm)
  534. {
  535. if (RetainedResults.Results.TryGetValue(dfLayoutTimeField.Name, out value))
  536. {
  537. item.Time = DateTime.Parse(value).TimeOfDay;
  538. }
  539. }
  540. item.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) =>
  541. {
  542. if (e.PropertyName == "Time")
  543. dfLayout.ChangeField(dfLayoutTimeField.Name);
  544. };
  545. return new Tuple<View, Boolean>(item, dfLayoutTimeField.Properties.Required);
  546. }
  547. private Tuple<View, Boolean> LoadOptionPicker(DFLayoutControl element)
  548. {
  549. View view = null;
  550. try
  551. {
  552. string value = "";
  553. var isrequired = false;
  554. DFLayoutOptionField dfLayoutOptionField = element as DFLayoutOptionField;
  555. isrequired = dfLayoutOptionField.Properties.Required;
  556. if (GlobalVariables.DeviceString == "i" || GlobalVariables.DeviceString == "I")
  557. {
  558. CustomPickeriOS item = new CustomPickeriOS();
  559. string s = dfLayoutOptionField.Properties.Options;
  560. string[] substrings = s.Split(',');
  561. var optionList = substrings.ToList();
  562. item.AddItems(optionList);
  563. if (!string.IsNullOrEmpty(dfLayoutOptionField.Properties.Default))
  564. {
  565. int index = optionList.IndexOf(dfLayoutOptionField.Properties.Default);
  566. item.SelectedIndex = index;
  567. item.SelectedItem = optionList[index];
  568. item.SetDefault(optionList[index]);
  569. }
  570. if (loadData.TryGetValue(dfLayoutOptionField.Name, out value))
  571. {
  572. int index = optionList.IndexOf(value);
  573. item.SelectedIndex = index;
  574. item.SelectedItem = optionList[index];
  575. item.SetDefault(optionList[index]);
  576. }
  577. if (loadRetainedForm)
  578. {
  579. if (RetainedResults.Results.TryGetValue(dfLayoutOptionField.Name, out value))
  580. {
  581. int index = optionList.IndexOf(value);
  582. item.SelectedIndex = index;
  583. item.SelectedItem = optionList[index];
  584. item.SetDefault(optionList[index]);
  585. }
  586. }
  587. if (isrequired)
  588. {
  589. item.BackgroundColor = isRequiredColor;
  590. item.SetBackGroundColor(isRequiredColor);
  591. }
  592. item.CustomPickeriOSValueChanged += () =>
  593. {
  594. dfLayout.ChangeField(dfLayoutOptionField.Name);
  595. };
  596. view = item;
  597. }
  598. else
  599. {
  600. Picker picker = new Picker();
  601. picker.Title = "Select an option";
  602. picker.VerticalTextAlignment = TextAlignment.Center;
  603. picker.HorizontalTextAlignment = TextAlignment.Center;
  604. string s = dfLayoutOptionField.Properties.Options;
  605. string[] substrings = s.Split(',');
  606. var optionList = substrings.ToList();
  607. picker.ItemsSource = optionList;
  608. if (!string.IsNullOrEmpty(dfLayoutOptionField.Properties.Default))
  609. {
  610. int index = optionList.IndexOf(dfLayoutOptionField.Properties.Default);
  611. picker.SelectedIndex = index;
  612. picker.SelectedItem = optionList[index];
  613. }
  614. if (loadData.TryGetValue(dfLayoutOptionField.Name, out value))
  615. {
  616. int index = optionList.IndexOf(value);
  617. picker.SelectedIndex = index;
  618. picker.SelectedItem = optionList[index];
  619. }
  620. if (loadRetainedForm)
  621. {
  622. if (RetainedResults.Results.TryGetValue(dfLayoutOptionField.Name, out value))
  623. {
  624. int index = optionList.IndexOf(value);
  625. picker.SelectedIndex = index;
  626. picker.SelectedItem = optionList[index];
  627. }
  628. }
  629. if (isrequired)
  630. {
  631. picker.BackgroundColor = isRequiredColor;
  632. }
  633. picker.SelectedIndexChanged += (object sender, EventArgs e) =>
  634. {
  635. dfLayout.ChangeField(dfLayoutOptionField.Name);
  636. };
  637. view = picker;
  638. }
  639. return new Tuple<View, Boolean>(view, isrequired);
  640. }
  641. catch
  642. {
  643. return new Tuple<View, Boolean>(view, false);
  644. }
  645. }
  646. private Tuple<View, Boolean> LoadImage(DFLayoutControl element)
  647. {
  648. View view = null;
  649. DFLayoutImage dfLayoutImage = element as DFLayoutImage;
  650. Image img = new Image();
  651. CoreTable table = new Client<Document>().Query(
  652. new Filter<Document>(x => x.ID).IsEqualTo(dfLayoutImage.Image.ID),
  653. null,
  654. null);
  655. if (table.Rows.Any())
  656. {
  657. CoreRow row = table.Rows.FirstOrDefault();
  658. byte[] data = row.Get<Document, byte[]>(x => x.Data);
  659. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  660. img.MinimumHeightRequest = 150;
  661. img.MinimumWidthRequest = 150;
  662. img.Aspect = Aspect.AspectFit;
  663. img.Source = src;
  664. img.HorizontalOptions = LayoutOptions.FillAndExpand;
  665. img.VerticalOptions = LayoutOptions.FillAndExpand;
  666. img.GestureRecognizers.Add(new TapGestureRecognizer
  667. {
  668. Command = new Command(OnTap),
  669. CommandParameter = src,
  670. NumberOfTapsRequired = 1
  671. });
  672. if (img != null)
  673. {
  674. view = img;
  675. }
  676. }
  677. return new Tuple<View, Boolean>(view, false);
  678. }
  679. private void OnTap(object obj)
  680. {
  681. ImageViewer viewer = new ImageViewer(obj as ImageSource);
  682. Navigation.PushAsync(viewer);
  683. }
  684. private Tuple<View, Boolean> LoadLookupPicker(DFLayoutControl element)
  685. {
  686. View view = null;
  687. string value = "";
  688. DFLayoutLookupField dfLayoutLookupField = element as DFLayoutLookupField;
  689. var isrequired = dfLayoutLookupField.Properties.Required;
  690. Dictionary<Guid, string> lookupFieldOptions = new Dictionary<Guid, string>();
  691. string type = dfLayoutLookupField.Properties.LookupType;
  692. Type lookuptype = CoreUtils.GetEntityOrNull(type);
  693. if (lookuptype == typeof(DrawingTemplate))
  694. view = LoadDrawingTemplateLookupButton(lookuptype, dfLayoutLookupField, lookupFieldOptions, element);
  695. else if (!string.IsNullOrWhiteSpace(dfLayoutLookupField.Properties.Filter))
  696. view = LoadGenericLookupButton(lookuptype, dfLayoutLookupField, lookupFieldOptions, element);
  697. else if (lookuptype == typeof(Product))
  698. view = LoadProductLookupPicker(view, dfLayoutLookupField, value);
  699. else if (lookuptype != null)
  700. view = LoadGenericLookupButton(lookuptype, dfLayoutLookupField, lookupFieldOptions, element);
  701. else if (lookuptype == null)
  702. view = new Label { Text = "Null lookup selected", HorizontalOptions = LayoutOptions.Center, HorizontalTextAlignment = TextAlignment.Center };
  703. return new Tuple<View, Boolean>(view, isrequired);
  704. }
  705. private View LoadGenericLookupButton(Type lookuptype, DFLayoutLookupField dfLayoutLookupField, Dictionary<Guid, string> lookupFieldOptions, DFLayoutControl element)
  706. {
  707. string value = "";
  708. CoreTable table = new CoreTable();
  709. var client = ClientFactory.CreateClient(lookuptype);
  710. var columns = LookupFactory.DefineColumns(lookuptype);
  711. var sort = LookupFactory.DefineSort(lookuptype);
  712. var filter = LookupFactory.DefineFilter(lookuptype);
  713. foreach (var property in dfLayoutLookupField.Properties.AdditionalPropertiesList)
  714. {
  715. columns.Add(property);
  716. }
  717. if (!string.IsNullOrWhiteSpace(dfLayoutLookupField.Properties.Filter))
  718. filter = ReturnFilter(dfLayoutLookupField, lookuptype);
  719. if (lookuptype == typeof(ProductStyle) && JobID != Guid.Empty)
  720. table = ReturnJobStyleProducts(table, client, columns, sort);
  721. if (table.Rows.Count == 0)
  722. table = client.Query(filter, columns, sort); // do generic query if a specific one hasn't already been done
  723. Dictionary<Guid, Dictionary<string, object>> additionalPropertyValues = new Dictionary<Guid, Dictionary<string, object>>();
  724. foreach (var row in table.Rows)
  725. {
  726. Guid id = row.Get<Guid>("ID");
  727. Dictionary<string, object> values = row.ToDictionary(new string[] { "ID" });
  728. additionalPropertyValues.Add(id, values);
  729. string display = LookupFactory.FormatLookup(lookuptype, values, new string[] { });
  730. lookupFieldOptions[id] = display;
  731. }
  732. dfLayoutLookupFieldLookupOptions[element as DFLayoutLookupField] = lookupFieldOptions;
  733. List<string> itemList = lookupFieldOptions.Values.ToList();
  734. DataButtonControl button = new DataButtonControl()
  735. {
  736. Text = "Choose Option",
  737. HorizontalOptions = LayoutOptions.FillAndExpand,
  738. VerticalOptions = LayoutOptions.Center,
  739. Margin = 0,
  740. BackgroundColor = Color.FromHex("#15C7C1"),
  741. FontAttributes = FontAttributes.Bold,
  742. TextColor = Color.White,
  743. CornerRadius = 5,
  744. Padding = 1,
  745. ExtraData = additionalPropertyValues,
  746. LookupFieldOptions = lookupFieldOptions
  747. };
  748. button.Clicked += (object sender, EventArgs eventArgs) =>
  749. {
  750. ListSelectionPage selectionPage = new ListSelectionPage(lookupFieldOptions);
  751. selectionPage.OnSimpleListTapped += (s) =>
  752. {
  753. button.Data = s;
  754. button.Text = s;
  755. dfLayout.ChangeField(dfLayoutLookupField.Name);
  756. };
  757. selectionPage.OnDictionaryItemTapped += (id, s) =>
  758. {
  759. button.Data = s;
  760. button.Text = s;
  761. button.ChosenID = id;
  762. dfLayout.ChangeField(dfLayoutLookupField.Name);
  763. };
  764. Navigation.PushAsync(selectionPage);
  765. };
  766. if (loadData.TryGetValue(dfLayoutLookupField.Name, out value))
  767. {
  768. button.Data = value;
  769. button.Text = value;
  770. }
  771. if (loadRetainedForm)
  772. {
  773. if (RetainedResults.Results.TryGetValue(dfLayoutLookupField.Name, out value))
  774. {
  775. button.Data = value;
  776. button.Text = value;
  777. }
  778. }
  779. if (isRequired)
  780. {
  781. button.BackgroundColor = isRequiredColor;
  782. }
  783. return button;
  784. }
  785. private View LoadDrawingTemplateLookupButton(Type lookuptype, DFLayoutLookupField dfLayoutLookupField, Dictionary<Guid, string> lookupFieldOptions, DFLayoutControl element)
  786. {
  787. string value = "";
  788. var columns = new Columns<DrawingTemplate>(x => x.Code, x => x.Document.ID);
  789. CoreTable table = new Client<DrawingTemplate>().Query(null, columns);
  790. Dictionary<string, Guid> dictionary = new Dictionary<string, Guid>();
  791. foreach (var row in table.Rows)
  792. {
  793. List<object> list = row.Values;
  794. if (list[0] != null && list[1] != null)
  795. dictionary.Add(list[0].ToString(), Guid.Parse(list[1].ToString()));
  796. }
  797. DataButtonControl button = new DataButtonControl()
  798. {
  799. Text = "Choose Option",
  800. HorizontalOptions = LayoutOptions.FillAndExpand,
  801. VerticalOptions = LayoutOptions.Center,
  802. Margin = 0,
  803. BackgroundColor = Color.FromHex("#15C7C1"),
  804. FontAttributes = FontAttributes.Bold,
  805. TextColor = Color.White,
  806. CornerRadius = 5,
  807. Padding = 1,
  808. };
  809. button.Clicked += (object sender, EventArgs eventArgs) =>
  810. {
  811. ListSelectionPage selectionPage = new ListSelectionPage(dictionary);
  812. selectionPage.OnSimpleListTapped += (s) =>
  813. {
  814. button.Data = s;
  815. button.Text = s;
  816. dfLayout.ChangeField(dfLayoutLookupField.Name);
  817. };
  818. Navigation.PushAsync(selectionPage);
  819. };
  820. if (loadData.TryGetValue(dfLayoutLookupField.Name, out value))
  821. {
  822. button.Data = value;
  823. button.Text = value;
  824. }
  825. if (loadRetainedForm)
  826. {
  827. if (RetainedResults.Results.TryGetValue(dfLayoutLookupField.Name, out value))
  828. {
  829. button.Data = value;
  830. button.Text = value;
  831. }
  832. }
  833. if (isRequired)
  834. {
  835. button.BackgroundColor = isRequiredColor;
  836. }
  837. return button;
  838. }
  839. private IFilter ReturnFilter(DFLayoutLookupField dfLayoutLookupField, Type lookuptype)
  840. {
  841. var propertyFilter = dfLayoutLookupField.Properties.Filter;
  842. var filtertype = typeof(Filter<>).MakeGenericType(lookuptype);
  843. var deserialised = Serialization.Deserialize(filtertype, propertyFilter);
  844. IFilter filter = deserialised as IFilter;
  845. return filter;
  846. }
  847. private CoreTable ReturnJobStyleProducts(CoreTable table, IClient client, IColumns columns, ISortOrder sort)
  848. {
  849. CoreTable jobStyleTable = new Client<JobStyle>().Query(new Filter<JobStyle>(x => x.Job.ID).IsEqualTo(JobID),
  850. new Columns<JobStyle>(x => x.Style.ID));
  851. if (jobStyleTable.Rows.Any())
  852. {
  853. Guid firstID = Guid.Parse(jobStyleTable.Rows.FirstOrDefault().Values[0].ToString());
  854. var stylefilter = new Filter<ProductStyle>(x => x.ID).IsEqualTo(firstID);
  855. foreach (CoreRow jobStyleRow in jobStyleTable.Rows)
  856. {
  857. if (jobStyleRow != jobStyleTable.Rows.FirstOrDefault())
  858. {
  859. stylefilter = stylefilter.Or(x => x.ID).IsEqualTo(Guid.Parse(jobStyleRow.Values[0].ToString()));
  860. }
  861. }
  862. table = client.Query(stylefilter, columns, sort);
  863. }
  864. return table;
  865. }
  866. private View LoadProductLookupPicker(View view, DFLayoutLookupField dfLayoutLookupField, string value)
  867. {
  868. FrameButton framebutton = new FrameButton();
  869. if (loadData.TryGetValue(dfLayoutLookupField.Name, out value))
  870. {
  871. framebutton.SetButtonText(value);
  872. framebutton.Data = value + "," + (GlobalVariables.ProductShells.FirstOrDefault(x => x.Code == value)).ID.ToString();
  873. //CoreTable productTable = new Client<Product>().Query(new Filter<Product>(x => x.Code).IsEqualTo(value),
  874. // new Columns<Product>(x => x.Image.ID));
  875. //if (productTable.Rows.Any())
  876. //{
  877. // CoreTable imageTable1 = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(productTable.Rows.First().Values[0].ToString())),
  878. // new Columns<Document>(x => x.Data));
  879. // CoreRow docrow = imageTable1.Rows.FirstOrDefault();
  880. // if (docrow != null)
  881. // {
  882. // byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  883. // ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  884. // if (src != null)
  885. // {
  886. // framebutton.SetImage(src);
  887. // }
  888. // }
  889. //}
  890. }
  891. framebutton.OnFrameButtonClicked += (() =>
  892. {
  893. ProductList productList = new ProductList(GlobalVariables.ProductShells, true);
  894. productList.OnProductSelected += () =>
  895. {
  896. framebutton.SetButtonText(productList.SelectedProduct.Code);
  897. framebutton.Data = productList.SelectedProduct.Code + "," + productList.SelectedProduct.ID.ToString();
  898. //CoreTable imageTable = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(productList.SelectedProduct.ImageID),
  899. // new Columns<Document>(x => x.Data));
  900. //CoreRow docrow = imageTable.Rows.FirstOrDefault();
  901. //if (docrow != null)
  902. //{
  903. // byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  904. // ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  905. // if (src != null)
  906. // {
  907. // framebutton.SetImage(src);
  908. // }
  909. //}
  910. dfLayout.ChangeField(dfLayoutLookupField.Name);
  911. };
  912. Navigation.PushAsync(productList);
  913. });
  914. view = framebutton;
  915. return view;
  916. }
  917. private Tuple<View, Boolean> LoadSignaturePad(DFLayoutControl element)
  918. {
  919. string value = "";
  920. View view = null;
  921. DFLayoutSignaturePad dFLayoutSignaturePad = element as DFLayoutSignaturePad;
  922. var isrequired = dFLayoutSignaturePad.Properties.Required;
  923. if (loadData.TryGetValue(dFLayoutSignaturePad.Name, out value))
  924. {
  925. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture();
  926. byte[] data = Convert.FromBase64String(value);
  927. embeddedImageCapture.DataToImage(data);
  928. view = embeddedImageCapture;
  929. view.IsEnabled = false;
  930. }
  931. else
  932. {
  933. SfSignaturePad signaturePad = new SfSignaturePad();
  934. EmbeddedImageCapture hiddenEmbeddedImageCapture = new EmbeddedImageCapture() { HeightRequest = 200, IsVisible = false, IsEnabled = false };
  935. if (isrequired) signaturePad.BackgroundColor = isRequiredColor;
  936. signaturePad.MinimumHeightRequest = 200;
  937. signaturePad.MinimumStrokeWidth = 0.2;
  938. signaturePad.MaximumStrokeWidth = 17;
  939. Button clearButton = new Button()
  940. {
  941. Text = "Clear",
  942. HorizontalOptions = LayoutOptions.FillAndExpand,
  943. VerticalOptions = LayoutOptions.End,
  944. Margin = 0,
  945. BackgroundColor = Color.FromHex("#15C7C1"),
  946. FontAttributes = FontAttributes.Bold,
  947. TextColor = Color.White,
  948. CornerRadius = 5,
  949. Padding = 1
  950. };
  951. clearButton.Clicked += (object sender, EventArgs e) =>
  952. {
  953. signaturePad.IsVisible = true;
  954. hiddenEmbeddedImageCapture.IsVisible = false;
  955. useSavedSignatures.Remove(dFLayoutSignaturePad.Name);
  956. signaturePad.Clear();
  957. };
  958. Grid.SetColumn(clearButton, 2);
  959. Button signNowButton = new Button()
  960. {
  961. Text = "Apply Saved",
  962. HorizontalOptions = LayoutOptions.FillAndExpand,
  963. VerticalOptions = LayoutOptions.End,
  964. Margin = 0,
  965. BackgroundColor = Color.FromHex("#15C7C1"),
  966. FontAttributes = FontAttributes.Bold,
  967. TextColor = Color.White,
  968. CornerRadius = 5,
  969. Padding = 1
  970. };
  971. signNowButton.Clicked += (object sender, EventArgs e) =>
  972. {
  973. if (Application.Current.Properties.ContainsKey("SavedSignature"))
  974. {
  975. Task.Run(() =>
  976. {
  977. hiddenEmbeddedImageCapture.DataToImage((Application.Current.Properties["SavedSignature"]) as byte[]);
  978. });
  979. Thread.Sleep(1000);
  980. signaturePad.IsVisible = false;
  981. hiddenEmbeddedImageCapture.IsVisible = true;
  982. if (!useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  983. useSavedSignatures.Add(dFLayoutSignaturePad.Name);
  984. }
  985. };
  986. Grid.SetColumn(signNowButton, 1);
  987. Button createSignatureButton = new Button()
  988. {
  989. Text = "Save New",
  990. HorizontalOptions = LayoutOptions.FillAndExpand,
  991. VerticalOptions = LayoutOptions.End,
  992. Margin = 0,
  993. BackgroundColor = Color.FromHex("#15C7C1"),
  994. FontAttributes = FontAttributes.Bold,
  995. TextColor = Color.White,
  996. CornerRadius = 5,
  997. Padding = 1
  998. };
  999. createSignatureButton.Clicked += (object sender, EventArgs e) =>
  1000. {
  1001. SignatureSaver saver = new SignatureSaver();
  1002. saver.OnSignatureSaved += () =>
  1003. {
  1004. Task.Run(() =>
  1005. {
  1006. hiddenEmbeddedImageCapture.DataToImage((Application.Current.Properties["SavedSignature"]) as byte[]);
  1007. });
  1008. Thread.Sleep(1000);
  1009. signaturePad.IsVisible = false;
  1010. hiddenEmbeddedImageCapture.IsVisible = true;
  1011. if (!useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  1012. useSavedSignatures.Add(dFLayoutSignaturePad.Name);
  1013. };
  1014. Navigation.PushAsync(saver);
  1015. };
  1016. Grid.SetColumn(createSignatureButton, 0);
  1017. Grid buttonsGrid = new Grid { Margin = 0, Padding = 0, VerticalOptions = LayoutOptions.End };
  1018. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1019. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1020. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1021. buttonsGrid.Children.Add(createSignatureButton);
  1022. buttonsGrid.Children.Add(signNowButton);
  1023. buttonsGrid.Children.Add(clearButton);
  1024. Grid grid = new Grid() { Margin = 0, Padding = 0 };
  1025. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(200, GridUnitType.Absolute) });
  1026. Grid.SetRow(signaturePad, 0);
  1027. Grid.SetRow(hiddenEmbeddedImageCapture, 0);
  1028. grid.Children.Add(hiddenEmbeddedImageCapture);
  1029. grid.Children.Add(signaturePad);
  1030. grid.Children.Add(buttonsGrid);
  1031. view = grid;
  1032. }
  1033. return new Tuple<View, Boolean>(view, isrequired);
  1034. }
  1035. private Tuple<View, Boolean> LoadEmbeddedImage(DFLayoutControl element)
  1036. {
  1037. string value = "";
  1038. bool disableLibrary = false;
  1039. DFLayoutEmbeddedImage dfLayoutEmbeddedImage = element as DFLayoutEmbeddedImage;
  1040. try
  1041. {
  1042. disableLibrary = dfLayoutEmbeddedImage.Properties.DisableLibrary;
  1043. }
  1044. catch { }
  1045. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture(disableLibrary);
  1046. if (disableLibrary)
  1047. embeddedImageCapture.LibraryButton.BackgroundColor = Color.LightGray;
  1048. if (loadData.TryGetValue(dfLayoutEmbeddedImage.Name, out value))
  1049. {
  1050. byte[] data = Convert.FromBase64String(value);
  1051. embeddedImageCapture.DataToImage(data);
  1052. }
  1053. else
  1054. {
  1055. if (dfLayoutEmbeddedImage.Properties.Required)
  1056. {
  1057. embeddedImageCapture.CameraButton.BackgroundColor = isRequiredColor;
  1058. embeddedImageCapture.CameraButton.BorderWidth = 1;
  1059. embeddedImageCapture.CameraButton.BorderColor = Color.Black;
  1060. if (!disableLibrary)
  1061. {
  1062. embeddedImageCapture.LibraryButton.BackgroundColor = isRequiredColor;
  1063. embeddedImageCapture.LibraryButton.BorderWidth = 1;
  1064. embeddedImageCapture.LibraryButton.BorderColor = Color.Black;
  1065. }
  1066. }
  1067. }
  1068. return new Tuple<View, Boolean>(embeddedImageCapture, dfLayoutEmbeddedImage.Properties.Required);
  1069. }
  1070. private Tuple<View, Boolean> LoadMultiImage(DFLayoutControl element)
  1071. {
  1072. string value = "";
  1073. bool disableLibrary = false;
  1074. DFLayoutMultiImage dfLayoutMultiEmbeddedImage = element as DFLayoutMultiImage;
  1075. try
  1076. {
  1077. disableLibrary = dfLayoutMultiEmbeddedImage.Properties.DisableLibrary;
  1078. }
  1079. catch { }
  1080. MultiImageCapture multiImageCapture = new MultiImageCapture(disableLibrary);
  1081. if (loadData.TryGetValue(dfLayoutMultiEmbeddedImage.Name, out value))
  1082. {
  1083. List<string> values = Serialization.Deserialize<List<string>>(value);
  1084. foreach (string s in values)
  1085. {
  1086. byte[] data = Convert.FromBase64String(s);
  1087. multiImageCapture.DataToImage(data);
  1088. }
  1089. }
  1090. return new Tuple<View, bool>(multiImageCapture, dfLayoutMultiEmbeddedImage.Properties.Required);
  1091. }
  1092. private Tuple<View, bool> LoadMultiSignaturePad(DFLayoutControl element)
  1093. {
  1094. string value = "";
  1095. DFLayoutMultiSignaturePad dfLayoutMultiSignaturePad = element as DFLayoutMultiSignaturePad;
  1096. DataButtonControl button = new DataButtonControl
  1097. {
  1098. Text = "Add Signatures",
  1099. TextColor = Color.White,
  1100. HorizontalOptions = LayoutOptions.FillAndExpand,
  1101. CornerRadius = 0,
  1102. BackgroundColor = Color.FromHex("#15C7C1"),
  1103. FontAttributes = FontAttributes.Bold
  1104. };
  1105. if (loadData.TryGetValue(dfLayoutMultiSignaturePad.Name, out value))
  1106. {
  1107. button.Data = value;
  1108. }
  1109. MultiSignaturePad multiSignaturePad = new MultiSignaturePad(button.Data);
  1110. multiSignaturePad.OnMultiSignatureSaved += (result) =>
  1111. {
  1112. button.Data = result;
  1113. };
  1114. button.Clicked += (object sender, EventArgs e) =>
  1115. {
  1116. Navigation.PushAsync(multiSignaturePad);
  1117. };
  1118. return new Tuple<View, bool>(button, dfLayoutMultiSignaturePad.Properties.Required);
  1119. }
  1120. private Tuple<View, bool> LoadAddTaskField(DFLayoutControl element)
  1121. {
  1122. string value = "";
  1123. DFLayoutAddTaskField field = element as DFLayoutAddTaskField;
  1124. DFCreateTaskView taskView = new DFCreateTaskView();
  1125. taskView.OnAddTaskButtonClicked += () =>
  1126. {
  1127. AddEditTask addEditTask = new AddEditTask(Guid.Empty, "New task from Digital Form");
  1128. if (field.Properties.TaskType != null)
  1129. {
  1130. addEditTask.kanban.Type.ID = field.Properties.TaskType.ID;
  1131. try
  1132. {
  1133. addEditTask.kanban.Type.Description = new Client<KanbanType>().Query
  1134. (new Filter<KanbanType>(x => x.ID).IsEqualTo(field.Properties.TaskType.ID),
  1135. new Columns<KanbanType>(x => x.Description)).Rows.FirstOrDefault().Values[0].ToString();
  1136. }
  1137. catch { }
  1138. addEditTask.UpdateScreen(true);
  1139. }
  1140. addEditTask.OnTaskSaved += (taskNumber) =>
  1141. {
  1142. taskView.DisableButton();
  1143. taskView.TaskNumber = taskNumber.ToString();
  1144. };
  1145. Navigation.PushAsync(addEditTask);
  1146. };
  1147. if (loadData.TryGetValue(field.Name, out value))
  1148. {
  1149. taskView.TaskNumber = value;
  1150. taskView.DisableButton();
  1151. }
  1152. return new Tuple<View, bool>(taskView, field.Properties.Required);
  1153. }
  1154. private void FormatAndAddView(View view, DFLayoutControl element, bool isrequired)
  1155. {
  1156. try
  1157. {
  1158. if (element is DFLayoutField)
  1159. {
  1160. pairs[element as DFLayoutField] = view;
  1161. if (isSecure || readOnly || !string.IsNullOrWhiteSpace((element as DFLayoutField).GetPropertyValue<string>("Expression")))
  1162. view.IsEnabled = false;
  1163. Frame frame = new Frame
  1164. {
  1165. Content = view,
  1166. BorderColor = Color.FromHex("#15C7C1"),
  1167. BackgroundColor = Color.Default,
  1168. CornerRadius = 5,
  1169. Padding = 2,
  1170. HasShadow = false,
  1171. };
  1172. if (readOnly || isSecure)
  1173. {
  1174. frame.BorderColor = Color.Gray;
  1175. frame.BackgroundColor = Color.LightGray;
  1176. frame.VerticalOptions = LayoutOptions.FillAndExpand;
  1177. frame.MinimumHeightRequest = 60;
  1178. }
  1179. if (isrequired)
  1180. {
  1181. frame.BackgroundColor = isRequiredColor;
  1182. }
  1183. view = frame;
  1184. }
  1185. if (element.Row > 0) SetRow(view, element.Row - 1); //rows and columns coming in from the desktop designer start from 1, not 0 (most of the time??)
  1186. else if (element.Row == 0) SetRow(view, dfLayout.RowHeights.Count + 1);
  1187. if (element.RowSpan > 0) SetRowSpan(view, element.RowSpan);
  1188. else if (element.RowSpan == 0) SetRowSpan(view, 1);
  1189. if (element.Column > 0) SetColumn(view, element.Column - 1); //rows and columns coming in from the desktop designer start from 1, not 0 (most of the time??)
  1190. if (element.ColumnSpan > 0) SetColumnSpan(view, element.ColumnSpan);
  1191. if (element.ColumnSpan > 1)
  1192. {
  1193. view.HorizontalOptions = LayoutOptions.FillAndExpand;
  1194. if (view is Label)
  1195. {
  1196. view.HorizontalOptions = LayoutOptions.CenterAndExpand;
  1197. Label lblView = view as Label;
  1198. lblView.HorizontalTextAlignment = TextAlignment.Center;
  1199. view = lblView;
  1200. }
  1201. }
  1202. else if (element.ColumnSpan == 0) SetColumnSpan(view, 1);
  1203. view.SetValue(Grid.MarginProperty, 0.01);
  1204. if (element is DFLayoutSignaturePad)
  1205. {
  1206. view.Margin = new Thickness(25, 0, 25, 0);
  1207. }
  1208. Device.BeginInvokeOnMainThread(() => { Children.Add(view); });
  1209. if (element is DFLayoutHeader)
  1210. if ((element as DFLayoutHeader).Collapsed)
  1211. headersToCollapse.Add(view as DigitalFormsHeader);
  1212. }
  1213. catch
  1214. { }
  1215. }
  1216. #endregion
  1217. #region Save
  1218. public void SaveData(bool saveForLater)
  1219. {
  1220. try
  1221. {
  1222. results.Clear();
  1223. propertyResults.Clear();
  1224. isRequiredEmpty = false;
  1225. isRequiredMessage = "";
  1226. foreach (DFLayoutField field in pairs.Keys)
  1227. {
  1228. View view = pairs[field];
  1229. String userInput = AddResultsToDictionary(field, view);
  1230. if (isRequiredEmpty)
  1231. {
  1232. if (!saveForLater)
  1233. return;
  1234. }
  1235. AddPropertyToDictionary(field, userInput);
  1236. }
  1237. UpdateInstanceAndModel(saveForLater);
  1238. }
  1239. catch (Exception ex)
  1240. {
  1241. errors.Add(ex.Message);
  1242. }
  1243. }
  1244. #region Adding Results to dictionaries
  1245. private string AddResultsToDictionary(DFLayoutField field, View view, bool AddToDictionary = true) //"results" dictionary is the data to be saved in Form.Data
  1246. {
  1247. string userInput = "";
  1248. if (field is DFLayoutStringField || field is DFLayoutIntegerField || field is DFLayoutDoubleField)
  1249. {
  1250. userInput = (view as Editor).Text;
  1251. }
  1252. else if (field is DFLayoutBooleanField)
  1253. {
  1254. if ((view as CustomBoolean).ValueChanged)
  1255. {
  1256. userInput = (view as CustomBoolean).Value.ToString();
  1257. }
  1258. }
  1259. else if (field is DFLayoutDateField)
  1260. {
  1261. userInput = ((view as DatePicker).Date).ToString("dd-MM-yyyy");
  1262. }
  1263. else if (field is DFLayoutTimeField)
  1264. {
  1265. userInput = ((view as TimePicker).Time).ToString("c");
  1266. }
  1267. else if (field is DFLayoutOptionField)
  1268. {
  1269. userInput = GetStringFromOptionField(view);
  1270. }
  1271. else if (field is DFLayoutLookupField)
  1272. {
  1273. if (view is DataButtonControl)
  1274. {
  1275. var button = view as DataButtonControl;
  1276. userInput = FindIDForDesignLookupField(button); //userinput is used to set guid later on for properties, but will not get added to form data only for lookupfields
  1277. results.Add(field.Name, button.Data);
  1278. if (RetainedResults.IsFormRetained)
  1279. if ((field as DFLayoutLookupField).Properties.Retain)
  1280. RetainedResults.Results.Add(field.Name, button.Data);
  1281. }
  1282. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1283. {
  1284. FrameButton framebutton = view as FrameButton;
  1285. if (!string.IsNullOrWhiteSpace(framebutton.Data))
  1286. {
  1287. string[] buttonData = framebutton.Data.Split(',');
  1288. if (AddToDictionary)
  1289. results.Add(field.Name, buttonData[0]); //[0] is product.code
  1290. userInput = buttonData[1]; //[1] is product id as string
  1291. if (RetainedResults.IsFormRetained)
  1292. if ((field as DFLayoutLookupField).Properties.Retain)
  1293. RetainedResults.Results.Add(field.Name, buttonData[0]);
  1294. }
  1295. }
  1296. }
  1297. else if (field is DFLayoutSignaturePad)
  1298. {
  1299. string value = "";
  1300. var grid = view as Grid;
  1301. var signaturePad = grid.Children.OfType<SfSignaturePad>().FirstOrDefault();
  1302. DFLayoutSignaturePad dFLayoutSignaturePad = field as DFLayoutSignaturePad;
  1303. if (loadData.TryGetValue(dFLayoutSignaturePad.Name, out value))
  1304. {
  1305. userInput = value;
  1306. }
  1307. else
  1308. {
  1309. if (useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  1310. {
  1311. ImageSource src = ImageSource.FromStream(() => new MemoryStream((Application.Current.Properties["SavedSignature"]) as byte[]));
  1312. userInput = ImageSourceToBase64(src);
  1313. }
  1314. else
  1315. {
  1316. signaturePad.Save();
  1317. if (signaturePad.ImageSource != null)
  1318. {
  1319. userInput = ImageSourceToBase64(signaturePad.ImageSource);
  1320. }
  1321. }
  1322. }
  1323. }
  1324. else if (field is DFLayoutEmbeddedImage)
  1325. {
  1326. var embeddedImage = view as EmbeddedImageCapture;
  1327. if (embeddedImage.Image.Source != null)
  1328. {
  1329. try
  1330. {
  1331. userInput = ImageSourceToBase64(embeddedImage.Image.Source);
  1332. }
  1333. catch (Exception e)
  1334. {
  1335. userInput = "";
  1336. }
  1337. }
  1338. }
  1339. else if (field is DFLayoutMultiImage)
  1340. {
  1341. var multiImage = view as MultiImageCapture;
  1342. if (multiImage.Images.Count > 0)
  1343. {
  1344. try
  1345. {
  1346. userInput = ImagesToBase64(multiImage.Images);
  1347. }
  1348. catch (Exception e)
  1349. {
  1350. userInput = "";
  1351. }
  1352. }
  1353. }
  1354. else if (field is DFLayoutAddTaskField)
  1355. {
  1356. var addTaskButton = view as DFCreateTaskView;
  1357. if (!string.IsNullOrWhiteSpace(addTaskButton.TaskNumber))
  1358. {
  1359. userInput = addTaskButton.TaskNumber;
  1360. }
  1361. }
  1362. else if (field is DFLayoutMultiSignaturePad)
  1363. {
  1364. var databutton = view as DataButtonControl;
  1365. if (!string.IsNullOrWhiteSpace(databutton.Data))
  1366. userInput = databutton.Data;
  1367. }
  1368. //add the field name and input to 'results' dictionary for processing outside of QAFormViewer
  1369. if (!String.IsNullOrWhiteSpace(userInput) && AddToDictionary)
  1370. {
  1371. if (field is DFLayoutLookupField)
  1372. { /*do nothing as results have already been added*/ }
  1373. else
  1374. {
  1375. try
  1376. {
  1377. results.Add(field.Name, userInput);
  1378. }
  1379. catch (Exception e)
  1380. {
  1381. errors.Add(e.Message);
  1382. }
  1383. if (RetainedResults.IsFormRetained)
  1384. {
  1385. AddRetainedToDictionary(field, userInput);
  1386. }
  1387. }
  1388. }
  1389. else CheckRequired(field);
  1390. return userInput;
  1391. }
  1392. private void AddPropertyToDictionary(DFLayoutField field, String userInput)
  1393. {
  1394. if (!string.IsNullOrWhiteSpace(userInput))
  1395. {
  1396. try
  1397. {
  1398. string property = "";
  1399. if (field is DFLayoutStringField)
  1400. property = (field as DFLayoutStringField).Properties.Property;
  1401. else if (field is DFLayoutIntegerField)
  1402. property = (field as DFLayoutIntegerField).Properties.Property;
  1403. else if (field is DFLayoutDoubleField)
  1404. property = (field as DFLayoutDoubleField).Properties.Property;
  1405. else if (field is DFLayoutBooleanField)
  1406. property = (field as DFLayoutBooleanField).Properties.Property;
  1407. else if (field is DFLayoutDateField)
  1408. property = (field as DFLayoutDateField).Properties.Property;
  1409. else if (field is DFLayoutTimeField)
  1410. property = (field as DFLayoutTimeField).Properties.Property;
  1411. else if (field is DFLayoutOptionField)
  1412. property = (field as DFLayoutOptionField).Properties.Property;
  1413. else if (field is DFLayoutLookupField)
  1414. property = (field as DFLayoutLookupField).Properties.Property;
  1415. //'propertyResults' dictionary for processing outside of QAFormViewer
  1416. if (!string.IsNullOrWhiteSpace(property)) propertyResults.Add(property, userInput);
  1417. }
  1418. catch (Exception e)
  1419. {
  1420. errors.Add(e.Message);
  1421. return;
  1422. }
  1423. }
  1424. }
  1425. private async void AddRetainedToDictionary(DFLayoutField field, string userInput)
  1426. {
  1427. await Task.Run(() =>
  1428. {
  1429. try
  1430. {
  1431. if (field is DFLayoutStringField)
  1432. {
  1433. if ((field as DFLayoutStringField).Properties.Retain)
  1434. RetainedResults.Results.Add(field.Name, userInput);
  1435. }
  1436. else if (field is DFLayoutIntegerField)
  1437. {
  1438. if ((field as DFLayoutIntegerField).Properties.Retain)
  1439. RetainedResults.Results.Add(field.Name, userInput);
  1440. }
  1441. else if (field is DFLayoutDoubleField)
  1442. {
  1443. if ((field as DFLayoutDoubleField).Properties.Retain)
  1444. RetainedResults.Results.Add(field.Name, userInput);
  1445. }
  1446. else if (field is DFLayoutBooleanField)
  1447. {
  1448. if ((field as DFLayoutBooleanField).Properties.Retain)
  1449. RetainedResults.Results.Add(field.Name, userInput);
  1450. }
  1451. else if (field is DFLayoutDateField)
  1452. {
  1453. if ((field as DFLayoutDateField).Properties.Retain)
  1454. RetainedResults.Results.Add(field.Name, userInput);
  1455. }
  1456. else if (field is DFLayoutTimeField)
  1457. {
  1458. if ((field as DFLayoutTimeField).Properties.Retain)
  1459. RetainedResults.Results.Add(field.Name, userInput);
  1460. }
  1461. else if (field is DFLayoutOptionField)
  1462. {
  1463. if ((field as DFLayoutOptionField).Properties.Retain)
  1464. RetainedResults.Results.Add(field.Name, userInput);
  1465. }
  1466. }
  1467. catch (Exception e)
  1468. {
  1469. errors.Add("Retained results error: " + e.Message);
  1470. }
  1471. });
  1472. }
  1473. #endregion
  1474. #region Checks / Custom methods
  1475. private void CheckRequired(DFLayoutField field)
  1476. {
  1477. if (field is DFLayoutStringField)
  1478. {
  1479. if ((field as DFLayoutStringField).Properties.Required)
  1480. {
  1481. isRequiredEmpty = true;
  1482. isRequiredMessage = (field as DFLayoutStringField).Description;
  1483. }
  1484. }
  1485. else if (field is DFLayoutIntegerField)
  1486. {
  1487. if ((field as DFLayoutIntegerField).Properties.Required)
  1488. {
  1489. isRequiredEmpty = true;
  1490. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  1491. }
  1492. }
  1493. else if (field is DFLayoutDoubleField)
  1494. {
  1495. if ((field as DFLayoutDoubleField).Properties.Required)
  1496. {
  1497. {
  1498. isRequiredEmpty = true;
  1499. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  1500. }
  1501. }
  1502. }
  1503. else if (field is DFLayoutBooleanField)
  1504. {
  1505. if ((field as DFLayoutBooleanField).Properties.Required)
  1506. {
  1507. {
  1508. isRequiredEmpty = true;
  1509. isRequiredMessage = (field as DFLayoutBooleanField).Description;
  1510. }
  1511. }
  1512. }
  1513. else if (field is DFLayoutDateField)
  1514. {
  1515. if ((field as DFLayoutDateField).Properties.Required)
  1516. {
  1517. {
  1518. isRequiredEmpty = true;
  1519. isRequiredMessage = (field as DFLayoutDateField).Description;
  1520. }
  1521. }
  1522. }
  1523. else if (field is DFLayoutTimeField)
  1524. {
  1525. if ((field as DFLayoutTimeField).Properties.Required)
  1526. {
  1527. {
  1528. isRequiredEmpty = true;
  1529. isRequiredMessage = (field as DFLayoutTimeField).Description;
  1530. }
  1531. }
  1532. }
  1533. else if (field is DFLayoutOptionField)
  1534. {
  1535. if ((field as DFLayoutOptionField).Properties.Required)
  1536. {
  1537. {
  1538. isRequiredEmpty = true;
  1539. isRequiredMessage = (field as DFLayoutOptionField).Description;
  1540. }
  1541. }
  1542. }
  1543. else if (field is DFLayoutLookupField)
  1544. {
  1545. if ((field as DFLayoutLookupField).Properties.Required)
  1546. {
  1547. {
  1548. isRequiredEmpty = true;
  1549. isRequiredMessage = (field as DFLayoutLookupField).Description;
  1550. }
  1551. }
  1552. }
  1553. else if (field is DFLayoutSignaturePad)
  1554. {
  1555. if ((field as DFLayoutSignaturePad).Properties.Required)
  1556. {
  1557. {
  1558. isRequiredEmpty = true;
  1559. isRequiredMessage = (field as DFLayoutSignaturePad).Description;
  1560. }
  1561. }
  1562. }
  1563. else if (field is DFLayoutEmbeddedImage)
  1564. {
  1565. if ((field as DFLayoutEmbeddedImage).Properties.Required)
  1566. {
  1567. {
  1568. isRequiredEmpty = true;
  1569. isRequiredMessage = (field as DFLayoutEmbeddedImage).Description;
  1570. }
  1571. }
  1572. }
  1573. else if (field is DFLayoutMultiImage)
  1574. {
  1575. if ((field as DFLayoutMultiImage).Properties.Required)
  1576. {
  1577. {
  1578. isRequiredEmpty = true;
  1579. isRequiredMessage = (field as DFLayoutMultiImage).Description;
  1580. }
  1581. }
  1582. }
  1583. else if (field is DFLayoutMultiSignaturePad)
  1584. {
  1585. if ((field as DFLayoutMultiSignaturePad).Properties.Required)
  1586. {
  1587. {
  1588. isRequiredEmpty = true;
  1589. isRequiredMessage = (field as DFLayoutMultiSignaturePad).Description;
  1590. }
  1591. }
  1592. }
  1593. }
  1594. private string ImagesToBase64(List<Image> images)
  1595. {
  1596. List<string> strings = new List<string>();
  1597. foreach (Image image in images)
  1598. {
  1599. strings.Add(ImageSourceToBase64(image.Source));
  1600. }
  1601. return Serialization.Serialize(strings);
  1602. }
  1603. private string ImageSourceToBase64(ImageSource source)
  1604. {
  1605. StreamImageSource streamImageSource = (StreamImageSource)source;
  1606. System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
  1607. Task<Stream> task = streamImageSource.Stream(cancellationToken);
  1608. Stream stream = task.Result;
  1609. byte[] bytes = new byte[stream.Length];
  1610. stream.Read(bytes, 0, bytes.Length);
  1611. string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  1612. return s;
  1613. }
  1614. private string FindIDForDesignLookupField(DataButtonControl button)
  1615. {
  1616. try
  1617. {
  1618. string userInputValue = button.Data;
  1619. string guidString = "";
  1620. if (userInputValue != nullOrInvalidType)
  1621. {
  1622. foreach (KeyValuePair<DFLayoutLookupField, Dictionary<Guid, string>> pair in dfLayoutLookupFieldLookupOptions)
  1623. {
  1624. foreach (KeyValuePair<Guid, string> innerPair in pair.Value)
  1625. {
  1626. if (innerPair.Value == userInputValue)
  1627. {
  1628. guidString = innerPair.Key.ToString();
  1629. }
  1630. }
  1631. }
  1632. }
  1633. return guidString;
  1634. }
  1635. catch
  1636. {
  1637. return "";
  1638. }
  1639. }
  1640. #endregion
  1641. #region Save Completion
  1642. //point of determination for whether or not QA form gets completed or saved for later
  1643. private void UpdateInstanceAndModel(bool saveForLater)
  1644. {
  1645. if (results.Count != 0)
  1646. {
  1647. if (!saveForLater)
  1648. {
  1649. CompleteForm();
  1650. }
  1651. model.Instance.FormCompletedBy.ID = userID;
  1652. if (digitalFormID != Guid.Empty) model.Instance.Form.ID = digitalFormID;
  1653. DigitalForm.SerializeFormData(model.Instance, QueryVariables(model.Instance), results.ToDictionary(x => x.Key, x => x.Value as object));
  1654. FormData = model.Instance.FormData;
  1655. if (model.Instance.FormStarted == DateTime.MinValue)
  1656. {
  1657. model.Instance.FormStarted = timeStarted;
  1658. }
  1659. TimeSpan span = DateTime.Now - timeStarted;
  1660. model.Instance.FormOpen = model.Instance.FormOpen + span;
  1661. model.Update(null);
  1662. }
  1663. }
  1664. public List<DigitalFormVariable> QueryVariables(IDigitalFormInstance form)
  1665. {
  1666. List<DigitalFormVariable> variables = new List<DigitalFormVariable>();
  1667. var table = new Client<DigitalFormVariable>().Query(
  1668. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(form.Form.ID),
  1669. new Columns<DigitalFormVariable>(x => x.Code, x => x.Parameters, x => x.Description, x => x.VariableType),
  1670. null
  1671. );
  1672. foreach (CoreRow coreRow in table.Rows)
  1673. {
  1674. var variable = coreRow.ToObject<DigitalFormVariable>();
  1675. variables.Add(variable);
  1676. }
  1677. return variables;
  1678. }
  1679. private void CompleteForm()
  1680. {
  1681. model.Instance.FormCompleted = DateTime.Now;
  1682. FormCompleted = model.Instance.FormCompleted;
  1683. model.Instance.Location.Longitude = location.Longitude;
  1684. model.Instance.Location.Latitude = location.Latitude;
  1685. model.Instance.Location.Timestamp = model.Instance.FormCompleted;
  1686. }
  1687. #endregion
  1688. #endregion
  1689. #region Expressions
  1690. public object GetFieldValue(string field)
  1691. {
  1692. try
  1693. {
  1694. Tuple<DFLayoutField, View> tuple = FindView(field);
  1695. return GetValueFromView(tuple.Item1, tuple.Item2);
  1696. }
  1697. catch
  1698. {
  1699. return "";
  1700. }
  1701. }
  1702. public void SetFieldValue(string field, object value)
  1703. {
  1704. try
  1705. {
  1706. Tuple<DFLayoutField, View> tuple = FindView(field);
  1707. SetValueOfView(tuple.Item1, tuple.Item2, value);
  1708. }
  1709. catch { }
  1710. }
  1711. private void SetValueOfView(DFLayoutField field, View view, object value)
  1712. {
  1713. if (field is DFLayoutBooleanField)
  1714. (view as CustomBoolean).Value = (bool)value;
  1715. else if (field is DFLayoutStringField || field is DFLayoutIntegerField || field is DFLayoutDoubleField)
  1716. (view as Editor).Text = value.ToString();
  1717. else if (field is DFLayoutDateField)
  1718. (view as DatePicker).Date = (DateTime)(value as DateTime?);
  1719. else if (field is DFLayoutDateTimeField)
  1720. (view as TimePicker).Time = (TimeSpan)(value as TimeSpan?);
  1721. else if (field is DFLayoutOptionField)
  1722. AssignPickerOption(view, field as DFLayoutOptionField, value as string);
  1723. else if (field is DFLayoutLookupField)
  1724. AssignLookupOption(view, value as string);
  1725. else if (field is DFLayoutEmbeddedImage)
  1726. AssignImage(view, value as byte[]);
  1727. }
  1728. private void AssignImage(View view, byte[] bytes)
  1729. {
  1730. var imageCapture = view as EmbeddedImageCapture;
  1731. imageCapture.DataToImage(bytes);
  1732. }
  1733. private void AssignLookupOption(View view, string value)
  1734. {
  1735. if (view is DataButtonControl)
  1736. {
  1737. (view as DataButtonControl).Text = value;
  1738. (view as DataButtonControl).Data = value;
  1739. }
  1740. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1741. {
  1742. (view as FrameButton).SetButtonText(value);
  1743. (view as FrameButton).Data = value + "," + Guid.Empty.ToString();
  1744. }
  1745. }
  1746. private Tuple<DFLayoutField, View> FindView(string field)
  1747. {
  1748. DFLayoutField dflayoutfield = pairs.Keys.FirstOrDefault(x => x.Name == field);
  1749. View view = pairs[dflayoutfield];
  1750. return new Tuple<DFLayoutField, View>(dflayoutfield, view);
  1751. }
  1752. private void AssignPickerOption(View view, DFLayoutOptionField field, string value)
  1753. {
  1754. string s = field.Properties.Options;
  1755. string[] substrings = s.Split(',');
  1756. var optionList = substrings.ToList();
  1757. if (GlobalVariables.DeviceString == "i" || GlobalVariables.DeviceString == "I")
  1758. {
  1759. var iOSPicker = view as CustomPickeriOS;
  1760. iOSPicker.SelectedIndex = optionList.IndexOf(value);
  1761. }
  1762. else
  1763. {
  1764. var picker = view as Picker;
  1765. picker.SelectedIndex = optionList.IndexOf(value);
  1766. }
  1767. }
  1768. private object GetValueFromView(DFLayoutField field, View view)
  1769. {
  1770. if (field is DFLayoutBooleanField)
  1771. return (view as CustomBoolean).Value;
  1772. if (field is DFLayoutStringField)
  1773. return (view as Editor).Text;
  1774. else if (field is DFLayoutIntegerField)
  1775. return int.Parse((view as Editor).Text);
  1776. else if (field is DFLayoutDoubleField)
  1777. return double.Parse((view as Editor).Text);
  1778. else if (field is DFLayoutDateField)
  1779. return (view as DatePicker).Date;
  1780. else if (field is DFLayoutDateTimeField)
  1781. return (view as TimePicker).Time;
  1782. else if (field is DFLayoutOptionField)
  1783. return GetStringFromOptionField(view);
  1784. else if (field is DFLayoutLookupField)
  1785. return GetStringFromLookupField(view);
  1786. else if (field is DFLayoutSignaturePad)
  1787. return GetByteArrayFromSignaturePad(view, field.Name);
  1788. else if (field is DFLayoutEmbeddedImage)
  1789. return GetByteArrayFromEmbeddedImage(view);
  1790. else if (field is DFLayoutMultiImage)
  1791. return GetByteArrayListFromMultiImage(view);
  1792. else return "";
  1793. }
  1794. private object GetByteArrayListFromMultiImage(View view)
  1795. {
  1796. List<byte[]> list = new List<byte[]>();
  1797. var multiImage = view as MultiImageCapture;
  1798. if (multiImage.Images.Count > 0)
  1799. {
  1800. foreach (Image image in multiImage.Images)
  1801. {
  1802. string base64 = ImageSourceToBase64(image.Source);
  1803. list.Add(Convert.FromBase64String(base64));
  1804. }
  1805. }
  1806. return list;
  1807. }
  1808. private object GetByteArrayFromEmbeddedImage(View view)
  1809. {
  1810. var embeddedImage = view as EmbeddedImageCapture;
  1811. if (embeddedImage.Image.Source != null)
  1812. {
  1813. string base64 = ImageSourceToBase64(embeddedImage.Image.Source);
  1814. return Convert.FromBase64String(base64);
  1815. }
  1816. else
  1817. return null;
  1818. }
  1819. private byte[] GetByteArrayFromSignaturePad(View view, string name)
  1820. {
  1821. var grid = view as Grid;
  1822. var signaturePad = grid.Children.OfType<SfSignaturePad>().FirstOrDefault();
  1823. if (useSavedSignatures.Contains(name))
  1824. {
  1825. return Application.Current.Properties["SavedSignature"] as byte[];
  1826. }
  1827. else
  1828. {
  1829. signaturePad.Save();
  1830. if (signaturePad.ImageSource != null)
  1831. {
  1832. string base64 = ImageSourceToBase64(signaturePad.ImageSource);
  1833. return Convert.FromBase64String(base64);
  1834. }
  1835. else
  1836. return null;
  1837. }
  1838. }
  1839. private object GetStringFromLookupField(View view)
  1840. {
  1841. string value = "";
  1842. if (view is DataButtonControl)
  1843. {
  1844. var button = view as DataButtonControl;
  1845. value = FindIDForDesignLookupField(button); //userinput is used to set guid later on for properties, but will not get added to form data only for lookupfields
  1846. }
  1847. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1848. {
  1849. FrameButton framebutton = view as FrameButton;
  1850. if (!string.IsNullOrWhiteSpace(framebutton.Data))
  1851. {
  1852. string[] buttonData = framebutton.Data.Split(',');
  1853. value = buttonData[1]; //[1] is product id as string
  1854. }
  1855. }
  1856. return value;
  1857. }
  1858. private string GetStringFromOptionField(View view)
  1859. {
  1860. if (GlobalVariables.DeviceString == "i" || GlobalVariables.DeviceString == "I")
  1861. {
  1862. var iOSPicker = view as CustomPickeriOS;
  1863. if (iOSPicker.SelectedIndex != -1)
  1864. {
  1865. return iOSPicker.SelectedItem.ToString();
  1866. }
  1867. else
  1868. return "";
  1869. }
  1870. else
  1871. {
  1872. var picker = view as Picker;
  1873. if (picker.SelectedIndex != -1)
  1874. {
  1875. return picker.SelectedItem.ToString();
  1876. }
  1877. else
  1878. return "";
  1879. }
  1880. }
  1881. public object GetFieldData(string fieldName, string dataField)
  1882. {
  1883. Tuple<DFLayoutField, View> tuple = FindView(fieldName);
  1884. try
  1885. {
  1886. if (tuple.Item1 is DFLayoutLookupField)
  1887. {
  1888. DFLayoutLookupField field = tuple.Item1 as DFLayoutLookupField;
  1889. string type = field.Properties.LookupType;
  1890. Type lookuptype = CoreUtils.GetEntityOrNull(type);
  1891. if (lookuptype == typeof(Product))
  1892. {
  1893. var framebutton = tuple.Item2 as FrameButton;
  1894. string[] buttonData = framebutton.Data.Split(',');
  1895. string id = buttonData[1]; //[1] is product id as string
  1896. CoreTable table = new Client<Product>().Query(new Filter<Product>(x => x.ID).IsEqualTo(Guid.Parse(id)), new Columns<Product>(x => x.Image.ID));
  1897. if (table.Rows.Any())
  1898. {
  1899. Guid returnid = table.Rows.FirstOrDefault().Get<Product, Guid>(x => x.Image.ID);
  1900. return returnid;
  1901. }
  1902. else
  1903. return null;
  1904. }
  1905. else
  1906. {
  1907. var button = tuple.Item2 as DataButtonControl;
  1908. Dictionary<string, object> dict = button.ExtraData[button.ChosenID];
  1909. return dict[dataField];
  1910. }
  1911. }
  1912. else
  1913. return null;
  1914. }
  1915. catch
  1916. {
  1917. return null;
  1918. }
  1919. }
  1920. public void SetFieldColour(string field, System.Drawing.Color? colour = null)
  1921. {
  1922. }
  1923. #endregion
  1924. }
  1925. }