QAFormViewer.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Linq;
  5. using Xamarin.Forms;
  6. using InABox.Core;
  7. using InABox.Clients;
  8. using System.IO;
  9. using InABox.Mobile;
  10. using Syncfusion.XForms.SignaturePad;
  11. using Comal.Classes;
  12. using PRSClasses;
  13. namespace PRS.Mobile
  14. {
  15. public class QAFormViewer : Grid, IDFRenderer
  16. {
  17. #region Fields
  18. // This connects the DFLayout elements to the respective UI elements
  19. public readonly Dictionary<DFLayoutField, IDigitalFormField> Bindings = new();
  20. // These get saved into FormData
  21. public readonly Dictionary<String, String> Data = new();
  22. // These are used to pre-populate the next instance of this form
  23. public readonly Dictionary<String, String> RetainedData = new();
  24. public readonly Dictionary<DFLayoutLookupField, Dictionary<Guid, string>> dfLayoutLookupFieldLookupOptions = new();
  25. private DFLayout _layout = new();
  26. private IDigitalFormDataModel _model;
  27. const string nullOrInvalidType = "Null or Invalid Type";
  28. private readonly List<string> useSavedSignatures = new();
  29. public readonly List<string> errors = new();
  30. public bool isRequiredEmpty { get; set; }
  31. public string isRequiredMessage { get; set; }
  32. private readonly Location location = new();
  33. private readonly Color isRequiredColor = Color.DarkOrange;
  34. private readonly Color isRequiredFrame = Color.Firebrick;
  35. private DateTime timeStarted = DateTime.MinValue;
  36. readonly List<DigitalFormHeader> headersToCollapse = new();
  37. #endregion
  38. #region Constructor
  39. public QAFormViewer()
  40. {
  41. }
  42. public QAFormViewer(IDigitalFormDataModel model, DFLayout layout,
  43. Guid job = default) : this()
  44. {
  45. Load(model, layout, job);
  46. }
  47. public void Load(IDigitalFormDataModel model, DFLayout layout, Guid job = default)
  48. {
  49. GetLocation();
  50. timeStarted = DateTime.Now;
  51. _model = model;
  52. _layout = layout;
  53. _layout.Renderer = this;
  54. isRequiredEmpty = false;
  55. isRequiredMessage = "";
  56. LoadFormLayout();
  57. Data.Clear();
  58. Dictionary<String, Object> data = DigitalForm.DeserializeFormData(this._model.Instance);
  59. if (data != null)
  60. {
  61. foreach (KeyValuePair<string, object> keyValuePair in data)
  62. Data.Add(keyValuePair.Key, keyValuePair.Value.ToString());
  63. }
  64. LoadData();
  65. }
  66. #endregion
  67. #region Location
  68. private async void GetLocation()
  69. {
  70. await Task.Run(() =>
  71. {
  72. LocationServices locationServices = new LocationServices();
  73. locationServices.OnLocationFound += LocationFound;
  74. locationServices.OnLocationError += LocationError;
  75. locationServices.GetLocation();
  76. });
  77. }
  78. private async void LocationFound(LocationServices sender)
  79. {
  80. location.Latitude = sender.Latitude;
  81. location.Longitude = sender.Longitude;
  82. }
  83. private async void LocationError(LocationServices sender, Exception error)
  84. {
  85. errors.Add("Location error: " + error.Message);
  86. }
  87. #endregion Location
  88. #region Load and Save Data
  89. public void LoadData()
  90. {
  91. foreach (DFLayoutField field in Bindings.Keys)
  92. {
  93. try
  94. {
  95. if (RetainedData.TryGetValue(field.Name, out var retained))
  96. Bindings[field].Deserialize(retained);
  97. else if (Data.TryGetValue(field.Name, out var data))
  98. Bindings[field].Deserialize(data);
  99. else
  100. Bindings[field].Deserialize("");
  101. _layout.ChangeField(field.Name);
  102. }
  103. catch (Exception ex)
  104. {
  105. InABox.Mobile.MobileLogging.Log(ex,$"LoadData({field.Name})");
  106. }
  107. }
  108. }
  109. public void SaveData(bool saveForLater)
  110. {
  111. try
  112. {
  113. Data.Clear();
  114. RetainedData.Clear();
  115. foreach (DFLayoutField field in Bindings.Keys)
  116. {
  117. var value = Bindings[field].Serialize();
  118. Data[field.Name] = value;
  119. if (field.GetProperties().Retain)
  120. RetainedData[field.Name] = value;
  121. }
  122. UpdateModel(saveForLater);
  123. }
  124. catch (Exception ex)
  125. {
  126. InABox.Mobile.MobileLogging.Log(ex,"SaveData");
  127. }
  128. }
  129. #endregion
  130. #region Load Form Layout
  131. private void LoadFormLayout()
  132. {
  133. RowDefinitions.Clear();
  134. foreach (var row in _layout.RowHeights)
  135. {
  136. RowDefinition def = new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) };
  137. SetRowHeight(row, def);
  138. RowDefinitions.Add(def);
  139. }
  140. ColumnDefinitions.Clear();
  141. foreach (var col in _layout.ColumnWidths)
  142. {
  143. ColumnDefinition def = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) };
  144. def = SetColumnWidth(col, def);
  145. ColumnDefinitions.Add(def);
  146. }
  147. Children.Clear();
  148. foreach (DFLayoutControl element in _layout.Elements)
  149. LoadViewAccordingToElement(element);
  150. foreach (var header in headersToCollapse)
  151. AdjustHeaderSection(header, false);
  152. }
  153. private static void SetRowHeight(string row, RowDefinition def)
  154. {
  155. if (int.TryParse(row, out int rowHeight))
  156. def.Height = new GridLength(Math.Max(60, rowHeight), GridUnitType.Absolute);
  157. else if (row.Contains("*"))
  158. {
  159. def.Height = int.TryParse(row.Substring(0, row.IndexOf("*")), out int result)
  160. ? new GridLength(result, GridUnitType.Star)
  161. : new GridLength(1, GridUnitType.Star);
  162. }
  163. else
  164. def.Height = new GridLength(1, GridUnitType.Auto);
  165. }
  166. private static ColumnDefinition SetColumnWidth(string col, ColumnDefinition def)
  167. {
  168. if (int.TryParse(col, out int colWidth))
  169. def = new ColumnDefinition { Width = new GridLength(colWidth, GridUnitType.Absolute) };
  170. else if (col.Contains("*"))
  171. {
  172. def = int.TryParse(col.Substring(0, col.IndexOf("*")), out int result)
  173. ? new ColumnDefinition { Width = new GridLength(result, GridUnitType.Star) }
  174. : new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
  175. }
  176. return def;
  177. }
  178. private void LoadViewAccordingToElement(DFLayoutControl element)
  179. {
  180. // Not sure why this is here - perhaps to ry and handle malformed element definitions?
  181. if (string.IsNullOrEmpty(element.Description))
  182. return;
  183. if (element is DFLayoutLabel l)
  184. {
  185. var result = new DigitalFormLabel() { Definition = l };
  186. AddViewToGrid(result, element);
  187. }
  188. else if (element is DFLayoutImage img )
  189. {
  190. var result = new DigitalFormImage() { Definition = img };
  191. AddViewToGrid(result, element);
  192. }
  193. else if (element is DFLayoutHeader hdr)
  194. {
  195. var result = new DigitalFormHeader() { Definition = hdr };
  196. result.CollapsedChanged += (sender, args) => AdjustHeaderSection(result, result.Collapsed);
  197. if (hdr.Collapsed)
  198. headersToCollapse.Add(result);
  199. AddViewToGrid(result, element);
  200. }
  201. else if (element is DFLayoutBooleanField b)
  202. CreateView<DigitalFormBoolean, DFLayoutBooleanField, DFLayoutBooleanFieldProperties, bool>(b);
  203. else if (element is DFLayoutStringField s)
  204. {
  205. if (s.Properties.PopupEditor)
  206. CreateView<DigitalFormStringPopup, DFLayoutStringField, DFLayoutStringFieldProperties, string>(s);
  207. else if (s.Properties.TextWrapping)
  208. CreateView<DigitalFormStringEditor, DFLayoutStringField, DFLayoutStringFieldProperties, string>(s);
  209. else
  210. CreateView<DigitalFormStringEntry, DFLayoutStringField, DFLayoutStringFieldProperties, string>(s);
  211. }
  212. else if (element is DFLayoutIntegerField i)
  213. CreateView<DigitalFormIntegerEntry, DFLayoutIntegerField, DFLayoutIntegerFieldProperties, int>(i);
  214. else if (element is DFLayoutDoubleField d)
  215. CreateView<DigitalFormDoubleEntry, DFLayoutDoubleField, DFLayoutDoubleFieldProperties, double>(d);
  216. else if (element is DFLayoutDateField df)
  217. CreateView<DigitalFormDateEntry, DFLayoutDateField, DFLayoutDateFieldProperties, DateTime>(df);
  218. else if (element is DFLayoutTimeField tf)
  219. CreateView<DigitalFormTimeEntry, DFLayoutTimeField, DFLayoutTimeFieldProperties, TimeSpan>(tf);
  220. else if (element is DFLayoutOptionField of)
  221. {
  222. if (of.Properties.OptionType == DFLayoutOptionType.Radio)
  223. CreateView<DigitalFormOptionRadioList, DFLayoutOptionField, DFLayoutOptionFieldProperties, string>(of);
  224. else if (of.Properties.OptionType == DFLayoutOptionType.Buttons)
  225. CreateView<DigitalFormOptionButtonList, DFLayoutOptionField, DFLayoutOptionFieldProperties, string>(of);
  226. else
  227. CreateView<DigitalFormOptionComboBox, DFLayoutOptionField, DFLayoutOptionFieldProperties, string>(of);
  228. }
  229. else if (element is DFLayoutLookupField lf)
  230. {
  231. if (lf.Properties.DisplayType == DFLayoutLookupDisplayType.Combo)
  232. CreateView<DigitalFormLookupComboBox, DFLayoutLookupField, DFLayoutLookupFieldProperties, Guid>(lf);
  233. else
  234. CreateView<DigitalFormLookupButton, DFLayoutLookupField, DFLayoutLookupFieldProperties, Guid>(lf);
  235. }
  236. else if (element is DFLayoutSignaturePad sp)
  237. CreateView<DigitalFormSignature, DFLayoutSignaturePad, DFLayoutSignaturePadProperties, byte[]>(sp);
  238. else if (element is DFLayoutEmbeddedImage ei)
  239. CreateView<DigitalFormEmbeddedImage, DFLayoutEmbeddedImage, DFLayoutEmbeddedImageProperties, DFLayoutEmbeddedMediaValue>(ei);
  240. else if (element is DFLayoutVideoField ev)
  241. CreateView<DigitalFormEmbeddedVideo, DFLayoutVideoField, DFLayoutVideoFieldProperties, DFLayoutEmbeddedMediaValue>(ev);
  242. else if (element is DFLayoutMultiImage mi)
  243. CreateView<DigitalFormMultiImage, DFLayoutMultiImage, DFLayoutMultiImageProperties, DFLayoutEmbeddedMediaValues>(mi);
  244. else if (element is DFLayoutMultiSignaturePad)
  245. {
  246. var tuple = LoadMultiSignaturePad(element);
  247. AddViewToGrid(tuple.Item1, element);
  248. }
  249. else if (element is DFLayoutAddTaskField)
  250. {
  251. var tuple = LoadAddTaskField(element);
  252. AddViewToGrid(tuple.Item1, element);
  253. }
  254. }
  255. private TField CreateView<TField, TDefinition, TProperties, TValue>(TDefinition definition)
  256. where TField : View, IDigitalFormField<TDefinition, TProperties, TValue>, new()
  257. where TDefinition : DFLayoutField<TProperties>
  258. where TProperties : DFLayoutFieldProperties<TValue>, new()
  259. {
  260. TField item = new TField()
  261. {
  262. Definition = definition,
  263. IsEnabled = !definition.GetProperties().Secure
  264. && _model.Instance.FormCompleted.IsEmpty()
  265. && string.IsNullOrWhiteSpace(definition.GetPropertyValue<string>("Expression")),
  266. };
  267. item.ValueChanged += (sender, args) =>
  268. {
  269. // Update Object?
  270. var property = args.Definition.GetProperties().Property;
  271. if (!String.IsNullOrWhiteSpace(property))
  272. CoreUtils.SetPropertyValue(_model.Entity,property,args.Value);
  273. foreach (var other in Bindings.Keys.Where(x=>x.Name != args.Definition.Name))
  274. {
  275. var linked = other.GetProperties().Property;
  276. if (!String.IsNullOrWhiteSpace(linked))
  277. {
  278. var linkedvalue = CoreUtils.GetPropertyValue(_model.Entity, linked);
  279. // Problem - linkedvalue might not be the same as the
  280. //CoreUtils.SetPropertyValue(pairs[other],"Value",linkedvalue);
  281. }
  282. }
  283. // Update ChangedLinkedProperties?
  284. // Run Expressions and Scripts?
  285. // Update Dictionary?
  286. };
  287. Bindings[definition] = item;
  288. AddViewToGrid(item, definition);
  289. return item;
  290. }
  291. private void AddViewToGrid(View view, DFLayoutControl element)
  292. {
  293. //rows and columns coming in from the desktop designer start from 1, not 0 (most of the time??)
  294. SetRow(view, Math.Max(0,element.Row - 1));
  295. SetRowSpan(view, Math.Max(1,element.RowSpan));
  296. SetColumn(view, Math.Max(0,element.Column - 1));
  297. SetColumnSpan(view, Math.Max(1,element.ColumnSpan));
  298. Children.Add(view);
  299. }
  300. private void AdjustHeaderSection(DigitalFormHeader header, bool collapsed)
  301. {
  302. try
  303. {
  304. var thisHeaderRow = GetRow(header);
  305. var headerRows = Children.OfType<DigitalFormHeader>().Select(x => GetRow(x)).OrderBy(x=>x).ToArray();
  306. if (headerRows.Any())
  307. AdjustHeightsToNextHeader(headerRows, thisHeaderRow, collapsed);
  308. else
  309. AdjustHeightsBelowHeader(headerRows, thisHeaderRow, collapsed);
  310. }
  311. catch (Exception ex)
  312. {
  313. MobileLogging.Log(ex, "AdjustHeaderSection()");
  314. }
  315. }
  316. private void AdjustHeightsToNextHeader(int[] headerRows, int thisHeaderRow, bool collapsed)
  317. {
  318. int nextHeaderRow = headerRows[0];
  319. for (int i = thisHeaderRow + 1; i < nextHeaderRow; i++)
  320. AdjustHeight(i, collapsed);
  321. }
  322. private void AdjustHeightsBelowHeader(int[] headerRows, int thisHeaderRow, bool collapsed)
  323. {
  324. for (int i = thisHeaderRow + 1; i < RowDefinitions.Count; i++)
  325. AdjustHeight(i, collapsed);
  326. }
  327. private void AdjustHeight(int i, bool collapsed)
  328. {
  329. var rowdef = RowDefinitions[i];
  330. if (collapsed)
  331. SetRowHeight(_layout.RowHeights[i], rowdef);
  332. else
  333. rowdef.Height = 0;
  334. }
  335. private Tuple<View, bool> LoadMultiSignaturePad(DFLayoutControl element)
  336. {
  337. string value = "";
  338. DFLayoutMultiSignaturePad dfLayoutMultiSignaturePad = element as DFLayoutMultiSignaturePad;
  339. DataButtonControl button = new DataButtonControl
  340. {
  341. Text = "Add Signatures",
  342. };
  343. if (Data.TryGetValue(dfLayoutMultiSignaturePad.Name, out value))
  344. {
  345. button.Data = value;
  346. }
  347. MultiSignaturePad multiSignaturePad = new MultiSignaturePad(button.Data);
  348. multiSignaturePad.OnMultiSignatureSaved += (result) =>
  349. {
  350. button.Data = result;
  351. };
  352. button.Clicked += (object sender, MobileButtonClickEventArgs e) =>
  353. {
  354. Navigation.PushAsync(multiSignaturePad);
  355. };
  356. if (dfLayoutMultiSignaturePad.Properties.Required)
  357. {
  358. button.BackgroundColor = isRequiredColor;
  359. button.BorderColor = isRequiredFrame;
  360. }
  361. else if (!_model.Instance.FormCompleted.IsEmpty())
  362. {
  363. button.BackgroundColor = Color.Silver;
  364. button.BorderColor = Color.Gray;
  365. }
  366. return new Tuple<View, bool>(button, dfLayoutMultiSignaturePad.Properties.Required);
  367. }
  368. private Tuple<View, bool> LoadAddTaskField(DFLayoutControl element)
  369. {
  370. string value = "";
  371. DFLayoutAddTaskField field = element as DFLayoutAddTaskField;
  372. DFCreateTaskView taskView = new DFCreateTaskView();
  373. taskView.OnAddTaskButtonClicked += () =>
  374. {
  375. AddEditTask addEditTask = new AddEditTask(Guid.Empty, "New task from Digital Form");
  376. if (field.Properties.TaskType != null)
  377. {
  378. addEditTask.kanban.Type.ID = field.Properties.TaskType.ID;
  379. try
  380. {
  381. addEditTask.kanban.Type.Description = new Client<KanbanType>().Query(
  382. new Filter<KanbanType>(x => x.ID).IsEqualTo(field.Properties.TaskType.ID),
  383. new Columns<KanbanType>(x => x.Description)
  384. ).Rows.FirstOrDefault()?.Get<KanbanType,String>(c=>c.Description) ?? "";
  385. }
  386. catch (Exception ex)
  387. {
  388. MobileLogging.Log(ex,"LoadAddTaskField");
  389. }
  390. addEditTask.UpdateScreen(true);
  391. }
  392. addEditTask.OnTaskSaved += (taskNumber) =>
  393. {
  394. taskView.DisableButton();
  395. taskView.TaskNumber = taskNumber.ToString();
  396. };
  397. Navigation.PushAsync(addEditTask);
  398. };
  399. if (Data.TryGetValue(field.Name, out value))
  400. {
  401. taskView.TaskNumber = value;
  402. taskView.DisableButton();
  403. }
  404. return new Tuple<View, bool>(taskView, field.Properties.Required);
  405. }
  406. #endregion
  407. #region Checks / Custom methods
  408. private void CheckRequired(DFLayoutField field)
  409. {
  410. if (field is DFLayoutStringField)
  411. {
  412. if ((field as DFLayoutStringField).Properties.Required)
  413. {
  414. isRequiredEmpty = true;
  415. isRequiredMessage = (field as DFLayoutStringField).Description;
  416. }
  417. }
  418. else if (field is DFLayoutIntegerField)
  419. {
  420. if ((field as DFLayoutIntegerField).Properties.Required)
  421. {
  422. isRequiredEmpty = true;
  423. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  424. }
  425. }
  426. else if (field is DFLayoutDoubleField)
  427. {
  428. if ((field as DFLayoutDoubleField).Properties.Required)
  429. {
  430. {
  431. isRequiredEmpty = true;
  432. isRequiredMessage = (field as DFLayoutIntegerField).Description;
  433. }
  434. }
  435. }
  436. else if (field is DFLayoutBooleanField)
  437. {
  438. if ((field as DFLayoutBooleanField).Properties.Required)
  439. {
  440. {
  441. isRequiredEmpty = true;
  442. isRequiredMessage = (field as DFLayoutBooleanField).Description;
  443. }
  444. }
  445. }
  446. else if (field is DFLayoutDateField)
  447. {
  448. if ((field as DFLayoutDateField).Properties.Required)
  449. {
  450. {
  451. isRequiredEmpty = true;
  452. isRequiredMessage = (field as DFLayoutDateField).Description;
  453. }
  454. }
  455. }
  456. else if (field is DFLayoutTimeField)
  457. {
  458. if ((field as DFLayoutTimeField).Properties.Required)
  459. {
  460. {
  461. isRequiredEmpty = true;
  462. isRequiredMessage = (field as DFLayoutTimeField).Description;
  463. }
  464. }
  465. }
  466. else if (field is DFLayoutOptionField)
  467. {
  468. if ((field as DFLayoutOptionField).Properties.Required)
  469. {
  470. {
  471. isRequiredEmpty = true;
  472. isRequiredMessage = (field as DFLayoutOptionField).Description;
  473. }
  474. }
  475. }
  476. else if (field is DFLayoutLookupField)
  477. {
  478. if ((field as DFLayoutLookupField).Properties.Required)
  479. {
  480. {
  481. isRequiredEmpty = true;
  482. isRequiredMessage = (field as DFLayoutLookupField).Description;
  483. }
  484. }
  485. }
  486. else if (field is DFLayoutSignaturePad)
  487. {
  488. if ((field as DFLayoutSignaturePad).Properties.Required)
  489. {
  490. {
  491. isRequiredEmpty = true;
  492. isRequiredMessage = (field as DFLayoutSignaturePad).Description;
  493. }
  494. }
  495. }
  496. else if (field is DFLayoutEmbeddedImage)
  497. {
  498. if ((field as DFLayoutEmbeddedImage).Properties.Required)
  499. {
  500. {
  501. isRequiredEmpty = true;
  502. isRequiredMessage = (field as DFLayoutEmbeddedImage).Description;
  503. }
  504. }
  505. }
  506. else if (field is DFLayoutMultiImage)
  507. {
  508. if ((field as DFLayoutMultiImage).Properties.Required)
  509. {
  510. {
  511. isRequiredEmpty = true;
  512. isRequiredMessage = (field as DFLayoutMultiImage).Description;
  513. }
  514. }
  515. }
  516. else if (field is DFLayoutMultiSignaturePad)
  517. {
  518. if ((field as DFLayoutMultiSignaturePad).Properties.Required)
  519. {
  520. {
  521. isRequiredEmpty = true;
  522. isRequiredMessage = (field as DFLayoutMultiSignaturePad).Description;
  523. }
  524. }
  525. }
  526. }
  527. private string ImageSourceToBase64(ImageSource source)
  528. {
  529. StreamImageSource streamImageSource = (StreamImageSource)source;
  530. System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
  531. Task<Stream> task = streamImageSource.Stream(cancellationToken);
  532. Stream stream = task.Result;
  533. byte[] bytes = new byte[stream.Length];
  534. stream.Read(bytes, 0, bytes.Length);
  535. string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  536. return s;
  537. }
  538. private string FindIDForDesignLookupField(DataButtonControl button)
  539. {
  540. try
  541. {
  542. string userInputValue = button.Data;
  543. string guidString = "";
  544. if (userInputValue != nullOrInvalidType)
  545. {
  546. foreach (KeyValuePair<DFLayoutLookupField, Dictionary<Guid, string>> pair in dfLayoutLookupFieldLookupOptions)
  547. {
  548. foreach (KeyValuePair<Guid, string> innerPair in pair.Value)
  549. {
  550. if (innerPair.Value == userInputValue)
  551. {
  552. guidString = innerPair.Key.ToString();
  553. }
  554. }
  555. }
  556. }
  557. return guidString;
  558. }
  559. catch (Exception e)
  560. {
  561. MobileLogging.Log(e,"FindIDForDesignLookupField");
  562. return "";
  563. }
  564. }
  565. #endregion
  566. #region Save Completion
  567. //point of determination for whether or not QA form gets completed or saved for later
  568. private void UpdateModel(bool saveForLater)
  569. {
  570. if (Data.Count != 0)
  571. {
  572. // Blobs are uploaded in the background by DigitalFormDocumentHandler class,
  573. // so the blob breakup doesn't have to happen anymore :-)
  574. DigitalForm.SerializeFormData(_model.Instance, _model.Variables, Data.ToDictionary(x => x.Key, x => x.Value as object));
  575. //DigitalForm.SerializeFormData(model.Instance, QueryVariables(model.Instance), results.ToDictionary(x => x.Key, x => x.Value as object));
  576. if (_model.Instance.FormStarted == DateTime.MinValue)
  577. _model.Instance.FormStarted = timeStarted;
  578. _model.Instance.FormOpen += (DateTime.Now - timeStarted);
  579. if (!saveForLater)
  580. {
  581. _model.Instance.FormCompleted = DateTime.Now;
  582. _model.Instance.FormCompletedBy.ID = App.Data.Me.UserID;
  583. _model.Instance.Location.Longitude = location.Longitude;
  584. _model.Instance.Location.Latitude = location.Latitude;
  585. _model.Instance.Location.Timestamp = _model.Instance.FormCompleted;
  586. }
  587. _model.Update(null);
  588. }
  589. }
  590. #endregion
  591. #region Expressions
  592. public object GetFieldValue(string name)
  593. {
  594. try
  595. {
  596. var definition = Bindings.Keys.FirstOrDefault(x => String.Equals(x.Name, name));
  597. if (definition != null)
  598. return CoreUtils.GetPropertyValue(Bindings[definition],"Value");
  599. }
  600. catch (Exception e)
  601. {
  602. MobileLogging.Log(e,"SetFieldValue");
  603. }
  604. return null;
  605. }
  606. public void SetFieldValue(string name, object value)
  607. {
  608. try
  609. {
  610. var definition = Bindings.Keys.FirstOrDefault(x => String.Equals(x.Name, name));
  611. if (definition != null)
  612. CoreUtils.SetPropertyValue(Bindings[definition],"Value",value);
  613. }
  614. catch (Exception e)
  615. {
  616. MobileLogging.Log(e,"SetFieldValue");
  617. }
  618. }
  619. public object GetFieldData(string fieldName, string dataField)
  620. {
  621. var definition = Bindings.Keys.FirstOrDefault(x => String.Equals(x.Name, fieldName));
  622. if ((definition != null) && (Bindings[definition] is DigitalFormLookupView lookup))
  623. return lookup.OtherValue(dataField);
  624. return null;
  625. }
  626. public void SetFieldColour(string field, System.Drawing.Color? colour = null)
  627. {
  628. if (colour != null)
  629. {
  630. try
  631. {
  632. System.Drawing.Color color = (System.Drawing.Color)colour;
  633. var definition = Bindings.Keys.FirstOrDefault(x => String.Equals(x.Name, field));
  634. if (definition != null)
  635. {
  636. Bindings[definition].BackgroundColor = Color.FromRgba(
  637. Convert.ToDouble(color.R),
  638. Convert.ToDouble(color.G),
  639. Convert.ToDouble(color.B),
  640. Convert.ToDouble(color.A)
  641. );
  642. }
  643. }
  644. catch (Exception e)
  645. {
  646. MobileLogging.Log(e,"SetFieldColor");
  647. }
  648. }
  649. }
  650. #endregion
  651. }
  652. }