QAFormViewer.cs 91 KB

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