QAFormViewer.cs 77 KB

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