DigitalFormLookupView.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 DigitalFormLookupValue _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 void Load(string value)
  39. {
  40. _value = new DigitalFormLookupValue(value);
  41. UpdateUI();
  42. }
  43. public string Save()
  44. {
  45. return _value?.ToString() ?? "";
  46. }
  47. public event DigitalFormViewChangedHandler ValueChanged;
  48. protected DigitalFormLookupView()
  49. {
  50. _value = new DigitalFormLookupValue();
  51. }
  52. protected void DoLookup(object sender, MobileButtonClickEventArgs args)
  53. {
  54. var entityType = CoreUtils.GetEntity(Definition.Properties.LookupType);
  55. var modeltype = typeof(SelectionViewModel<>).MakeGenericType(entityType);
  56. var model = (ISelectionViewModel)Activator.CreateInstance(modeltype);
  57. foreach (var additionalcolumn in Definition.Properties.AdditionalPropertiesList)
  58. model.VisibleColumns.Add(additionalcolumn);
  59. GenericSelectionPage _selector = new GenericSelectionPage("Select Value", model);
  60. _selector.OnItemSelected += (o,e) =>
  61. {
  62. _value.ID = e.Row.Get<Guid>("ID");
  63. _value.Text = e.Formatted;
  64. _value.Values = e.Row.ToDictionary();
  65. UpdateUI();
  66. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  67. };
  68. Navigation.PushAsync(_selector);
  69. }
  70. private void Initialize(DFLayoutLookupField definition)
  71. {
  72. UpdateStatus();
  73. }
  74. protected abstract void UpdateStatus();
  75. }
  76. }