QAFormViewer.cs 83 KB

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