QAFormViewer.cs 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  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. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading Form"))
  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. var isrequired = false;
  552. DFLayoutOptionField dfLayoutOptionField = element as DFLayoutOptionField;
  553. isrequired = dfLayoutOptionField.Properties.Required;
  554. if (GlobalVariables.DeviceString == "i" || GlobalVariables.DeviceString == "I")
  555. {
  556. CustomPickeriOS item = new CustomPickeriOS();
  557. string s = dfLayoutOptionField.Properties.Options;
  558. string[] substrings = s.Split(',');
  559. var optionList = substrings.ToList();
  560. item.AddItems(optionList);
  561. if (!string.IsNullOrEmpty(dfLayoutOptionField.Properties.Default))
  562. {
  563. int index = optionList.IndexOf(dfLayoutOptionField.Properties.Default);
  564. item.SelectedIndex = index;
  565. item.SelectedItem = optionList[index];
  566. item.SetDefault(optionList[index]);
  567. }
  568. if (loadData.TryGetValue(dfLayoutOptionField.Name, out value))
  569. {
  570. int index = optionList.IndexOf(value);
  571. item.SelectedIndex = index;
  572. item.SelectedItem = optionList[index];
  573. item.SetDefault(optionList[index]);
  574. }
  575. if (loadRetainedForm)
  576. {
  577. if (RetainedResults.Results.TryGetValue(dfLayoutOptionField.Name, out value))
  578. {
  579. int index = optionList.IndexOf(value);
  580. item.SelectedIndex = index;
  581. item.SelectedItem = optionList[index];
  582. item.SetDefault(optionList[index]);
  583. }
  584. }
  585. if (isrequired)
  586. {
  587. item.BackgroundColor = isRequiredColor;
  588. item.SetBackGroundColor(isRequiredColor);
  589. }
  590. item.CustomPickeriOSValueChanged += () =>
  591. {
  592. dfLayout.ChangeField(dfLayoutOptionField.Name);
  593. };
  594. view = item;
  595. }
  596. else
  597. {
  598. Picker picker = new Picker();
  599. picker.Title = "Select an option";
  600. picker.VerticalTextAlignment = TextAlignment.Center;
  601. picker.HorizontalTextAlignment = TextAlignment.Center;
  602. string s = dfLayoutOptionField.Properties.Options;
  603. string[] substrings = s.Split(',');
  604. var optionList = substrings.ToList();
  605. picker.ItemsSource = optionList;
  606. if (!string.IsNullOrEmpty(dfLayoutOptionField.Properties.Default))
  607. {
  608. int index = optionList.IndexOf(dfLayoutOptionField.Properties.Default);
  609. picker.SelectedIndex = index;
  610. picker.SelectedItem = optionList[index];
  611. }
  612. if (loadData.TryGetValue(dfLayoutOptionField.Name, out value))
  613. {
  614. int index = optionList.IndexOf(value);
  615. picker.SelectedIndex = index;
  616. picker.SelectedItem = optionList[index];
  617. }
  618. if (loadRetainedForm)
  619. {
  620. if (RetainedResults.Results.TryGetValue(dfLayoutOptionField.Name, out value))
  621. {
  622. int index = optionList.IndexOf(value);
  623. picker.SelectedIndex = index;
  624. picker.SelectedItem = optionList[index];
  625. }
  626. }
  627. if (isrequired)
  628. {
  629. picker.BackgroundColor = isRequiredColor;
  630. }
  631. picker.SelectedIndexChanged += (object sender, EventArgs e) =>
  632. {
  633. dfLayout.ChangeField(dfLayoutOptionField.Name);
  634. };
  635. view = picker;
  636. }
  637. return new Tuple<View, Boolean>(view, isrequired);
  638. }
  639. private Tuple<View, Boolean> LoadImage(View view, DFLayoutControl element)
  640. {
  641. DFLayoutImage dfLayoutImage = element as DFLayoutImage;
  642. Image img = new Image();
  643. CoreTable table = new Client<Document>().Query(
  644. new Filter<Document>(x => x.ID).IsEqualTo(dfLayoutImage.Image.ID),
  645. null,
  646. null);
  647. if (table.Rows.Any())
  648. {
  649. CoreRow row = table.Rows.FirstOrDefault();
  650. byte[] data = row.Get<Document, byte[]>(x => x.Data);
  651. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  652. img.MinimumHeightRequest = 150;
  653. img.MinimumWidthRequest = 150;
  654. img.Aspect = Aspect.AspectFit;
  655. img.Source = src;
  656. img.HorizontalOptions = LayoutOptions.FillAndExpand;
  657. img.VerticalOptions = LayoutOptions.FillAndExpand;
  658. img.GestureRecognizers.Add(new TapGestureRecognizer
  659. {
  660. Command = new Command(OnTap),
  661. CommandParameter = src,
  662. NumberOfTapsRequired = 1
  663. });
  664. if (img != null)
  665. {
  666. view = img;
  667. }
  668. }
  669. return new Tuple<View, Boolean>(view, false);
  670. }
  671. private void OnTap(object obj)
  672. {
  673. ImageViewer viewer = new ImageViewer(obj as ImageSource);
  674. Navigation.PushAsync(viewer);
  675. }
  676. private Tuple<View, Boolean> LoadLookupPicker(View view, DFLayoutControl element, string value)
  677. {
  678. DFLayoutLookupField dfLayoutLookupField = element as DFLayoutLookupField;
  679. var isrequired = dfLayoutLookupField.Properties.Required;
  680. Dictionary<Guid, string> lookupFieldOptions = new Dictionary<Guid, string>();
  681. string type = dfLayoutLookupField.Properties.LookupType;
  682. Type lookuptype = CoreUtils.GetEntityOrNull(type);
  683. if (lookuptype == typeof(DrawingTemplate))
  684. view = LoadDrawingTemplateLookupButton(lookuptype, dfLayoutLookupField, lookupFieldOptions, element);
  685. else if (!string.IsNullOrWhiteSpace(dfLayoutLookupField.Properties.Filter))
  686. view = LoadGenericLookupButton(lookuptype, dfLayoutLookupField, lookupFieldOptions, element);
  687. else if (lookuptype == typeof(Product))
  688. view = LoadProductLookupPicker(view, dfLayoutLookupField, value);
  689. else if (lookuptype != null)
  690. view = LoadGenericLookupButton(lookuptype, dfLayoutLookupField, lookupFieldOptions, element);
  691. else if (lookuptype == null)
  692. view = new Label { Text = "Null lookup selected", HorizontalOptions = LayoutOptions.Center, HorizontalTextAlignment = TextAlignment.Center };
  693. return new Tuple<View, Boolean>(view, isrequired);
  694. }
  695. private View LoadGenericLookupButton(Type lookuptype, DFLayoutLookupField dfLayoutLookupField, Dictionary<Guid, string> lookupFieldOptions, DFLayoutControl element)
  696. {
  697. string value = "";
  698. CoreTable table = new CoreTable();
  699. var client = ClientFactory.CreateClient(lookuptype);
  700. var columns = LookupFactory.DefineColumns(lookuptype);
  701. var sort = LookupFactory.DefineSort(lookuptype);
  702. var filter = LookupFactory.DefineFilter(lookuptype);
  703. foreach (var property in dfLayoutLookupField.Properties.AdditionalPropertiesList)
  704. {
  705. columns.Add(property);
  706. }
  707. if (!string.IsNullOrWhiteSpace(dfLayoutLookupField.Properties.Filter))
  708. filter = ReturnFilter(dfLayoutLookupField, lookuptype);
  709. if (lookuptype == typeof(ProductStyle) && JobID != Guid.Empty)
  710. table = ReturnJobStyleProducts(table, client, columns, sort);
  711. if (table.Rows.Count == 0)
  712. table = client.Query(filter, columns, sort); // do generic query if a specific one hasn't already been done
  713. Dictionary<Guid, Dictionary<string, object>> additionalPropertyValues = new Dictionary<Guid, Dictionary<string, object>>();
  714. foreach (var row in table.Rows)
  715. {
  716. Guid id = row.Get<Guid>("ID");
  717. Dictionary<string, object> values = row.ToDictionary(new string[] { "ID" });
  718. additionalPropertyValues.Add(id, values);
  719. string display = LookupFactory.FormatLookup(lookuptype, values, new string[] { });
  720. lookupFieldOptions[id] = display;
  721. }
  722. dfLayoutLookupFieldLookupOptions[element as DFLayoutLookupField] = lookupFieldOptions;
  723. List<string> itemList = lookupFieldOptions.Values.ToList();
  724. DataButtonControl button = new DataButtonControl()
  725. {
  726. Text = "Choose Option",
  727. HorizontalOptions = LayoutOptions.FillAndExpand,
  728. VerticalOptions = LayoutOptions.Center,
  729. Margin = 0,
  730. BackgroundColor = Color.FromHex("#15C7C1"),
  731. FontAttributes = FontAttributes.Bold,
  732. TextColor = Color.White,
  733. CornerRadius = 5,
  734. Padding = 1,
  735. ExtraData = additionalPropertyValues,
  736. LookupFieldOptions = lookupFieldOptions
  737. };
  738. button.Clicked += (object sender, EventArgs eventArgs) =>
  739. {
  740. ListSelectionPage selectionPage = new ListSelectionPage(lookupFieldOptions);
  741. selectionPage.OnSimpleListTapped += (s) =>
  742. {
  743. button.Data = s;
  744. button.Text = s;
  745. dfLayout.ChangeField(dfLayoutLookupField.Name);
  746. };
  747. selectionPage.OnDictionaryItemTapped += (id, s) =>
  748. {
  749. button.Data = s;
  750. button.Text = s;
  751. button.ChosenID = id;
  752. dfLayout.ChangeField(dfLayoutLookupField.Name);
  753. };
  754. Navigation.PushAsync(selectionPage);
  755. };
  756. if (loadData.TryGetValue(dfLayoutLookupField.Name, out value))
  757. {
  758. button.Data = value;
  759. button.Text = value;
  760. }
  761. if (loadRetainedForm)
  762. {
  763. if (RetainedResults.Results.TryGetValue(dfLayoutLookupField.Name, out value))
  764. {
  765. button.Data = value;
  766. button.Text = value;
  767. }
  768. }
  769. if (isRequired)
  770. {
  771. button.BackgroundColor = isRequiredColor;
  772. }
  773. return button;
  774. }
  775. private View LoadDrawingTemplateLookupButton(Type lookuptype, DFLayoutLookupField dfLayoutLookupField, Dictionary<Guid, string> lookupFieldOptions, DFLayoutControl element)
  776. {
  777. string value = "";
  778. var columns = new Columns<DrawingTemplate>(x => x.Code, x => x.Document.ID);
  779. CoreTable table = new Client<DrawingTemplate>().Query(null, columns);
  780. Dictionary<string, Guid> dictionary = new Dictionary<string, Guid>();
  781. foreach (var row in table.Rows)
  782. {
  783. List<object> list = row.Values;
  784. if (list[0] != null && list[1] != null)
  785. dictionary.Add(list[0].ToString(), Guid.Parse(list[1].ToString()));
  786. }
  787. DataButtonControl button = new DataButtonControl()
  788. {
  789. Text = "Choose Option",
  790. HorizontalOptions = LayoutOptions.FillAndExpand,
  791. VerticalOptions = LayoutOptions.Center,
  792. Margin = 0,
  793. BackgroundColor = Color.FromHex("#15C7C1"),
  794. FontAttributes = FontAttributes.Bold,
  795. TextColor = Color.White,
  796. CornerRadius = 5,
  797. Padding = 1,
  798. };
  799. button.Clicked += (object sender, EventArgs eventArgs) =>
  800. {
  801. ListSelectionPage selectionPage = new ListSelectionPage(dictionary);
  802. selectionPage.OnSimpleListTapped += (s) =>
  803. {
  804. button.Data = s;
  805. button.Text = s;
  806. dfLayout.ChangeField(dfLayoutLookupField.Name);
  807. };
  808. Navigation.PushAsync(selectionPage);
  809. };
  810. if (loadData.TryGetValue(dfLayoutLookupField.Name, out value))
  811. {
  812. button.Data = value;
  813. button.Text = value;
  814. }
  815. if (loadRetainedForm)
  816. {
  817. if (RetainedResults.Results.TryGetValue(dfLayoutLookupField.Name, out value))
  818. {
  819. button.Data = value;
  820. button.Text = value;
  821. }
  822. }
  823. if (isRequired)
  824. {
  825. button.BackgroundColor = isRequiredColor;
  826. }
  827. return button;
  828. }
  829. private IFilter ReturnFilter(DFLayoutLookupField dfLayoutLookupField, Type lookuptype)
  830. {
  831. var propertyFilter = dfLayoutLookupField.Properties.Filter;
  832. var filtertype = typeof(Filter<>).MakeGenericType(lookuptype);
  833. var deserialised = Serialization.Deserialize(filtertype, propertyFilter);
  834. IFilter filter = deserialised as IFilter;
  835. return filter;
  836. }
  837. private CoreTable ReturnJobStyleProducts(CoreTable table, IClient client, IColumns columns, ISortOrder sort)
  838. {
  839. CoreTable jobStyleTable = new Client<JobStyle>().Query(new Filter<JobStyle>(x => x.Job.ID).IsEqualTo(JobID),
  840. new Columns<JobStyle>(x => x.Style.ID));
  841. if (jobStyleTable.Rows.Any())
  842. {
  843. Guid firstID = Guid.Parse(jobStyleTable.Rows.FirstOrDefault().Values[0].ToString());
  844. var stylefilter = new Filter<ProductStyle>(x => x.ID).IsEqualTo(firstID);
  845. foreach (CoreRow jobStyleRow in jobStyleTable.Rows)
  846. {
  847. if (jobStyleRow != jobStyleTable.Rows.FirstOrDefault())
  848. {
  849. stylefilter = stylefilter.Or(x => x.ID).IsEqualTo(Guid.Parse(jobStyleRow.Values[0].ToString()));
  850. }
  851. }
  852. table = client.Query(stylefilter, columns, sort);
  853. }
  854. return table;
  855. }
  856. private View LoadProductLookupPicker(View view, DFLayoutLookupField dfLayoutLookupField, string value)
  857. {
  858. FrameButton framebutton = new FrameButton();
  859. if (loadData.TryGetValue(dfLayoutLookupField.Name, out value))
  860. {
  861. framebutton.SetButtonText(value);
  862. framebutton.Data = value + "," + (GlobalVariables.ProductShells.FirstOrDefault(x => x.Code == value)).ID.ToString();
  863. //CoreTable productTable = new Client<Product>().Query(new Filter<Product>(x => x.Code).IsEqualTo(value),
  864. // new Columns<Product>(x => x.Image.ID));
  865. //if (productTable.Rows.Any())
  866. //{
  867. // CoreTable imageTable1 = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(productTable.Rows.First().Values[0].ToString())),
  868. // new Columns<Document>(x => x.Data));
  869. // CoreRow docrow = imageTable1.Rows.FirstOrDefault();
  870. // if (docrow != null)
  871. // {
  872. // byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  873. // ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  874. // if (src != null)
  875. // {
  876. // framebutton.SetImage(src);
  877. // }
  878. // }
  879. //}
  880. }
  881. framebutton.OnFrameButtonClicked += (() =>
  882. {
  883. ProductList productList = new ProductList(GlobalVariables.ProductShells, true);
  884. productList.OnProductSelected += () =>
  885. {
  886. framebutton.SetButtonText(productList.SelectedProduct.Code);
  887. framebutton.Data = productList.SelectedProduct.Code + "," + productList.SelectedProduct.ID.ToString();
  888. //CoreTable imageTable = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(productList.SelectedProduct.ImageID),
  889. // new Columns<Document>(x => x.Data));
  890. //CoreRow docrow = imageTable.Rows.FirstOrDefault();
  891. //if (docrow != null)
  892. //{
  893. // byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  894. // ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  895. // if (src != null)
  896. // {
  897. // framebutton.SetImage(src);
  898. // }
  899. //}
  900. dfLayout.ChangeField(dfLayoutLookupField.Name);
  901. };
  902. Navigation.PushAsync(productList);
  903. });
  904. view = framebutton;
  905. return view;
  906. }
  907. private Tuple<View, Boolean> LoadSignaturePad(View view, DFLayoutControl element, string value)
  908. {
  909. DFLayoutSignaturePad dFLayoutSignaturePad = element as DFLayoutSignaturePad;
  910. var isrequired = dFLayoutSignaturePad.Properties.Required;
  911. if (loadData.TryGetValue(dFLayoutSignaturePad.Name, out value))
  912. {
  913. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture();
  914. byte[] data = Convert.FromBase64String(value);
  915. embeddedImageCapture.DataToImage(data);
  916. view = embeddedImageCapture;
  917. view.IsEnabled = false;
  918. }
  919. else
  920. {
  921. SfSignaturePad signaturePad = new SfSignaturePad();
  922. EmbeddedImageCapture hiddenEmbeddedImageCapture = new EmbeddedImageCapture() { HeightRequest = 200, IsVisible = false, IsEnabled = false };
  923. if (isrequired) signaturePad.BackgroundColor = isRequiredColor;
  924. signaturePad.MinimumHeightRequest = 200;
  925. signaturePad.MinimumStrokeWidth = 0.2;
  926. signaturePad.MaximumStrokeWidth = 17;
  927. Button clearButton = new Button()
  928. {
  929. Text = "Clear",
  930. HorizontalOptions = LayoutOptions.FillAndExpand,
  931. VerticalOptions = LayoutOptions.End,
  932. Margin = 0,
  933. BackgroundColor = Color.FromHex("#15C7C1"),
  934. FontAttributes = FontAttributes.Bold,
  935. TextColor = Color.White,
  936. CornerRadius = 5,
  937. Padding = 1
  938. };
  939. clearButton.Clicked += (object sender, EventArgs e) =>
  940. {
  941. signaturePad.IsVisible = true;
  942. hiddenEmbeddedImageCapture.IsVisible = false;
  943. useSavedSignatures.Remove(dFLayoutSignaturePad.Name);
  944. signaturePad.Clear();
  945. };
  946. Grid.SetColumn(clearButton, 2);
  947. Button signNowButton = new Button()
  948. {
  949. Text = "Apply Saved",
  950. HorizontalOptions = LayoutOptions.FillAndExpand,
  951. VerticalOptions = LayoutOptions.End,
  952. Margin = 0,
  953. BackgroundColor = Color.FromHex("#15C7C1"),
  954. FontAttributes = FontAttributes.Bold,
  955. TextColor = Color.White,
  956. CornerRadius = 5,
  957. Padding = 1
  958. };
  959. signNowButton.Clicked += (object sender, EventArgs e) =>
  960. {
  961. if (Application.Current.Properties.ContainsKey("SavedSignature"))
  962. {
  963. Task.Run(() =>
  964. {
  965. hiddenEmbeddedImageCapture.DataToImage((Application.Current.Properties["SavedSignature"]) as byte[]);
  966. });
  967. Thread.Sleep(1000);
  968. signaturePad.IsVisible = false;
  969. hiddenEmbeddedImageCapture.IsVisible = true;
  970. if (!useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  971. useSavedSignatures.Add(dFLayoutSignaturePad.Name);
  972. }
  973. };
  974. Grid.SetColumn(signNowButton, 1);
  975. Button createSignatureButton = new Button()
  976. {
  977. Text = "Save New",
  978. HorizontalOptions = LayoutOptions.FillAndExpand,
  979. VerticalOptions = LayoutOptions.End,
  980. Margin = 0,
  981. BackgroundColor = Color.FromHex("#15C7C1"),
  982. FontAttributes = FontAttributes.Bold,
  983. TextColor = Color.White,
  984. CornerRadius = 5,
  985. Padding = 1
  986. };
  987. createSignatureButton.Clicked += (object sender, EventArgs e) =>
  988. {
  989. SignatureSaver saver = new SignatureSaver();
  990. saver.OnSignatureSaved += () =>
  991. {
  992. Task.Run(() =>
  993. {
  994. hiddenEmbeddedImageCapture.DataToImage((Application.Current.Properties["SavedSignature"]) as byte[]);
  995. });
  996. Thread.Sleep(1000);
  997. signaturePad.IsVisible = false;
  998. hiddenEmbeddedImageCapture.IsVisible = true;
  999. if (!useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  1000. useSavedSignatures.Add(dFLayoutSignaturePad.Name);
  1001. };
  1002. Navigation.PushAsync(saver);
  1003. };
  1004. Grid.SetColumn(createSignatureButton, 0);
  1005. Grid buttonsGrid = new Grid { Margin = 0, Padding = 0, VerticalOptions = LayoutOptions.End };
  1006. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1007. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1008. buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  1009. buttonsGrid.Children.Add(createSignatureButton);
  1010. buttonsGrid.Children.Add(signNowButton);
  1011. buttonsGrid.Children.Add(clearButton);
  1012. Grid grid = new Grid() { Margin = 0, Padding = 0 };
  1013. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(200, GridUnitType.Absolute) });
  1014. Grid.SetRow(signaturePad, 0);
  1015. Grid.SetRow(hiddenEmbeddedImageCapture, 0);
  1016. grid.Children.Add(hiddenEmbeddedImageCapture);
  1017. grid.Children.Add(signaturePad);
  1018. grid.Children.Add(buttonsGrid);
  1019. view = grid;
  1020. }
  1021. return new Tuple<View, Boolean>(view, isrequired);
  1022. }
  1023. private Tuple<View, Boolean> LoadEmbeddedImage(View view, DFLayoutControl element, string value)
  1024. {
  1025. bool disableLibrary = false;
  1026. DFLayoutEmbeddedImage dfLayoutEmbeddedImage = element as DFLayoutEmbeddedImage;
  1027. try
  1028. {
  1029. disableLibrary = dfLayoutEmbeddedImage.Properties.DisableLibrary;
  1030. }
  1031. catch { }
  1032. EmbeddedImageCapture embeddedImageCapture = new EmbeddedImageCapture(disableLibrary);
  1033. if (disableLibrary)
  1034. embeddedImageCapture.LibraryButton.BackgroundColor = Color.LightGray;
  1035. if (loadData.TryGetValue(dfLayoutEmbeddedImage.Name, out value))
  1036. {
  1037. byte[] data = Convert.FromBase64String(value);
  1038. embeddedImageCapture.DataToImage(data);
  1039. }
  1040. else
  1041. {
  1042. if (dfLayoutEmbeddedImage.Properties.Required)
  1043. {
  1044. embeddedImageCapture.CameraButton.BackgroundColor = isRequiredColor;
  1045. embeddedImageCapture.CameraButton.BorderWidth = 1;
  1046. embeddedImageCapture.CameraButton.BorderColor = Color.Black;
  1047. if (!disableLibrary)
  1048. {
  1049. embeddedImageCapture.LibraryButton.BackgroundColor = isRequiredColor;
  1050. embeddedImageCapture.LibraryButton.BorderWidth = 1;
  1051. embeddedImageCapture.LibraryButton.BorderColor = Color.Black;
  1052. }
  1053. }
  1054. }
  1055. view = embeddedImageCapture;
  1056. return new Tuple<View, Boolean>(view, dfLayoutEmbeddedImage.Properties.Required);
  1057. }
  1058. private Tuple<View, Boolean> LoadMultiImage(View view, DFLayoutControl element, string value)
  1059. {
  1060. bool disableLibrary = false;
  1061. DFLayoutMultiImage dfLayoutMultiEmbeddedImage = element as DFLayoutMultiImage;
  1062. try
  1063. {
  1064. disableLibrary = dfLayoutMultiEmbeddedImage.Properties.DisableLibrary;
  1065. }
  1066. catch { }
  1067. MultiImageCapture multiImageCapture = new MultiImageCapture(disableLibrary);
  1068. if (loadData.TryGetValue(dfLayoutMultiEmbeddedImage.Name, out value))
  1069. {
  1070. List<string> values = Serialization.Deserialize<List<string>>(value);
  1071. foreach (string s in values)
  1072. {
  1073. byte[] data = Convert.FromBase64String(s);
  1074. multiImageCapture.DataToImage(data);
  1075. }
  1076. }
  1077. view = multiImageCapture;
  1078. return new Tuple<View, bool>(view, dfLayoutMultiEmbeddedImage.Properties.Required);
  1079. }
  1080. private Tuple<View, bool> LoadMultiSignaturePad(View view, DFLayoutControl element, string value)
  1081. {
  1082. DFLayoutMultiSignaturePad dfLayoutMultiSignaturePad = element as DFLayoutMultiSignaturePad;
  1083. DataButtonControl button = new DataButtonControl
  1084. {
  1085. Text = "Add Signatures",
  1086. TextColor = Color.White,
  1087. HorizontalOptions = LayoutOptions.FillAndExpand,
  1088. CornerRadius = 0,
  1089. BackgroundColor = Color.FromHex("#15C7C1"),
  1090. FontAttributes = FontAttributes.Bold
  1091. };
  1092. if (loadData.TryGetValue(dfLayoutMultiSignaturePad.Name, out value))
  1093. {
  1094. button.Data = value;
  1095. }
  1096. MultiSignaturePad multiSignaturePad = new MultiSignaturePad(button.Data);
  1097. multiSignaturePad.OnMultiSignatureSaved += (result) =>
  1098. {
  1099. button.Data = result;
  1100. };
  1101. button.Clicked += (object sender, EventArgs e) =>
  1102. {
  1103. Navigation.PushAsync(multiSignaturePad);
  1104. };
  1105. view = button;
  1106. return new Tuple<View, bool>(view, dfLayoutMultiSignaturePad.Properties.Required);
  1107. }
  1108. private Tuple<View, bool> LoadAddTaskField(View view, DFLayoutControl element, string value)
  1109. {
  1110. DFLayoutAddTaskField field = element as DFLayoutAddTaskField;
  1111. DFCreateTaskView taskView = new DFCreateTaskView();
  1112. taskView.OnAddTaskButtonClicked += () =>
  1113. {
  1114. AddEditTask addEditTask = new AddEditTask(Guid.Empty, "New task from Digital Form");
  1115. if (field.Properties.TaskType != null)
  1116. {
  1117. addEditTask.kanban.Type.ID = field.Properties.TaskType.ID;
  1118. try
  1119. {
  1120. addEditTask.kanban.Type.Description = new Client<KanbanType>().Query
  1121. (new Filter<KanbanType>(x => x.ID).IsEqualTo(field.Properties.TaskType.ID),
  1122. new Columns<KanbanType>(x => x.Description)).Rows.FirstOrDefault().Values[0].ToString();
  1123. }
  1124. catch { }
  1125. addEditTask.UpdateScreen(true);
  1126. }
  1127. addEditTask.OnTaskSaved += (taskNumber) =>
  1128. {
  1129. taskView.DisableButton();
  1130. taskView.TaskNumber = taskNumber.ToString();
  1131. };
  1132. Navigation.PushAsync(addEditTask);
  1133. };
  1134. if (loadData.TryGetValue(field.Name, out value))
  1135. {
  1136. taskView.TaskNumber = value;
  1137. taskView.DisableButton();
  1138. }
  1139. return new Tuple<View, bool>(taskView, field.Properties.Required);
  1140. }
  1141. private void FormatAndAddView(View view, DFLayoutControl element, bool isrequired)
  1142. {
  1143. try
  1144. {
  1145. if (element is DFLayoutField)
  1146. {
  1147. pairs[element as DFLayoutField] = view;
  1148. if (isSecure || readOnly || !string.IsNullOrWhiteSpace((element as DFLayoutField).GetPropertyValue<string>("Expression")))
  1149. view.IsEnabled = false;
  1150. Frame frame = new Frame
  1151. {
  1152. Content = view,
  1153. BorderColor = Color.FromHex("#15C7C1"),
  1154. BackgroundColor = Color.Default,
  1155. CornerRadius = 5,
  1156. Padding = 2,
  1157. HasShadow = false,
  1158. };
  1159. if (readOnly || isSecure)
  1160. {
  1161. frame.BorderColor = Color.Gray;
  1162. frame.BackgroundColor = Color.LightGray;
  1163. frame.VerticalOptions = LayoutOptions.FillAndExpand;
  1164. frame.MinimumHeightRequest = 60;
  1165. }
  1166. if (isrequired)
  1167. {
  1168. frame.BackgroundColor = isRequiredColor;
  1169. }
  1170. view = frame;
  1171. }
  1172. 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??)
  1173. else if (element.Row == 0) SetRow(view, dfLayout.RowHeights.Count + 1);
  1174. if (element.RowSpan > 0) SetRowSpan(view, element.RowSpan);
  1175. else if (element.RowSpan == 0) SetRowSpan(view, 1);
  1176. 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??)
  1177. if (element.ColumnSpan > 0) SetColumnSpan(view, element.ColumnSpan);
  1178. if (element.ColumnSpan > 1)
  1179. {
  1180. view.HorizontalOptions = LayoutOptions.FillAndExpand;
  1181. if (view is Label)
  1182. {
  1183. view.HorizontalOptions = LayoutOptions.CenterAndExpand;
  1184. Label lblView = view as Label;
  1185. lblView.HorizontalTextAlignment = TextAlignment.Center;
  1186. view = lblView;
  1187. }
  1188. }
  1189. else if (element.ColumnSpan == 0) SetColumnSpan(view, 1);
  1190. view.SetValue(Grid.MarginProperty, 0.01);
  1191. if (element is DFLayoutSignaturePad)
  1192. {
  1193. view.Margin = new Thickness(25, 0, 25, 0);
  1194. }
  1195. Device.BeginInvokeOnMainThread(() => { Children.Add(view); });
  1196. if (element is DFLayoutHeader)
  1197. if ((element as DFLayoutHeader).Collapsed)
  1198. headersToCollapse.Add(view as DigitalFormsHeader);
  1199. }
  1200. catch
  1201. { }
  1202. }
  1203. #endregion
  1204. #region Save
  1205. public void SaveData(bool saveForLater)
  1206. {
  1207. try
  1208. {
  1209. results.Clear();
  1210. propertyResults.Clear();
  1211. isRequiredEmpty = false;
  1212. isRequiredMessage = "";
  1213. foreach (DFLayoutField field in pairs.Keys)
  1214. {
  1215. View view = pairs[field];
  1216. String userInput = AddResultsToDictionary(field, view);
  1217. if (isRequiredEmpty)
  1218. {
  1219. if (!saveForLater)
  1220. return;
  1221. }
  1222. AddPropertyToDictionary(field, userInput);
  1223. }
  1224. UpdateInstanceAndModel(saveForLater);
  1225. }
  1226. catch (Exception ex)
  1227. {
  1228. errors.Add(ex.Message);
  1229. }
  1230. }
  1231. #region Adding Results to dictionaries
  1232. private string AddResultsToDictionary(DFLayoutField field, View view, bool AddToDictionary = true) //"results" dictionary is the data to be saved in Form.Data
  1233. {
  1234. string userInput = "";
  1235. if (field is DFLayoutStringField || field is DFLayoutIntegerField || field is DFLayoutDoubleField)
  1236. {
  1237. userInput = (view as Editor).Text;
  1238. }
  1239. else if (field is DFLayoutBooleanField)
  1240. {
  1241. if ((view as CustomBoolean).ValueChanged)
  1242. {
  1243. userInput = (view as CustomBoolean).Value.ToString();
  1244. }
  1245. }
  1246. else if (field is DFLayoutDateField)
  1247. {
  1248. userInput = ((view as DatePicker).Date).ToString("dd-MM-yyyy");
  1249. }
  1250. else if (field is DFLayoutTimeField)
  1251. {
  1252. userInput = ((view as TimePicker).Time).ToString("c");
  1253. }
  1254. else if (field is DFLayoutOptionField)
  1255. {
  1256. userInput = GetStringFromOptionField(view);
  1257. }
  1258. else if (field is DFLayoutLookupField)
  1259. {
  1260. if (view is DataButtonControl)
  1261. {
  1262. var button = view as DataButtonControl;
  1263. userInput = FindIDForDesignLookupField(button); //userinput is used to set guid later on for properties, but will not get added to form data only for lookupfields
  1264. results.Add(field.Name, button.Data);
  1265. if (RetainedResults.IsFormRetained)
  1266. if ((field as DFLayoutLookupField).Properties.Retain)
  1267. RetainedResults.Results.Add(field.Name, button.Data);
  1268. }
  1269. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1270. {
  1271. FrameButton framebutton = view as FrameButton;
  1272. if (!string.IsNullOrWhiteSpace(framebutton.Data))
  1273. {
  1274. string[] buttonData = framebutton.Data.Split(',');
  1275. if (AddToDictionary)
  1276. results.Add(field.Name, buttonData[0]); //[0] is product.code
  1277. userInput = buttonData[1]; //[1] is product id as string
  1278. if (RetainedResults.IsFormRetained)
  1279. if ((field as DFLayoutLookupField).Properties.Retain)
  1280. RetainedResults.Results.Add(field.Name, buttonData[0]);
  1281. }
  1282. }
  1283. }
  1284. else if (field is DFLayoutSignaturePad)
  1285. {
  1286. string value = "";
  1287. var grid = view as Grid;
  1288. var signaturePad = grid.Children.OfType<SfSignaturePad>().FirstOrDefault();
  1289. DFLayoutSignaturePad dFLayoutSignaturePad = field as DFLayoutSignaturePad;
  1290. if (loadData.TryGetValue(dFLayoutSignaturePad.Name, out value))
  1291. {
  1292. userInput = value;
  1293. }
  1294. else
  1295. {
  1296. if (useSavedSignatures.Contains(dFLayoutSignaturePad.Name))
  1297. {
  1298. ImageSource src = ImageSource.FromStream(() => new MemoryStream((Application.Current.Properties["SavedSignature"]) as byte[]));
  1299. userInput = ImageSourceToBase64(src);
  1300. }
  1301. else
  1302. {
  1303. signaturePad.Save();
  1304. if (signaturePad.ImageSource != null)
  1305. {
  1306. userInput = ImageSourceToBase64(signaturePad.ImageSource);
  1307. }
  1308. }
  1309. }
  1310. }
  1311. else if (field is DFLayoutEmbeddedImage)
  1312. {
  1313. var embeddedImage = view as EmbeddedImageCapture;
  1314. if (embeddedImage.Image.Source != null)
  1315. {
  1316. try
  1317. {
  1318. userInput = ImageSourceToBase64(embeddedImage.Image.Source);
  1319. }
  1320. catch (Exception e)
  1321. {
  1322. userInput = "";
  1323. }
  1324. }
  1325. }
  1326. else if (field is DFLayoutMultiImage)
  1327. {
  1328. var multiImage = view as MultiImageCapture;
  1329. if (multiImage.Images.Count > 0)
  1330. {
  1331. try
  1332. {
  1333. userInput = ImagesToBase64(multiImage.Images);
  1334. }
  1335. catch (Exception e)
  1336. {
  1337. userInput = "";
  1338. }
  1339. }
  1340. }
  1341. else if (field is DFLayoutAddTaskField)
  1342. {
  1343. var addTaskButton = view as DFCreateTaskView;
  1344. if (!string.IsNullOrWhiteSpace(addTaskButton.TaskNumber))
  1345. {
  1346. userInput = addTaskButton.TaskNumber;
  1347. }
  1348. }
  1349. else if (field is DFLayoutMultiSignaturePad)
  1350. {
  1351. var databutton = view as DataButtonControl;
  1352. if (!string.IsNullOrWhiteSpace(databutton.Data))
  1353. userInput = databutton.Data;
  1354. }
  1355. //add the field name and input to 'results' dictionary for processing outside of QAFormViewer
  1356. if (!String.IsNullOrWhiteSpace(userInput) && AddToDictionary)
  1357. {
  1358. if (field is DFLayoutLookupField)
  1359. { /*do nothing as results have already been added*/ }
  1360. else
  1361. {
  1362. try
  1363. {
  1364. results.Add(field.Name, userInput);
  1365. }
  1366. catch (Exception e)
  1367. {
  1368. errors.Add(e.Message);
  1369. }
  1370. if (RetainedResults.IsFormRetained)
  1371. {
  1372. AddRetainedToDictionary(field, userInput);
  1373. }
  1374. }
  1375. }
  1376. else CheckRequired(field);
  1377. return userInput;
  1378. }
  1379. private void AddPropertyToDictionary(DFLayoutField field, String userInput)
  1380. {
  1381. if (!string.IsNullOrWhiteSpace(userInput))
  1382. {
  1383. try
  1384. {
  1385. string property = "";
  1386. if (field is DFLayoutStringField)
  1387. property = (field as DFLayoutStringField).Properties.Property;
  1388. else if (field is DFLayoutIntegerField)
  1389. property = (field as DFLayoutIntegerField).Properties.Property;
  1390. else if (field is DFLayoutDoubleField)
  1391. property = (field as DFLayoutDoubleField).Properties.Property;
  1392. else if (field is DFLayoutBooleanField)
  1393. property = (field as DFLayoutBooleanField).Properties.Property;
  1394. else if (field is DFLayoutDateField)
  1395. property = (field as DFLayoutDateField).Properties.Property;
  1396. else if (field is DFLayoutTimeField)
  1397. property = (field as DFLayoutTimeField).Properties.Property;
  1398. else if (field is DFLayoutOptionField)
  1399. property = (field as DFLayoutOptionField).Properties.Property;
  1400. else if (field is DFLayoutLookupField)
  1401. property = (field as DFLayoutLookupField).Properties.Property;
  1402. //'propertyResults' dictionary for processing outside of QAFormViewer
  1403. if (!string.IsNullOrWhiteSpace(property)) propertyResults.Add(property, userInput);
  1404. }
  1405. catch (Exception e)
  1406. {
  1407. errors.Add(e.Message);
  1408. return;
  1409. }
  1410. }
  1411. }
  1412. private async void AddRetainedToDictionary(DFLayoutField field, string userInput)
  1413. {
  1414. await Task.Run(() =>
  1415. {
  1416. try
  1417. {
  1418. if (field is DFLayoutStringField)
  1419. {
  1420. if ((field as DFLayoutStringField).Properties.Retain)
  1421. RetainedResults.Results.Add(field.Name, userInput);
  1422. }
  1423. else if (field is DFLayoutIntegerField)
  1424. {
  1425. if ((field as DFLayoutIntegerField).Properties.Retain)
  1426. RetainedResults.Results.Add(field.Name, userInput);
  1427. }
  1428. else if (field is DFLayoutDoubleField)
  1429. {
  1430. if ((field as DFLayoutDoubleField).Properties.Retain)
  1431. RetainedResults.Results.Add(field.Name, userInput);
  1432. }
  1433. else if (field is DFLayoutBooleanField)
  1434. {
  1435. if ((field as DFLayoutBooleanField).Properties.Retain)
  1436. RetainedResults.Results.Add(field.Name, userInput);
  1437. }
  1438. else if (field is DFLayoutDateField)
  1439. {
  1440. if ((field as DFLayoutDateField).Properties.Retain)
  1441. RetainedResults.Results.Add(field.Name, userInput);
  1442. }
  1443. else if (field is DFLayoutTimeField)
  1444. {
  1445. if ((field as DFLayoutTimeField).Properties.Retain)
  1446. RetainedResults.Results.Add(field.Name, userInput);
  1447. }
  1448. else if (field is DFLayoutOptionField)
  1449. {
  1450. if ((field as DFLayoutOptionField).Properties.Retain)
  1451. RetainedResults.Results.Add(field.Name, userInput);
  1452. }
  1453. }
  1454. catch (Exception e)
  1455. {
  1456. errors.Add("Retained results error: " + e.Message);
  1457. }
  1458. });
  1459. }
  1460. #endregion
  1461. #region Checks / Custom methods
  1462. private void CheckRequired(DFLayoutField field)
  1463. {
  1464. if (field is DFLayoutStringField)
  1465. {
  1466. if ((field as DFLayoutStringField).Properties.Required)
  1467. {
  1468. isRequiredEmpty = true;
  1469. isRequiredMessage = (field as DFLayoutStringField).Description;
  1470. }
  1471. }
  1472. else if (field is DFLayoutIntegerField)
  1473. {
  1474. if ((field as DFLayoutIntegerField).Properties.Required)
  1475. {
  1476. isRequiredEmpty = true;
  1477. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  1478. }
  1479. }
  1480. else if (field is DFLayoutDoubleField)
  1481. {
  1482. if ((field as DFLayoutDoubleField).Properties.Required)
  1483. {
  1484. {
  1485. isRequiredEmpty = true;
  1486. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  1487. }
  1488. }
  1489. }
  1490. else if (field is DFLayoutBooleanField)
  1491. {
  1492. if ((field as DFLayoutBooleanField).Properties.Required)
  1493. {
  1494. {
  1495. isRequiredEmpty = true;
  1496. isRequiredMessage = (field as DFLayoutBooleanField).Description;
  1497. }
  1498. }
  1499. }
  1500. else if (field is DFLayoutDateField)
  1501. {
  1502. if ((field as DFLayoutDateField).Properties.Required)
  1503. {
  1504. {
  1505. isRequiredEmpty = true;
  1506. isRequiredMessage = (field as DFLayoutDateField).Description;
  1507. }
  1508. }
  1509. }
  1510. else if (field is DFLayoutTimeField)
  1511. {
  1512. if ((field as DFLayoutTimeField).Properties.Required)
  1513. {
  1514. {
  1515. isRequiredEmpty = true;
  1516. isRequiredMessage = (field as DFLayoutTimeField).Description;
  1517. }
  1518. }
  1519. }
  1520. else if (field is DFLayoutOptionField)
  1521. {
  1522. if ((field as DFLayoutOptionField).Properties.Required)
  1523. {
  1524. {
  1525. isRequiredEmpty = true;
  1526. isRequiredMessage = (field as DFLayoutOptionField).Description;
  1527. }
  1528. }
  1529. }
  1530. else if (field is DFLayoutLookupField)
  1531. {
  1532. if ((field as DFLayoutLookupField).Properties.Required)
  1533. {
  1534. {
  1535. isRequiredEmpty = true;
  1536. isRequiredMessage = (field as DFLayoutLookupField).Description;
  1537. }
  1538. }
  1539. }
  1540. else if (field is DFLayoutSignaturePad)
  1541. {
  1542. if ((field as DFLayoutSignaturePad).Properties.Required)
  1543. {
  1544. {
  1545. isRequiredEmpty = true;
  1546. isRequiredMessage = (field as DFLayoutSignaturePad).Description;
  1547. }
  1548. }
  1549. }
  1550. else if (field is DFLayoutEmbeddedImage)
  1551. {
  1552. if ((field as DFLayoutEmbeddedImage).Properties.Required)
  1553. {
  1554. {
  1555. isRequiredEmpty = true;
  1556. isRequiredMessage = (field as DFLayoutEmbeddedImage).Description;
  1557. }
  1558. }
  1559. }
  1560. else if (field is DFLayoutMultiImage)
  1561. {
  1562. if ((field as DFLayoutMultiImage).Properties.Required)
  1563. {
  1564. {
  1565. isRequiredEmpty = true;
  1566. isRequiredMessage = (field as DFLayoutMultiImage).Description;
  1567. }
  1568. }
  1569. }
  1570. else if (field is DFLayoutMultiSignaturePad)
  1571. {
  1572. if ((field as DFLayoutMultiSignaturePad).Properties.Required)
  1573. {
  1574. {
  1575. isRequiredEmpty = true;
  1576. isRequiredMessage = (field as DFLayoutMultiSignaturePad).Description;
  1577. }
  1578. }
  1579. }
  1580. }
  1581. private string ImagesToBase64(List<Image> images)
  1582. {
  1583. List<string> strings = new List<string>();
  1584. foreach (Image image in images)
  1585. {
  1586. strings.Add(ImageSourceToBase64(image.Source));
  1587. }
  1588. return Serialization.Serialize(strings);
  1589. }
  1590. private string ImageSourceToBase64(ImageSource source)
  1591. {
  1592. StreamImageSource streamImageSource = (StreamImageSource)source;
  1593. System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
  1594. Task<Stream> task = streamImageSource.Stream(cancellationToken);
  1595. Stream stream = task.Result;
  1596. byte[] bytes = new byte[stream.Length];
  1597. stream.Read(bytes, 0, bytes.Length);
  1598. string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  1599. return s;
  1600. }
  1601. private string FindIDForDesignLookupField(DataButtonControl button)
  1602. {
  1603. try
  1604. {
  1605. string userInputValue = button.Data;
  1606. string guidString = "";
  1607. if (userInputValue != nullOrInvalidType)
  1608. {
  1609. foreach (KeyValuePair<DFLayoutLookupField, Dictionary<Guid, string>> pair in dfLayoutLookupFieldLookupOptions)
  1610. {
  1611. foreach (KeyValuePair<Guid, string> innerPair in pair.Value)
  1612. {
  1613. if (innerPair.Value == userInputValue)
  1614. {
  1615. guidString = innerPair.Key.ToString();
  1616. }
  1617. }
  1618. }
  1619. }
  1620. return guidString;
  1621. }
  1622. catch
  1623. {
  1624. return "";
  1625. }
  1626. }
  1627. #endregion
  1628. #region Save Completion
  1629. //point of determination for whether or not QA form gets completed or saved for later
  1630. private void UpdateInstanceAndModel(bool saveForLater)
  1631. {
  1632. if (results.Count != 0)
  1633. {
  1634. if (!saveForLater)
  1635. {
  1636. CompleteForm();
  1637. }
  1638. model.Instance.FormCompletedBy.ID = userID;
  1639. if (digitalFormID != Guid.Empty) model.Instance.Form.ID = digitalFormID;
  1640. DigitalForm.SerializeFormData(model.Instance, QueryVariables(model.Instance), results.ToDictionary(x => x.Key, x => x.Value as object));
  1641. FormData = model.Instance.FormData;
  1642. if (model.Instance.FormStarted == DateTime.MinValue)
  1643. {
  1644. model.Instance.FormStarted = timeStarted;
  1645. }
  1646. TimeSpan span = DateTime.Now - timeStarted;
  1647. model.Instance.FormOpen = model.Instance.FormOpen + span;
  1648. model.Update(null);
  1649. }
  1650. }
  1651. public List<DigitalFormVariable> QueryVariables(IDigitalFormInstance form)
  1652. {
  1653. List<DigitalFormVariable> variables = new List<DigitalFormVariable>();
  1654. var table = new Client<DigitalFormVariable>().Query(
  1655. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(form.Form.ID),
  1656. new Columns<DigitalFormVariable>(x => x.Code, x => x.Parameters, x => x.Description, x => x.VariableType),
  1657. null
  1658. );
  1659. foreach (CoreRow coreRow in table.Rows)
  1660. {
  1661. var variable = coreRow.ToObject<DigitalFormVariable>();
  1662. variables.Add(variable);
  1663. }
  1664. return variables;
  1665. }
  1666. private void CompleteForm()
  1667. {
  1668. model.Instance.FormCompleted = DateTime.Now;
  1669. FormCompleted = model.Instance.FormCompleted;
  1670. model.Instance.Location.Longitude = location.Longitude;
  1671. model.Instance.Location.Latitude = location.Latitude;
  1672. model.Instance.Location.Timestamp = model.Instance.FormCompleted;
  1673. }
  1674. #endregion
  1675. #endregion
  1676. #region Expressions
  1677. public object GetFieldValue(string field)
  1678. {
  1679. try
  1680. {
  1681. Tuple<DFLayoutField, View> tuple = FindView(field);
  1682. return GetValueFromView(tuple.Item1, tuple.Item2);
  1683. }
  1684. catch
  1685. {
  1686. return "";
  1687. }
  1688. }
  1689. public void SetFieldValue(string field, object value)
  1690. {
  1691. try
  1692. {
  1693. Tuple<DFLayoutField, View> tuple = FindView(field);
  1694. SetValueOfView(tuple.Item1, tuple.Item2, value);
  1695. }
  1696. catch { }
  1697. }
  1698. private void SetValueOfView(DFLayoutField field, View view, object value)
  1699. {
  1700. if (field is DFLayoutBooleanField)
  1701. (view as CustomBoolean).Value = (bool)value;
  1702. else if (field is DFLayoutStringField || field is DFLayoutIntegerField || field is DFLayoutDoubleField)
  1703. (view as Editor).Text = value.ToString();
  1704. else if (field is DFLayoutDateField)
  1705. (view as DatePicker).Date = (DateTime)(value as DateTime?);
  1706. else if (field is DFLayoutDateTimeField)
  1707. (view as TimePicker).Time = (TimeSpan)(value as TimeSpan?);
  1708. else if (field is DFLayoutOptionField)
  1709. AssignPickerOption(view, field as DFLayoutOptionField, value as string);
  1710. else if (field is DFLayoutLookupField)
  1711. AssignLookupOption(view, value as string);
  1712. else if (field is DFLayoutEmbeddedImage)
  1713. AssignImage(view, value as byte[]);
  1714. }
  1715. private void AssignImage(View view, byte[] bytes)
  1716. {
  1717. var imageCapture = view as EmbeddedImageCapture;
  1718. imageCapture.DataToImage(bytes);
  1719. }
  1720. private void AssignLookupOption(View view, string value)
  1721. {
  1722. if (view is DataButtonControl)
  1723. {
  1724. (view as DataButtonControl).Text = value;
  1725. (view as DataButtonControl).Data = value;
  1726. }
  1727. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1728. {
  1729. (view as FrameButton).SetButtonText(value);
  1730. (view as FrameButton).Data = value + "," + Guid.Empty.ToString();
  1731. }
  1732. }
  1733. private Tuple<DFLayoutField, View> FindView(string field)
  1734. {
  1735. DFLayoutField dflayoutfield = pairs.Keys.FirstOrDefault(x => x.Name == field);
  1736. View view = pairs[dflayoutfield];
  1737. return new Tuple<DFLayoutField, View>(dflayoutfield, view);
  1738. }
  1739. private void AssignPickerOption(View view, DFLayoutOptionField field, string value)
  1740. {
  1741. string s = field.Properties.Options;
  1742. string[] substrings = s.Split(',');
  1743. var optionList = substrings.ToList();
  1744. if (GlobalVariables.DeviceString == "i" || GlobalVariables.DeviceString == "I")
  1745. {
  1746. var iOSPicker = view as CustomPickeriOS;
  1747. iOSPicker.SelectedIndex = optionList.IndexOf(value);
  1748. }
  1749. else
  1750. {
  1751. var picker = view as Picker;
  1752. picker.SelectedIndex = optionList.IndexOf(value);
  1753. }
  1754. }
  1755. private object GetValueFromView(DFLayoutField field, View view)
  1756. {
  1757. if (field is DFLayoutBooleanField)
  1758. return (view as CustomBoolean).Value;
  1759. if (field is DFLayoutStringField)
  1760. return (view as Editor).Text;
  1761. else if (field is DFLayoutIntegerField)
  1762. return int.Parse((view as Editor).Text);
  1763. else if (field is DFLayoutDoubleField)
  1764. return double.Parse((view as Editor).Text);
  1765. else if (field is DFLayoutDateField)
  1766. return (view as DatePicker).Date;
  1767. else if (field is DFLayoutDateTimeField)
  1768. return (view as TimePicker).Time;
  1769. else if (field is DFLayoutOptionField)
  1770. return GetStringFromOptionField(view);
  1771. else if (field is DFLayoutLookupField)
  1772. return GetStringFromLookupField(view);
  1773. else if (field is DFLayoutSignaturePad)
  1774. return GetByteArrayFromSignaturePad(view, field.Name);
  1775. else if (field is DFLayoutEmbeddedImage)
  1776. return GetByteArrayFromEmbeddedImage(view);
  1777. else if (field is DFLayoutMultiImage)
  1778. return GetByteArrayListFromMultiImage(view);
  1779. else return "";
  1780. }
  1781. private object GetByteArrayListFromMultiImage(View view)
  1782. {
  1783. List<byte[]> list = new List<byte[]>();
  1784. var multiImage = view as MultiImageCapture;
  1785. if (multiImage.Images.Count > 0)
  1786. {
  1787. foreach (Image image in multiImage.Images)
  1788. {
  1789. string base64 = ImageSourceToBase64(image.Source);
  1790. list.Add(Convert.FromBase64String(base64));
  1791. }
  1792. }
  1793. return list;
  1794. }
  1795. private object GetByteArrayFromEmbeddedImage(View view)
  1796. {
  1797. var embeddedImage = view as EmbeddedImageCapture;
  1798. if (embeddedImage.Image.Source != null)
  1799. {
  1800. string base64 = ImageSourceToBase64(embeddedImage.Image.Source);
  1801. return Convert.FromBase64String(base64);
  1802. }
  1803. else
  1804. return null;
  1805. }
  1806. private byte[] GetByteArrayFromSignaturePad(View view, string name)
  1807. {
  1808. var grid = view as Grid;
  1809. var signaturePad = grid.Children.OfType<SfSignaturePad>().FirstOrDefault();
  1810. if (useSavedSignatures.Contains(name))
  1811. {
  1812. return Application.Current.Properties["SavedSignature"] as byte[];
  1813. }
  1814. else
  1815. {
  1816. signaturePad.Save();
  1817. if (signaturePad.ImageSource != null)
  1818. {
  1819. string base64 = ImageSourceToBase64(signaturePad.ImageSource);
  1820. return Convert.FromBase64String(base64);
  1821. }
  1822. else
  1823. return null;
  1824. }
  1825. }
  1826. private object GetStringFromLookupField(View view)
  1827. {
  1828. string value = "";
  1829. if (view is DataButtonControl)
  1830. {
  1831. var button = view as DataButtonControl;
  1832. value = FindIDForDesignLookupField(button); //userinput is used to set guid later on for properties, but will not get added to form data only for lookupfields
  1833. }
  1834. else if (view is FrameButton) //currently only used for Products - we already have the Product.ID in Framebutton.Data (saved as Code,ID)
  1835. {
  1836. FrameButton framebutton = view as FrameButton;
  1837. if (!string.IsNullOrWhiteSpace(framebutton.Data))
  1838. {
  1839. string[] buttonData = framebutton.Data.Split(',');
  1840. value = buttonData[1]; //[1] is product id as string
  1841. }
  1842. }
  1843. return value;
  1844. }
  1845. private string GetStringFromOptionField(View view)
  1846. {
  1847. if (GlobalVariables.DeviceString == "i" || GlobalVariables.DeviceString == "I")
  1848. {
  1849. var iOSPicker = view as CustomPickeriOS;
  1850. if (iOSPicker.SelectedIndex != -1)
  1851. {
  1852. return iOSPicker.SelectedItem.ToString();
  1853. }
  1854. else
  1855. return "";
  1856. }
  1857. else
  1858. {
  1859. var picker = view as Picker;
  1860. if (picker.SelectedIndex != -1)
  1861. {
  1862. return picker.SelectedItem.ToString();
  1863. }
  1864. else
  1865. return "";
  1866. }
  1867. }
  1868. public object GetFieldData(string fieldName, string dataField)
  1869. {
  1870. Tuple<DFLayoutField, View> tuple = FindView(fieldName);
  1871. try
  1872. {
  1873. if (tuple.Item1 is DFLayoutLookupField)
  1874. {
  1875. DFLayoutLookupField field = tuple.Item1 as DFLayoutLookupField;
  1876. string type = field.Properties.LookupType;
  1877. Type lookuptype = CoreUtils.GetEntityOrNull(type);
  1878. if (lookuptype == typeof(Product))
  1879. {
  1880. var framebutton = tuple.Item2 as FrameButton;
  1881. string[] buttonData = framebutton.Data.Split(',');
  1882. string id = buttonData[1]; //[1] is product id as string
  1883. CoreTable table = new Client<Product>().Query(new Filter<Product>(x => x.ID).IsEqualTo(Guid.Parse(id)), new Columns<Product>(x => x.Image.ID));
  1884. if (table.Rows.Any())
  1885. {
  1886. Guid returnid = table.Rows.FirstOrDefault().Get<Product, Guid>(x => x.Image.ID);
  1887. return returnid;
  1888. }
  1889. else
  1890. return null;
  1891. }
  1892. else
  1893. {
  1894. var button = tuple.Item2 as DataButtonControl;
  1895. Dictionary<string, object> dict = button.ExtraData[button.ChosenID];
  1896. return dict[dataField];
  1897. }
  1898. }
  1899. else
  1900. return null;
  1901. }
  1902. catch
  1903. {
  1904. return null;
  1905. }
  1906. }
  1907. #endregion
  1908. }
  1909. }