Shell.cs 4.2 KB

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