QAFormViewer.cs 87 KB

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