Shell.cs 4.9 KB

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