QAFormViewer.cs 91 KB

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