DigitalFormLookupView.cs 3.5 KB

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