123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using InABox.Clients;
- using InABox.Core;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- namespace InABox.Mobile
- {
- public abstract class Shell<TParent,TEntity> : BindableObject, INotifyPropertyChanged, IShell
- where TParent : IModel
- where TEntity : Entity, IPersistent, IRemotable, new()
- {
-
- #region INotifyPropertyChanged
-
- public event PropertyChangedEventHandler PropertyChanged;
-
- protected void DoPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
-
- protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
- {
- if (EqualityComparer<T>.Default.Equals(field, value)) return false;
- field = value;
- DoPropertyChanged(propertyName);
- return true;
- }
-
- #endregion
-
- public TEntity Entity { get; private set; }
-
- protected virtual void RowChanged()
- {
- Entity = Row.ToObject<TEntity>();
- }
- public bool IsChanged() => Entity?.IsChanged() ?? false;
- public void Save(string auditmessage)
- {
- new Client<TEntity>().Save(Entity, auditmessage);
- }
- public void Cancel()
- {
- Entity?.CancelChanges();
- Entity = null;
- }
-
- private CoreRow _row = null;
- public CoreRow Row
- {
- get => _row;
- set
- {
- _row = value;
- RowChanged();
- }
- }
-
- public TParent Parent { get; set; }
- IModel IShell.Parent => this.Parent;
- public Guid ID => Get<Guid>();
-
- #region Row Get/Set Caching
-
- // We do this for three reasons:
- // 1. Rather than define properties in once class and columns in another,
- // we can define and link properties and columns in the one class,
- // using a _static_ constructor, which reduces complexity
- // 2. By caching based on the property name, we eliminate the need to convert
- // expressions to strings (expensive), while still retaining type-safety
- // 3. Using the Get/Set helper functions reduces code complexity when defining
- // shell properties, and distinguishes between data and calculated properties
- private static ShellColumns<TParent, TEntity> _columns;
-
- public ShellColumns<TParent, TEntity> Columns
- {
- get
- {
- if (_columns == null)
- {
- _columns = new ShellColumns<TParent, TEntity>();
- _columns.Map(nameof(ID), x => x.ID);
- ConfigureColumns(_columns);
- }
- return _columns;
- }
- }
- protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
- protected virtual T Get<T>([CallerMemberName] string property = null)
- {
- if (Entity != null)
- {
- return (T)CoreUtils.GetPropertyValue(
- Entity,
- CoreUtils.GetFullPropertyName(Columns[property], ".")
- );
- }
- var col = _columns.IndexOf(property);
- var value = Row.Get<T>(col, true); //() Row.Values[];
- return value;
- //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
- }
-
- protected virtual void Set<T>(T value, bool notify = true, [CallerMemberName] string property = null)
- {
- Entity ??= Row.ToObject<TEntity>();
- CoreUtils.SetPropertyValue(
- Entity,
- CoreUtils.GetFullPropertyName(Columns[property], "."),
- value
- );
- //Row.Values[Columns.IndexOf(property)] = value;
- if (notify)
- DoPropertyChanged(property);
- }
- #endregion
-
- }
- }
|