QAFormViewer.cs 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  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 we have a value, then show the image
  913. // We need a clear button to be able to reset it
  914. if (loadData.TryGetValue(dFLayoutSignaturePad.Name, out value))
  915. {
  916. if (!string.IsNullOrWhiteSpace(value))
  917. {
  918. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture();
  919. byte[] data = Convert.FromBase64String(value);
  920. embeddedImageCapture.DataToImage(data);
  921. view = embeddedImageCapture;
  922. view.IsEnabled = false;
  923. view.HeightRequest = 200;
  924. }
  925. }
  926. else
  927. {
  928. // If we don't have a value, show the signaturepad + utility buttons
  929. SfSignaturePad signaturePad = new SfSignaturePad()
  930. {
  931. MinimumStrokeWidth = 0.2,
  932. MaximumStrokeWidth = 17
  933. };
  934. if (isrequired)
  935. signaturePad.BackgroundColor = isRequiredColor;
  936. signaturePad.SetValue(Grid.ColumnProperty,0);
  937. signaturePad.SetValue(Grid.ColumnSpanProperty,3);
  938. signaturePad.SetValue(Grid.RowProperty,0);
  939. EmbeddedImageCapture hiddenEmbeddedImageCapture = new EmbeddedImageCapture()
  940. {
  941. IsVisible = false,
  942. IsEnabled = false
  943. };
  944. hiddenEmbeddedImageCapture.SetValue(Grid.ColumnProperty,0);
  945. hiddenEmbeddedImageCapture.SetValue(Grid.ColumnSpanProperty,3);
  946. hiddenEmbeddedImageCapture.SetValue(Grid.RowProperty,0);
  947. MobileButton createSignatureButton = new MobileButton()
  948. {
  949. Text = "Save New"
  950. };
  951. createSignatureButton.Clicked += (sender, args) =>
  952. {
  953. SignatureSaver saver = new SignatureSaver();
  954. saver.OnSignatureSaved += () =>
  955. {
  956. Task.Run(() =>
  957. {
  958. hiddenEmbeddedImageCapture.DataToImage((Application.Current.Properties["SavedSignature"]) as byte[]);
  959. });
  960. Thread.Sleep(1000);
  961. signaturePad.IsVisible = false;
  962. hiddenEmbeddedImageCapture.IsVisible = true;
  963. if (!useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  964. useSavedSignatures.Add(dFLayoutSignaturePad.Name);
  965. };
  966. Navigation.PushAsync(saver);
  967. };
  968. createSignatureButton.SetValue(Grid.ColumnProperty,0);
  969. createSignatureButton.SetValue(Grid.RowProperty,1);
  970. MobileButton signNowButton = new MobileButton()
  971. {
  972. Text = "Apply Saved",
  973. };
  974. signNowButton.Clicked += (sender, args) =>
  975. {
  976. if (Application.Current.Properties.ContainsKey("SavedSignature"))
  977. {
  978. Task.Run(() =>
  979. {
  980. hiddenEmbeddedImageCapture.DataToImage((Application.Current.Properties["SavedSignature"]) as byte[]);
  981. });
  982. Thread.Sleep(1000);
  983. signaturePad.IsVisible = false;
  984. hiddenEmbeddedImageCapture.IsVisible = true;
  985. if (!useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  986. useSavedSignatures.Add(dFLayoutSignaturePad.Name);
  987. }
  988. };
  989. signNowButton.SetValue(Grid.ColumnProperty,1);
  990. signNowButton.SetValue(Grid.RowProperty,1);
  991. MobileButton clearButton = new MobileButton()
  992. {
  993. Text = "Clear",
  994. };
  995. clearButton.Clicked += (sender, args) =>
  996. {
  997. signaturePad.IsVisible = true;
  998. hiddenEmbeddedImageCapture.IsVisible = false;
  999. useSavedSignatures.Remove(dFLayoutSignaturePad.Name);
  1000. signaturePad.Clear();
  1001. };
  1002. clearButton.SetValue(Grid.ColumnProperty,2);
  1003. clearButton.SetValue(Grid.RowProperty,1);
  1004. Grid grid = new Grid { Padding = 5, ColumnSpacing = 5 };
  1005. grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(200, GridUnitType.Absolute) });
  1006. grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(40, GridUnitType.Absolute) });
  1007. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1008. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1009. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1010. grid.Children.Add(signaturePad);
  1011. grid.Children.Add(hiddenEmbeddedImageCapture);
  1012. grid.Children.Add(createSignatureButton);
  1013. grid.Children.Add(signNowButton);
  1014. grid.Children.Add(clearButton);
  1015. view = grid;
  1016. }
  1017. return new Tuple<View, Boolean>(view, isrequired);
  1018. }
  1019. private Tuple<View, Boolean> LoadEmbeddedImage(DFLayoutControl element)
  1020. {
  1021. string value = "";
  1022. bool disableLibrary = false;
  1023. DFLayoutEmbeddedImage dfLayoutEmbeddedImage = element as DFLayoutEmbeddedImage;
  1024. try
  1025. {
  1026. disableLibrary = dfLayoutEmbeddedImage.Properties.DisableLibrary;
  1027. }
  1028. catch { }
  1029. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture(disableLibrary);
  1030. if (disableLibrary)
  1031. embeddedImageCapture.LibraryButton.BackgroundColor = Color.LightGray;
  1032. if (loadData.TryGetValue(dfLayoutEmbeddedImage.Name, out value))
  1033. {
  1034. if (!string.IsNullOrWhiteSpace(value))
  1035. {
  1036. byte[] data = Convert.FromBase64String(value);
  1037. embeddedImageCapture.DataToImage(data);
  1038. }
  1039. }
  1040. else
  1041. {
  1042. if (dfLayoutEmbeddedImage.Properties.Required)
  1043. embeddedImageCapture.BackgroundColor = isRequiredColor;
  1044. }
  1045. return new Tuple<View, Boolean>(embeddedImageCapture, dfLayoutEmbeddedImage.Properties.Required);
  1046. }
  1047. private Tuple<View, Boolean> LoadVideoField(DFLayoutControl element)
  1048. {
  1049. string value = "";
  1050. bool disableLibrary = false;
  1051. DFLayoutVideoField vidField = element as DFLayoutVideoField;
  1052. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture(false, true);
  1053. embeddedImageCapture.VideoQuality =
  1054. vidField.Properties.Quality == DFLayoutVideoFieldProperties.VideoQuality.Low ? Plugin.Media.Abstractions.VideoQuality.Low :
  1055. vidField.Properties.Quality == DFLayoutVideoFieldProperties.VideoQuality.Medium ? Plugin.Media.Abstractions.VideoQuality.Medium :
  1056. vidField.Properties.Quality == DFLayoutVideoFieldProperties.VideoQuality.High ? Plugin.Media.Abstractions.VideoQuality.High :
  1057. vidField.Properties.Quality == DFLayoutVideoFieldProperties.VideoQuality.Default ? Plugin.Media.Abstractions.VideoQuality.Medium :
  1058. Plugin.Media.Abstractions.VideoQuality.Medium;
  1059. embeddedImageCapture.VideoLength =
  1060. vidField.Properties.MaximumVideoLength == 0 ? new TimeSpan(0, 0, 20) :
  1061. new TimeSpan(0, 0, vidField.Properties.MaximumVideoLength);
  1062. if (loadData.TryGetValue(vidField.Name, out value))
  1063. {
  1064. byte[] data = Convert.FromBase64String(value);
  1065. }
  1066. else
  1067. {
  1068. if (vidField.Properties.Required)
  1069. embeddedImageCapture.BackgroundColor = isRequiredColor;
  1070. }
  1071. return new Tuple<View, Boolean>(embeddedImageCapture, vidField.Properties.Required);
  1072. }
  1073. private Tuple<View, Boolean> LoadMultiImage(DFLayoutControl element)
  1074. {
  1075. string value = "";
  1076. bool disableLibrary = false;
  1077. DFLayoutMultiImage dfLayoutMultiEmbeddedImage = element as DFLayoutMultiImage;
  1078. try
  1079. {
  1080. disableLibrary = dfLayoutMultiEmbeddedImage.Properties.DisableLibrary;
  1081. }
  1082. catch { }
  1083. MultiImageCapture multiImageCapture = new MultiImageCapture(disableLibrary);
  1084. if (loadData.TryGetValue(dfLayoutMultiEmbeddedImage.Name, out value))
  1085. {
  1086. List<string> values = Serialization.Deserialize<List<string>>(value);
  1087. foreach (string s in values)
  1088. {
  1089. byte[] data = Convert.FromBase64String(s);
  1090. multiImageCapture.DataToImage(data);
  1091. }
  1092. }
  1093. return new Tuple<View, bool>(multiImageCapture, dfLayoutMultiEmbeddedImage.Properties.Required);
  1094. }
  1095. private Tuple<View, bool> LoadMultiSignaturePad(DFLayoutControl element)
  1096. {
  1097. string value = "";
  1098. DFLayoutMultiSignaturePad dfLayoutMultiSignaturePad = element as DFLayoutMultiSignaturePad;
  1099. DataButtonControl button = new DataButtonControl
  1100. {
  1101. Text = "Add Signatures",
  1102. };
  1103. if (loadData.TryGetValue(dfLayoutMultiSignaturePad.Name, out value))
  1104. {
  1105. button.Data = value;
  1106. }
  1107. MultiSignaturePad multiSignaturePad = new MultiSignaturePad(button.Data);
  1108. multiSignaturePad.OnMultiSignatureSaved += (result) =>
  1109. {
  1110. button.Data = result;
  1111. };
  1112. button.Clicked += (object sender, EventArgs e) =>
  1113. {
  1114. Navigation.PushAsync(multiSignaturePad);
  1115. };
  1116. return new Tuple<View, bool>(button, dfLayoutMultiSignaturePad.Properties.Required);
  1117. }
  1118. private Tuple<View, bool> LoadAddTaskField(DFLayoutControl element)
  1119. {
  1120. string value = "";
  1121. DFLayoutAddTaskField field = element as DFLayoutAddTaskField;
  1122. DFCreateTaskView taskView = new DFCreateTaskView();
  1123. taskView.OnAddTaskButtonClicked += () =>
  1124. {
  1125. AddEditTask addEditTask = new AddEditTask(Guid.Empty, "New task from Digital Form");
  1126. if (field.Properties.TaskType != null)
  1127. {
  1128. addEditTask.kanban.Type.ID = field.Properties.TaskType.ID;
  1129. try
  1130. {
  1131. addEditTask.kanban.Type.Description = new Client<KanbanType>().Query
  1132. (new Filter<KanbanType>(x => x.ID).IsEqualTo(field.Properties.TaskType.ID),
  1133. new Columns<KanbanType>(x => x.Description)).Rows.FirstOrDefault().Values[0].ToString();
  1134. }
  1135. catch { }
  1136. addEditTask.UpdateScreen(true);
  1137. }
  1138. addEditTask.OnTaskSaved += (taskNumber) =>
  1139. {
  1140. taskView.DisableButton();
  1141. taskView.TaskNumber = taskNumber.ToString();
  1142. };
  1143. Navigation.PushAsync(addEditTask);
  1144. };
  1145. if (loadData.TryGetValue(field.Name, out value))
  1146. {
  1147. taskView.TaskNumber = value;
  1148. taskView.DisableButton();
  1149. }
  1150. return new Tuple<View, bool>(taskView, field.Properties.Required);
  1151. }
  1152. private void FormatAndAddView(View view, DFLayoutControl element, bool isrequired, bool requiresFrame)
  1153. {
  1154. try
  1155. {
  1156. if (element is DFLayoutField)
  1157. {
  1158. pairs[element as DFLayoutField] = view;
  1159. if (isSecure || readOnly ||
  1160. !string.IsNullOrWhiteSpace((element as DFLayoutField).GetPropertyValue<string>("Expression")))
  1161. view.IsEnabled = false;
  1162. if (requiresFrame)
  1163. {
  1164. MobileCard frame = new MobileCard
  1165. {
  1166. Content = view,
  1167. BorderColor = Color.Gray,
  1168. BackgroundColor = Color.LightYellow,
  1169. CornerRadius = 5,
  1170. Padding = 2,
  1171. HasShadow = false,
  1172. };
  1173. if (readOnly || isSecure)
  1174. {
  1175. frame.BorderColor = Color.Gray;
  1176. frame.BackgroundColor = Color.LightGray;
  1177. frame.VerticalOptions = LayoutOptions.FillAndExpand;
  1178. frame.MinimumHeightRequest = 60;
  1179. }
  1180. if (isrequired)
  1181. {
  1182. frame.BackgroundColor = isRequiredColor;
  1183. }
  1184. view = frame;
  1185. }
  1186. }
  1187. if (element.Row > 0)
  1188. SetRow(view,
  1189. element.Row -
  1190. 1); //rows and columns coming in from the desktop designer start from 1, not 0 (most of the time??)
  1191. else if (element.Row == 0)
  1192. SetRow(view, dfLayout.RowHeights.Count + 1);
  1193. if (element.RowSpan > 0)
  1194. SetRowSpan(view, element.RowSpan);
  1195. else if (element.RowSpan == 0)
  1196. SetRowSpan(view, 1);
  1197. if (element.Column > 0)
  1198. SetColumn(view,
  1199. element.Column -
  1200. 1); //rows and columns coming in from the desktop designer start from 1, not 0 (most of the time??)
  1201. if (element.ColumnSpan > 0)
  1202. SetColumnSpan(view, element.ColumnSpan);
  1203. if (element.ColumnSpan > 1)
  1204. {
  1205. view.HorizontalOptions = LayoutOptions.FillAndExpand;
  1206. if (view is Label)
  1207. {
  1208. view.HorizontalOptions = LayoutOptions.CenterAndExpand;
  1209. Label lblView = view as Label;
  1210. lblView.HorizontalTextAlignment = TextAlignment.Center;
  1211. view = lblView;
  1212. }
  1213. }
  1214. else if (element.ColumnSpan == 0)
  1215. SetColumnSpan(view, 1);
  1216. //view.SetValue(Grid.MarginProperty, 0.01);
  1217. //if (element is DFLayoutSignaturePad)
  1218. // view.Margin = new Thickness(25, 0, 25, 0);
  1219. Device.BeginInvokeOnMainThread(() => { Children.Add(view); });
  1220. if (element is DFLayoutHeader)
  1221. if ((element as DFLayoutHeader).Collapsed)
  1222. headersToCollapse.Add(view as DigitalFormsHeader);
  1223. }
  1224. catch (Exception e)
  1225. {
  1226. MobileLogging.Log(e,"DFLayout");
  1227. }
  1228. }
  1229. #endregion
  1230. #region Save
  1231. public void SaveData(bool saveForLater)
  1232. {
  1233. try
  1234. {
  1235. results.Clear();
  1236. propertyResults.Clear();
  1237. isRequiredEmpty = false;
  1238. isRequiredMessage = "";
  1239. foreach (DFLayoutField field in pairs.Keys)
  1240. {
  1241. View view = pairs[field];
  1242. String userInput = AddResultsToDictionary(field, view);
  1243. if (isRequiredEmpty)
  1244. {
  1245. if (!saveForLater)
  1246. return;
  1247. }
  1248. AddPropertyToDictionary(field, userInput);
  1249. }
  1250. UpdateInstanceAndModel(saveForLater);
  1251. }
  1252. catch (Exception ex)
  1253. {
  1254. }
  1255. }
  1256. #region Adding Results to dictionaries
  1257. private string AddResultsToDictionary(DFLayoutField field, View view, bool AddToDictionary = true) //"results" dictionary is the data to be saved in Form.Data
  1258. {
  1259. string userInput = "";
  1260. if (field is DFLayoutStringField || field is DFLayoutIntegerField || field is DFLayoutDoubleField)
  1261. {
  1262. if (field is DFLayoutStringField && (field as DFLayoutStringField).Properties.PopupEditor)
  1263. userInput = (view as DataButtonControl).Data;
  1264. else
  1265. userInput = (view as Editor).Text;
  1266. }
  1267. else if (field is DFLayoutBooleanField)
  1268. {
  1269. if ((view as CustomBoolean).ValueChanged)
  1270. {
  1271. userInput = (view as CustomBoolean).Value.ToString();
  1272. }
  1273. }
  1274. else if (field is DFLayoutDateField)
  1275. {
  1276. userInput = ((view as DatePicker).Date).ToString("dd-MM-yyyy");
  1277. }
  1278. else if (field is DFLayoutTimeField)
  1279. {
  1280. userInput = ((view as TimePicker).Time).ToString("c");
  1281. }
  1282. else if (field is DFLayoutOptionField)
  1283. {
  1284. userInput = GetStringFromOptionField(view);
  1285. }
  1286. else if (field is DFLayoutLookupField)
  1287. {
  1288. if (view is DataButtonControl)
  1289. {
  1290. var button = view as DataButtonControl;
  1291. userInput = FindIDForDesignLookupField(button); //userinput is used to set guid later on for properties, but will not get added to form data only for lookupfields
  1292. results.Add(field.Name, button.Data);
  1293. if (RetainedResults.IsFormRetained)
  1294. if ((field as DFLayoutLookupField).Properties.Retain)
  1295. RetainedResults.Results.Add(field.Name, button.Data);
  1296. }
  1297. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1298. {
  1299. FrameButton framebutton = view as FrameButton;
  1300. if (!string.IsNullOrWhiteSpace(framebutton.Data))
  1301. {
  1302. string[] buttonData = framebutton.Data.Split(',');
  1303. if (AddToDictionary)
  1304. results.Add(field.Name, buttonData[0]); //[0] is product.code
  1305. userInput = buttonData[1]; //[1] is product id as string
  1306. if (RetainedResults.IsFormRetained)
  1307. if ((field as DFLayoutLookupField).Properties.Retain)
  1308. RetainedResults.Results.Add(field.Name, buttonData[0]);
  1309. }
  1310. }
  1311. }
  1312. else if (field is DFLayoutSignaturePad)
  1313. {
  1314. string value = "";
  1315. var grid = view as Grid;
  1316. var signaturePad = grid.Children.OfType<SfSignaturePad>().FirstOrDefault();
  1317. DFLayoutSignaturePad dFLayoutSignaturePad = field as DFLayoutSignaturePad;
  1318. if (loadData.TryGetValue(dFLayoutSignaturePad.Name, out value))
  1319. {
  1320. userInput = value;
  1321. }
  1322. else
  1323. {
  1324. if (useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  1325. {
  1326. ImageSource src = ImageSource.FromStream(() => new MemoryStream((Application.Current.Properties["SavedSignature"]) as byte[]));
  1327. userInput = ImageSourceToBase64(src);
  1328. }
  1329. else
  1330. {
  1331. signaturePad.Save();
  1332. if (signaturePad.ImageSource != null)
  1333. {
  1334. userInput = ImageSourceToBase64(signaturePad.ImageSource);
  1335. }
  1336. }
  1337. }
  1338. }
  1339. else if (field is DFLayoutEmbeddedImage)
  1340. {
  1341. var embeddedImage = view as EmbeddedImageCapture;
  1342. if (embeddedImage.Image.Source != null)
  1343. {
  1344. try
  1345. {
  1346. userInput = ImageSourceToBase64(embeddedImage.Image.Source);
  1347. }
  1348. catch (Exception e)
  1349. {
  1350. userInput = "";
  1351. }
  1352. }
  1353. }
  1354. else if (field is DFLayoutVideoField)
  1355. {
  1356. var embeddedImage = view as EmbeddedImageCapture;
  1357. if (embeddedImage.bytes != null)
  1358. {
  1359. if (embeddedImage.bytes.Length > 0)
  1360. {
  1361. try
  1362. {
  1363. userInput = BytesToBase64(embeddedImage.bytes);
  1364. }
  1365. catch (Exception e)
  1366. {
  1367. userInput = "";
  1368. }
  1369. }
  1370. }
  1371. }
  1372. else if (field is DFLayoutMultiImage)
  1373. {
  1374. var multiImage = view as MultiImageCapture;
  1375. if (multiImage.Images.Count > 0)
  1376. {
  1377. try
  1378. {
  1379. userInput = ImagesToBase64(multiImage.Images);
  1380. }
  1381. catch (Exception e)
  1382. {
  1383. userInput = "";
  1384. }
  1385. }
  1386. }
  1387. else if (field is DFLayoutAddTaskField)
  1388. {
  1389. var addTaskButton = view as DFCreateTaskView;
  1390. if (!string.IsNullOrWhiteSpace(addTaskButton.TaskNumber))
  1391. {
  1392. userInput = addTaskButton.TaskNumber;
  1393. }
  1394. }
  1395. else if (field is DFLayoutMultiSignaturePad)
  1396. {
  1397. var databutton = view as DataButtonControl;
  1398. if (!string.IsNullOrWhiteSpace(databutton.Data))
  1399. userInput = databutton.Data;
  1400. }
  1401. //add the field name and input to 'results' dictionary for processing outside of QAFormViewer
  1402. if (!String.IsNullOrWhiteSpace(userInput) && AddToDictionary)
  1403. {
  1404. if (field is DFLayoutLookupField)
  1405. { /*do nothing as results have already been added*/ }
  1406. else
  1407. {
  1408. try
  1409. {
  1410. results.Add(field.Name, userInput);
  1411. }
  1412. catch (Exception e)
  1413. {
  1414. errors.Add(e.Message);
  1415. }
  1416. if (RetainedResults.IsFormRetained)
  1417. {
  1418. AddRetainedToDictionary(field, userInput);
  1419. }
  1420. }
  1421. }
  1422. else CheckRequired(field);
  1423. return userInput;
  1424. }
  1425. private void AddPropertyToDictionary(DFLayoutField field, String userInput)
  1426. {
  1427. if (!string.IsNullOrWhiteSpace(userInput))
  1428. {
  1429. try
  1430. {
  1431. string property = "";
  1432. if (field is DFLayoutStringField)
  1433. property = (field as DFLayoutStringField).Properties.Property;
  1434. else if (field is DFLayoutIntegerField)
  1435. property = (field as DFLayoutIntegerField).Properties.Property;
  1436. else if (field is DFLayoutDoubleField)
  1437. property = (field as DFLayoutDoubleField).Properties.Property;
  1438. else if (field is DFLayoutBooleanField)
  1439. property = (field as DFLayoutBooleanField).Properties.Property;
  1440. else if (field is DFLayoutDateField)
  1441. property = (field as DFLayoutDateField).Properties.Property;
  1442. else if (field is DFLayoutTimeField)
  1443. property = (field as DFLayoutTimeField).Properties.Property;
  1444. else if (field is DFLayoutOptionField)
  1445. property = (field as DFLayoutOptionField).Properties.Property;
  1446. else if (field is DFLayoutLookupField)
  1447. property = (field as DFLayoutLookupField).Properties.Property;
  1448. //'propertyResults' dictionary for processing outside of QAFormViewer
  1449. if (!string.IsNullOrWhiteSpace(property)) propertyResults.Add(property, userInput);
  1450. }
  1451. catch (Exception e)
  1452. {
  1453. errors.Add(e.Message);
  1454. return;
  1455. }
  1456. }
  1457. }
  1458. private async void AddRetainedToDictionary(DFLayoutField field, string userInput)
  1459. {
  1460. await Task.Run(() =>
  1461. {
  1462. try
  1463. {
  1464. if (field is DFLayoutStringField)
  1465. {
  1466. if ((field as DFLayoutStringField).Properties.Retain)
  1467. RetainedResults.Results.Add(field.Name, userInput);
  1468. }
  1469. else if (field is DFLayoutIntegerField)
  1470. {
  1471. if ((field as DFLayoutIntegerField).Properties.Retain)
  1472. RetainedResults.Results.Add(field.Name, userInput);
  1473. }
  1474. else if (field is DFLayoutDoubleField)
  1475. {
  1476. if ((field as DFLayoutDoubleField).Properties.Retain)
  1477. RetainedResults.Results.Add(field.Name, userInput);
  1478. }
  1479. else if (field is DFLayoutBooleanField)
  1480. {
  1481. if ((field as DFLayoutBooleanField).Properties.Retain)
  1482. RetainedResults.Results.Add(field.Name, userInput);
  1483. }
  1484. else if (field is DFLayoutDateField)
  1485. {
  1486. if ((field as DFLayoutDateField).Properties.Retain)
  1487. RetainedResults.Results.Add(field.Name, userInput);
  1488. }
  1489. else if (field is DFLayoutTimeField)
  1490. {
  1491. if ((field as DFLayoutTimeField).Properties.Retain)
  1492. RetainedResults.Results.Add(field.Name, userInput);
  1493. }
  1494. else if (field is DFLayoutOptionField)
  1495. {
  1496. if ((field as DFLayoutOptionField).Properties.Retain)
  1497. RetainedResults.Results.Add(field.Name, userInput);
  1498. }
  1499. }
  1500. catch (Exception e)
  1501. {
  1502. errors.Add("Retained results error: " + e.Message);
  1503. }
  1504. });
  1505. }
  1506. #endregion
  1507. #region Checks / Custom methods
  1508. private void CheckRequired(DFLayoutField field)
  1509. {
  1510. if (field is DFLayoutStringField)
  1511. {
  1512. if ((field as DFLayoutStringField).Properties.Required)
  1513. {
  1514. isRequiredEmpty = true;
  1515. isRequiredMessage = (field as DFLayoutStringField).Description;
  1516. }
  1517. }
  1518. else if (field is DFLayoutIntegerField)
  1519. {
  1520. if ((field as DFLayoutIntegerField).Properties.Required)
  1521. {
  1522. isRequiredEmpty = true;
  1523. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  1524. }
  1525. }
  1526. else if (field is DFLayoutDoubleField)
  1527. {
  1528. if ((field as DFLayoutDoubleField).Properties.Required)
  1529. {
  1530. {
  1531. isRequiredEmpty = true;
  1532. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  1533. }
  1534. }
  1535. }
  1536. else if (field is DFLayoutBooleanField)
  1537. {
  1538. if ((field as DFLayoutBooleanField).Properties.Required)
  1539. {
  1540. {
  1541. isRequiredEmpty = true;
  1542. isRequiredMessage = (field as DFLayoutBooleanField).Description;
  1543. }
  1544. }
  1545. }
  1546. else if (field is DFLayoutDateField)
  1547. {
  1548. if ((field as DFLayoutDateField).Properties.Required)
  1549. {
  1550. {
  1551. isRequiredEmpty = true;
  1552. isRequiredMessage = (field as DFLayoutDateField).Description;
  1553. }
  1554. }
  1555. }
  1556. else if (field is DFLayoutTimeField)
  1557. {
  1558. if ((field as DFLayoutTimeField).Properties.Required)
  1559. {
  1560. {
  1561. isRequiredEmpty = true;
  1562. isRequiredMessage = (field as DFLayoutTimeField).Description;
  1563. }
  1564. }
  1565. }
  1566. else if (field is DFLayoutOptionField)
  1567. {
  1568. if ((field as DFLayoutOptionField).Properties.Required)
  1569. {
  1570. {
  1571. isRequiredEmpty = true;
  1572. isRequiredMessage = (field as DFLayoutOptionField).Description;
  1573. }
  1574. }
  1575. }
  1576. else if (field is DFLayoutLookupField)
  1577. {
  1578. if ((field as DFLayoutLookupField).Properties.Required)
  1579. {
  1580. {
  1581. isRequiredEmpty = true;
  1582. isRequiredMessage = (field as DFLayoutLookupField).Description;
  1583. }
  1584. }
  1585. }
  1586. else if (field is DFLayoutSignaturePad)
  1587. {
  1588. if ((field as DFLayoutSignaturePad).Properties.Required)
  1589. {
  1590. {
  1591. isRequiredEmpty = true;
  1592. isRequiredMessage = (field as DFLayoutSignaturePad).Description;
  1593. }
  1594. }
  1595. }
  1596. else if (field is DFLayoutEmbeddedImage)
  1597. {
  1598. if ((field as DFLayoutEmbeddedImage).Properties.Required)
  1599. {
  1600. {
  1601. isRequiredEmpty = true;
  1602. isRequiredMessage = (field as DFLayoutEmbeddedImage).Description;
  1603. }
  1604. }
  1605. }
  1606. else if (field is DFLayoutMultiImage)
  1607. {
  1608. if ((field as DFLayoutMultiImage).Properties.Required)
  1609. {
  1610. {
  1611. isRequiredEmpty = true;
  1612. isRequiredMessage = (field as DFLayoutMultiImage).Description;
  1613. }
  1614. }
  1615. }
  1616. else if (field is DFLayoutMultiSignaturePad)
  1617. {
  1618. if ((field as DFLayoutMultiSignaturePad).Properties.Required)
  1619. {
  1620. {
  1621. isRequiredEmpty = true;
  1622. isRequiredMessage = (field as DFLayoutMultiSignaturePad).Description;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. private string ImagesToBase64(List<Image> images)
  1628. {
  1629. List<string> strings = new List<string>();
  1630. foreach (Image image in images)
  1631. {
  1632. strings.Add(ImageSourceToBase64(image.Source));
  1633. }
  1634. return Serialization.Serialize(strings);
  1635. }
  1636. private string ImageSourceToBase64(ImageSource source)
  1637. {
  1638. StreamImageSource streamImageSource = (StreamImageSource)source;
  1639. System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
  1640. Task<Stream> task = streamImageSource.Stream(cancellationToken);
  1641. Stream stream = task.Result;
  1642. byte[] bytes = new byte[stream.Length];
  1643. stream.Read(bytes, 0, bytes.Length);
  1644. string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  1645. return s;
  1646. }
  1647. private string BytesToBase64(byte[] bytes)
  1648. {
  1649. string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  1650. return s;
  1651. }
  1652. private string FindIDForDesignLookupField(DataButtonControl button)
  1653. {
  1654. try
  1655. {
  1656. string userInputValue = button.Data;
  1657. string guidString = "";
  1658. if (userInputValue != nullOrInvalidType)
  1659. {
  1660. foreach (KeyValuePair<DFLayoutLookupField, Dictionary<Guid, string>> pair in dfLayoutLookupFieldLookupOptions)
  1661. {
  1662. foreach (KeyValuePair<Guid, string> innerPair in pair.Value)
  1663. {
  1664. if (innerPair.Value == userInputValue)
  1665. {
  1666. guidString = innerPair.Key.ToString();
  1667. }
  1668. }
  1669. }
  1670. }
  1671. return guidString;
  1672. }
  1673. catch
  1674. {
  1675. return "";
  1676. }
  1677. }
  1678. #endregion
  1679. #region Save Completion
  1680. //point of determination for whether or not QA form gets completed or saved for later
  1681. private void UpdateInstanceAndModel(bool saveForLater)
  1682. {
  1683. if (results.Count != 0)
  1684. {
  1685. if (!saveForLater)
  1686. {
  1687. CompleteForm();
  1688. }
  1689. model.Instance.FormCompletedBy.ID = userID;
  1690. if (digitalFormID != Guid.Empty) model.Instance.Form.ID = digitalFormID;
  1691. DigitalForm.SerializeFormData(model.Instance, QueryVariables(model.Instance), results.ToDictionary(x => x.Key, x => x.Value as object));
  1692. FormData = model.Instance.FormData;
  1693. if (model.Instance.FormStarted == DateTime.MinValue)
  1694. {
  1695. model.Instance.FormStarted = timeStarted;
  1696. }
  1697. TimeSpan span = DateTime.Now - timeStarted;
  1698. model.Instance.FormOpen = model.Instance.FormOpen + span;
  1699. model.Update(null);
  1700. }
  1701. }
  1702. public List<DigitalFormVariable> QueryVariables(IDigitalFormInstance form)
  1703. {
  1704. List<DigitalFormVariable> variables = new List<DigitalFormVariable>();
  1705. var table = new Client<DigitalFormVariable>().Query(
  1706. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(form.Form.ID),
  1707. new Columns<DigitalFormVariable>(x => x.Code, x => x.Parameters, x => x.Description, x => x.VariableType),
  1708. null
  1709. );
  1710. foreach (CoreRow coreRow in table.Rows)
  1711. {
  1712. var variable = coreRow.ToObject<DigitalFormVariable>();
  1713. variables.Add(variable);
  1714. }
  1715. return variables;
  1716. }
  1717. private void CompleteForm()
  1718. {
  1719. model.Instance.FormCompleted = DateTime.Now;
  1720. FormCompleted = model.Instance.FormCompleted;
  1721. model.Instance.Location.Longitude = location.Longitude;
  1722. model.Instance.Location.Latitude = location.Latitude;
  1723. model.Instance.Location.Timestamp = model.Instance.FormCompleted;
  1724. }
  1725. #endregion
  1726. #endregion
  1727. #region Expressions
  1728. public object GetFieldValue(string field)
  1729. {
  1730. try
  1731. {
  1732. Tuple<DFLayoutField, View> tuple = FindView(field);
  1733. return GetValueFromView(tuple.Item1, tuple.Item2);
  1734. }
  1735. catch
  1736. {
  1737. return "";
  1738. }
  1739. }
  1740. public void SetFieldValue(string field, object value)
  1741. {
  1742. try
  1743. {
  1744. Tuple<DFLayoutField, View> tuple = FindView(field);
  1745. SetValueOfView(tuple.Item1, tuple.Item2, value);
  1746. }
  1747. catch { }
  1748. }
  1749. private void SetValueOfView(DFLayoutField field, View view, object value)
  1750. {
  1751. if (field is DFLayoutBooleanField)
  1752. (view as CustomBoolean).Value = (bool)value;
  1753. else if (field is DFLayoutStringField || field is DFLayoutIntegerField || field is DFLayoutDoubleField)
  1754. {
  1755. if (field is DFLayoutStringField && (field as DFLayoutStringField).Properties.PopupEditor)
  1756. {
  1757. (view as DataButtonControl).Data = value.ToString();
  1758. (view as DataButtonControl).Text = value.ToString();
  1759. }
  1760. else
  1761. (view as Editor).Text = value.ToString();
  1762. }
  1763. else if (field is DFLayoutDateField)
  1764. (view as DatePicker).Date = (DateTime)(value as DateTime?);
  1765. else if (field is DFLayoutDateTimeField)
  1766. (view as TimePicker).Time = (TimeSpan)(value as TimeSpan?);
  1767. else if (field is DFLayoutOptionField)
  1768. AssignPickerOption(view, field as DFLayoutOptionField, value as string);
  1769. else if (field is DFLayoutLookupField)
  1770. AssignLookupOption(view, value as string);
  1771. else if (field is DFLayoutEmbeddedImage)
  1772. AssignImage(view, value as byte[]);
  1773. }
  1774. private void AssignImage(View view, byte[] bytes)
  1775. {
  1776. var imageCapture = view as EmbeddedImageCapture;
  1777. imageCapture.DataToImage(bytes);
  1778. }
  1779. private void AssignLookupOption(View view, string value)
  1780. {
  1781. if (view is DataButtonControl)
  1782. {
  1783. (view as DataButtonControl).Text = value;
  1784. (view as DataButtonControl).Data = value;
  1785. }
  1786. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1787. {
  1788. (view as FrameButton).SetButtonText(value);
  1789. (view as FrameButton).Data = value + "," + Guid.Empty.ToString();
  1790. }
  1791. }
  1792. private Tuple<DFLayoutField, View> FindView(string field)
  1793. {
  1794. DFLayoutField dflayoutfield = pairs.Keys.FirstOrDefault(x => x.Name == field);
  1795. View view = pairs[dflayoutfield];
  1796. return new Tuple<DFLayoutField, View>(dflayoutfield, view);
  1797. }
  1798. private void AssignPickerOption(View view, DFLayoutOptionField field, string value)
  1799. {
  1800. string s = field.Properties.Options;
  1801. string[] substrings = s.Split(',');
  1802. var optionList = substrings.ToList();
  1803. if (Device.RuntimePlatform == Device.iOS)
  1804. {
  1805. var iOSPicker = view as CustomPickeriOS;
  1806. iOSPicker.SelectedIndex = optionList.IndexOf(value);
  1807. }
  1808. else
  1809. {
  1810. var picker = view as Picker;
  1811. picker.SelectedIndex = optionList.IndexOf(value);
  1812. }
  1813. }
  1814. private object GetValueFromView(DFLayoutField field, View view)
  1815. {
  1816. if (field is DFLayoutBooleanField)
  1817. return (view as CustomBoolean).Value;
  1818. if (field is DFLayoutStringField)
  1819. {
  1820. if ((field as DFLayoutStringField).Properties.PopupEditor)
  1821. return (view as DataButtonControl).Data;
  1822. else
  1823. return (view as Editor).Text;
  1824. }
  1825. else if (field is DFLayoutIntegerField)
  1826. return int.Parse((view as Editor).Text);
  1827. else if (field is DFLayoutDoubleField)
  1828. return double.Parse((view as Editor).Text);
  1829. else if (field is DFLayoutDateField)
  1830. return (view as DatePicker).Date;
  1831. else if (field is DFLayoutDateTimeField)
  1832. return (view as TimePicker).Time;
  1833. else if (field is DFLayoutOptionField)
  1834. return GetStringFromOptionField(view);
  1835. else if (field is DFLayoutLookupField)
  1836. return GetStringFromLookupField(view);
  1837. else if (field is DFLayoutSignaturePad)
  1838. return GetByteArrayFromSignaturePad(view, field.Name);
  1839. else if (field is DFLayoutEmbeddedImage)
  1840. return GetByteArrayFromEmbeddedImage(view);
  1841. else if (field is DFLayoutMultiImage)
  1842. return GetByteArrayListFromMultiImage(view);
  1843. else return "";
  1844. }
  1845. private object GetByteArrayListFromMultiImage(View view)
  1846. {
  1847. List<byte[]> list = new List<byte[]>();
  1848. var multiImage = view as MultiImageCapture;
  1849. if (multiImage.Images.Count > 0)
  1850. {
  1851. foreach (Image image in multiImage.Images)
  1852. {
  1853. string base64 = ImageSourceToBase64(image.Source);
  1854. list.Add(Convert.FromBase64String(base64));
  1855. }
  1856. }
  1857. return list;
  1858. }
  1859. private object GetByteArrayFromEmbeddedImage(View view)
  1860. {
  1861. var embeddedImage = view as EmbeddedImageCapture;
  1862. if (embeddedImage.Image.Source != null)
  1863. {
  1864. string base64 = ImageSourceToBase64(embeddedImage.Image.Source);
  1865. return Convert.FromBase64String(base64);
  1866. }
  1867. else
  1868. return null;
  1869. }
  1870. private byte[] GetByteArrayFromSignaturePad(View view, string name)
  1871. {
  1872. var grid = view as Grid;
  1873. var signaturePad = grid.Children.OfType<SfSignaturePad>().FirstOrDefault();
  1874. if (useSavedSignatures.Contains(name))
  1875. {
  1876. return Application.Current.Properties["SavedSignature"] as byte[];
  1877. }
  1878. else
  1879. {
  1880. signaturePad.Save();
  1881. if (signaturePad.ImageSource != null)
  1882. {
  1883. string base64 = ImageSourceToBase64(signaturePad.ImageSource);
  1884. return Convert.FromBase64String(base64);
  1885. }
  1886. else
  1887. return null;
  1888. }
  1889. }
  1890. private object GetStringFromLookupField(View view)
  1891. {
  1892. string value = "";
  1893. if (view is DataButtonControl)
  1894. {
  1895. var button = view as DataButtonControl;
  1896. value = FindIDForDesignLookupField(button); //userinput is used to set guid later on for properties, but will not get added to form data only for lookupfields
  1897. }
  1898. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1899. {
  1900. FrameButton framebutton = view as FrameButton;
  1901. if (!string.IsNullOrWhiteSpace(framebutton.Data))
  1902. {
  1903. string[] buttonData = framebutton.Data.Split(',');
  1904. value = buttonData[1]; //[1] is product id as string
  1905. }
  1906. }
  1907. return value;
  1908. }
  1909. private string GetStringFromOptionField(View view)
  1910. {
  1911. if (view is MobileTabStrip strip)
  1912. return strip.SelectedItem?.Text ?? "";
  1913. return "";
  1914. }
  1915. public object GetFieldData(string fieldName, string dataField)
  1916. {
  1917. Tuple<DFLayoutField, View> tuple = FindView(fieldName);
  1918. try
  1919. {
  1920. if (tuple.Item1 is DFLayoutLookupField)
  1921. {
  1922. DFLayoutLookupField field = tuple.Item1 as DFLayoutLookupField;
  1923. string type = field.Properties.LookupType;
  1924. Type lookuptype = CoreUtils.GetEntityOrNull(type);
  1925. if (lookuptype == typeof(Product))
  1926. {
  1927. var framebutton = tuple.Item2 as FrameButton;
  1928. string[] buttonData = framebutton.Data.Split(',');
  1929. string id = buttonData[1]; //[1] is product id as string
  1930. CoreTable table = new Client<Product>().Query(new Filter<Product>(x => x.ID).IsEqualTo(Guid.Parse(id)), new Columns<Product>(x => x.Image.ID));
  1931. if (table.Rows.Any())
  1932. {
  1933. Guid returnid = table.Rows.FirstOrDefault().Get<Product, Guid>(x => x.Image.ID);
  1934. return returnid;
  1935. }
  1936. else
  1937. return null;
  1938. }
  1939. else
  1940. {
  1941. var button = tuple.Item2 as DataButtonControl;
  1942. Dictionary<string, object> dict = button.ExtraData[button.ChosenID];
  1943. return dict[dataField];
  1944. }
  1945. }
  1946. else
  1947. return null;
  1948. }
  1949. catch
  1950. {
  1951. return null;
  1952. }
  1953. }
  1954. public void SetFieldColour(string field, System.Drawing.Color? colour = null)
  1955. {
  1956. if (colour != null)
  1957. {
  1958. try
  1959. {
  1960. System.Drawing.Color color = (System.Drawing.Color)colour;
  1961. FindView(field).Item2.BackgroundColor = Color.FromRgba(
  1962. Convert.ToDouble(color.R),
  1963. Convert.ToDouble(color.G),
  1964. Convert.ToDouble(color.B),
  1965. Convert.ToDouble(color.A));
  1966. }
  1967. catch { }
  1968. }
  1969. }
  1970. #endregion
  1971. }
  1972. }