DigitalFormLookupView.cs 4.4 KB

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