QAFormViewer.cs 86 KB

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