QAFormViewer.cs 83 KB

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