DigitalFormLookupView.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using InABox.Core;
  3. using InABox.Mobile;
  4. using Xamarin.Forms;
  5. namespace PRS.Mobile
  6. {
  7. public abstract class DigitalFormLookupView : ContentView,
  8. IDigitalFormField<DFLayoutLookupField, DFLayoutLookupFieldProperties, Guid>
  9. {
  10. private DFLayoutLookupField _definition;
  11. public DFLayoutLookupField Definition
  12. {
  13. get => _definition;
  14. set
  15. {
  16. _definition = value;
  17. Initialize(value ?? new DFLayoutLookupField());
  18. }
  19. }
  20. protected DFLayoutLookupValue _value;
  21. public Guid Value
  22. {
  23. get => _value.ID;
  24. set =>_value.ID = value;
  25. }
  26. public bool IsEmpty => Value == Guid.Empty;
  27. protected abstract void UpdateUI();
  28. private bool _readOnly;
  29. public bool ReadOnly
  30. {
  31. get => _readOnly;
  32. set
  33. {
  34. _readOnly = value;
  35. UpdateStatus();
  36. }
  37. }
  38. public object OtherValue(String property)
  39. {
  40. if ((_value?.Values != null) && _value.Values.TryGetValue(property, out object result))
  41. return result;
  42. return null;
  43. }
  44. public void Deserialize(string serialized)
  45. {
  46. _value = new DFLayoutLookupValue(serialized);
  47. UpdateUI();
  48. }
  49. public string Serialize()
  50. {
  51. return _value?.ToString() ?? "";
  52. }
  53. public event DigitalFormViewChangedHandler ValueChanged;
  54. protected DigitalFormLookupView()
  55. {
  56. _value = new DFLayoutLookupValue();
  57. }
  58. protected void DoLookup(object sender, MobileButtonClickEventArgs args)
  59. {
  60. var entityType = CoreUtils.GetEntity(Definition.Properties.LookupType);
  61. var modeltype = typeof(SelectionViewModel<>).MakeGenericType(entityType);
  62. var model = (ISelectionViewModel)Activator.CreateInstance(modeltype);
  63. foreach (var additionalcolumn in Definition.Properties.AdditionalPropertiesList)
  64. model.VisibleColumns.Add(additionalcolumn);
  65. GenericSelectionPage _selector = new GenericSelectionPage("Select Value", model);
  66. _selector.OnItemSelected += (o,e) =>
  67. {
  68. _value.ID = e.Row.Get<Guid>("ID");
  69. _value.Text = e.Formatted;
  70. _value.Values = e.Row.ToDictionary();
  71. UpdateUI();
  72. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  73. };
  74. Navigation.PushAsync(_selector);
  75. }
  76. private void Initialize(DFLayoutLookupField definition)
  77. {
  78. UpdateStatus();
  79. }
  80. protected abstract void UpdateStatus();
  81. }
  82. }