QAFormViewer.cs 88 KB

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