using System; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; namespace PRS.Mobile { public abstract class DigitalFormLookupView : ContentView, IDigitalFormField { private DFLayoutLookupField _definition; public DFLayoutLookupField Definition { get => _definition; set { _definition = value; Initialize(value ?? new DFLayoutLookupField()); } } protected DFLayoutLookupValue _value; public Guid Value { get => _value.ID; set =>_value.ID = value; } public bool IsEmpty => Value == Guid.Empty; protected abstract void UpdateUI(); private bool _readOnly; public bool ReadOnly { get => _readOnly; set { _readOnly = value; UpdateStatus(); } } public object OtherValue(String property) { if ((_value?.Values != null) && _value.Values.TryGetValue(property, out object result)) return result; return null; } public void Deserialize(string serialized) { _value = new DFLayoutLookupValue(serialized); UpdateUI(); } public string Serialize() { return _value?.ToString() ?? ""; } public event DigitalFormViewChangedHandler ValueChanged; protected DigitalFormLookupView() { _value = new DFLayoutLookupValue(); } protected void DoLookup(object sender, MobileButtonClickEventArgs args) { var entityType = CoreUtils.GetEntity(Definition.Properties.LookupType); var modeltype = typeof(SelectionViewModel<>).MakeGenericType(entityType); var model = (ISelectionViewModel)Activator.CreateInstance(modeltype); foreach (var additionalcolumn in Definition.Properties.AdditionalPropertiesList) model.VisibleColumns.Add(additionalcolumn); GenericSelectionPage _selector = new GenericSelectionPage("Select Value", model); _selector.OnItemSelected += (o,e) => { _value.ID = e.Row.Get("ID"); _value.Text = e.Formatted; _value.Values = e.Row.ToDictionary(); UpdateUI(); ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value)); }; Navigation.PushAsync(_selector); } private void Initialize(DFLayoutLookupField definition) { UpdateStatus(); } protected abstract void UpdateStatus(); } }