Shell.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System.ComponentModel;
  2. using System.Runtime.CompilerServices;
  3. using DynamicData.Binding;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. namespace InABox.Avalonia;
  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. private bool _isSelected;
  18. public bool IsSelected
  19. {
  20. get => _isSelected;
  21. set
  22. {
  23. if(_isSelected != value)
  24. {
  25. _isSelected = value;
  26. DoPropertyChanged(nameof(IsSelected));
  27. }
  28. }
  29. }
  30. #region INotifyPropertyChanged
  31. public event PropertyChangedEventHandler? PropertyChanged;
  32. protected void DoPropertyChanged(string propertyName)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  35. }
  36. protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
  37. {
  38. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  39. field = value;
  40. DoPropertyChanged(propertyName);
  41. return true;
  42. }
  43. #endregion
  44. private TEntity? _entity;
  45. private TEntity CheckEntity()
  46. {
  47. _entity ??= Row.ToObject<TEntity>();
  48. _entity.PropertyCascaded -= _entity_PropertyChanged;
  49. _entity.PropertyCascaded += _entity_PropertyChanged;
  50. return _entity;
  51. }
  52. private void _entity_PropertyChanged(object? sender, PropertyChangedEventArgs e)
  53. {
  54. if(e.PropertyName is not null && Columns.FindShellProperty(e.PropertyName) is string property)
  55. {
  56. DoPropertyChanged(property);
  57. }
  58. }
  59. public TEntity Entity => CheckEntity();
  60. protected virtual void RowChanged()
  61. {
  62. }
  63. public bool IsChanged() => _entity?.IsChanged() ?? false;
  64. public void SyncRow()
  65. {
  66. Row.Table.FillRow(Row, Entity);
  67. }
  68. public virtual void Save(string auditmessage)
  69. {
  70. new Client<TEntity>().Save(Entity, auditmessage);
  71. Entity.CommitChanges();
  72. SyncRow();
  73. if (!Parent.HasItem(this))
  74. {
  75. Parent.CommitItem(this);
  76. }
  77. }
  78. public Task SaveAsync(string auditMessage)
  79. {
  80. return Task.Run(() => Save(auditMessage));
  81. }
  82. public void Cancel()
  83. {
  84. _entity?.CancelChanges();
  85. }
  86. private CoreRow _row = null!;
  87. public CoreRow Row
  88. {
  89. get => _row;
  90. set
  91. {
  92. _row = value;
  93. RowChanged();
  94. }
  95. }
  96. public TParent Parent { get; set; }
  97. ICoreRepository IShell.Parent => this.Parent;
  98. public Guid ID => Get<Guid>();
  99. public virtual string[] TextSearchValues() => [];
  100. public bool Match(string? text)
  101. {
  102. if (string.IsNullOrWhiteSpace(text))
  103. return true;
  104. foreach (var value in TextSearchValues())
  105. {
  106. if (value.Contains(text, StringComparison.InvariantCultureIgnoreCase))
  107. return true;
  108. }
  109. return false;
  110. }
  111. #region Row Get/Set Caching
  112. // We do this for three reasons:
  113. // 1. Rather than define properties in once class and columns in another,
  114. // we can define and link properties and columns in the one class,
  115. // using a _static_ constructor, which reduces complexity
  116. // 2. By caching based on the property name, we eliminate the need to convert
  117. // expressions to strings (expensive), while still retaining type-safety
  118. // 3. Using the Get/Set helper functions reduces code complexity when defining
  119. // shell properties, and distinguishes between data and calculated properties
  120. private static ShellColumns<TParent, TEntity>? _columns;
  121. public ShellColumns<TParent, TEntity> Columns
  122. {
  123. get
  124. {
  125. if (_columns == null)
  126. {
  127. _columns = new ShellColumns<TParent, TEntity>();
  128. _columns.Map(nameof(ID), x => x.ID);
  129. ConfigureColumns(_columns);
  130. }
  131. return _columns;
  132. }
  133. }
  134. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  135. /// <summary>
  136. /// Get the property on the entity, coercing <see langword="default"/>(<typeparamref name="T"/>) into <see langword="null"/>.
  137. /// </summary>
  138. protected T? GetNullable<T>([CallerMemberName] string property = null)
  139. where T : struct
  140. {
  141. var value = Get<T>(property);
  142. if(Equals(value, default(T)))
  143. {
  144. return null;
  145. }
  146. else
  147. {
  148. return value;
  149. }
  150. }
  151. /// <summary>
  152. /// Set the property on the entity, coercing <see langword="null"/> into <see langword="default"/>(<typeparamref name="T"/>).
  153. /// </summary>
  154. protected void SetNullable<T>(T? value, bool donotify = true, [CallerMemberName] string property = null, params string[] notify)
  155. where T : struct
  156. {
  157. if(value is null)
  158. {
  159. Set(default(T), donotify, property, notify);
  160. }
  161. else
  162. {
  163. Set(value.Value, donotify, property, notify);
  164. }
  165. }
  166. protected virtual T Get<T>([CallerMemberName] string property = null)
  167. {
  168. if (_entity != null)
  169. {
  170. return (T)CoreUtils.GetPropertyValue(
  171. _entity,
  172. Columns[property]
  173. );
  174. }
  175. if (_row != null)
  176. {
  177. var col = _columns?.IndexOf(property);
  178. if (col != null)
  179. return Row.Get<T>(col.Value); //() Row.Values[];
  180. }
  181. return CoreUtils.GetDefault<T>();
  182. //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  183. }
  184. protected virtual void Set<T>(T value, bool donotify = true, [CallerMemberName] string property = null, params string[] notify)
  185. {
  186. CheckEntity();
  187. CoreUtils.SetPropertyValue(_entity, Columns[property], value);
  188. //Row.Values[Columns.IndexOf(property)] = value;
  189. // if (donotify)
  190. // {
  191. // DoPropertyChanged(property);
  192. // foreach (var _property in notify)
  193. // DoPropertyChanged(_property);
  194. // }
  195. }
  196. #endregion
  197. }