Shell.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Runtime.CompilerServices;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. using Xamarin.Essentials;
  7. using Xamarin.Forms;
  8. namespace InABox.Mobile
  9. {
  10. public abstract class Shell<TParent,TEntity> : BindableObject, INotifyPropertyChanged, IShell
  11. where TParent : IModel
  12. where TEntity : Entity, IPersistent, IRemotable, new()
  13. {
  14. #region INotifyPropertyChanged
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. protected void DoPropertyChanged(string propertyName)
  17. {
  18. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  19. }
  20. protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  21. {
  22. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  23. field = value;
  24. DoPropertyChanged(propertyName);
  25. return true;
  26. }
  27. #endregion
  28. public TEntity Entity { get; private set; }
  29. protected virtual void RowChanged()
  30. {
  31. Entity = Row.ToObject<TEntity>();
  32. }
  33. public bool IsChanged() => Entity?.IsChanged() ?? false;
  34. public void Save(string auditmessage)
  35. {
  36. new Client<TEntity>().Save(Entity, auditmessage);
  37. }
  38. public void Cancel()
  39. {
  40. Entity?.CancelChanges();
  41. Entity = null;
  42. }
  43. private CoreRow _row = null;
  44. public CoreRow Row
  45. {
  46. get => _row;
  47. set
  48. {
  49. _row = value;
  50. RowChanged();
  51. }
  52. }
  53. public TParent Parent { get; set; }
  54. IModel IShell.Parent => this.Parent;
  55. #region Row Get/Set Caching
  56. // We do this for three reasons:
  57. // 1. Rather than define properties in once class and columns in another,
  58. // we can define and link properties and columns in the one class,
  59. // using a _static_ constructor, which reduces complexity
  60. // 2. By caching based on the property name, we eliminate the need to convert
  61. // expressions to strings (expensive), while still retaining type-safety
  62. // 3. Using the Get/Set helper functions reduces code complexity when defining
  63. // shell properties, and distinguishes between data and calculated properties
  64. private static ShellColumns<TParent, TEntity> _columns;
  65. public ShellColumns<TParent, TEntity> Columns
  66. {
  67. get
  68. {
  69. if (_columns == null)
  70. {
  71. _columns = new ShellColumns<TParent, TEntity>();
  72. ConfigureColumns(_columns);
  73. }
  74. return _columns;
  75. }
  76. }
  77. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  78. protected virtual T Get<T>([CallerMemberName] string property = null)
  79. {
  80. if (Entity != null)
  81. {
  82. return (T)CoreUtils.GetPropertyValue(
  83. Entity,
  84. CoreUtils.GetFullPropertyName(Columns[property], ".")
  85. );
  86. }
  87. var value = Row.Values[_columns.IndexOf(property)];
  88. return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  89. }
  90. protected virtual void Set<T>(T value, bool notify = true, [CallerMemberName] string property = null)
  91. {
  92. Entity ??= Row.ToObject<TEntity>();
  93. CoreUtils.SetPropertyValue(
  94. Entity,
  95. CoreUtils.GetFullPropertyName(Columns[property], "."),
  96. value
  97. );
  98. //Row.Values[Columns.IndexOf(property)] = value;
  99. if (notify)
  100. DoPropertyChanged(property);
  101. }
  102. #endregion
  103. }
  104. }