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