QAFormViewer.cs 90 KB

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