Shell.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if (_entity != null)
  71. {
  72. new Client<TEntity>().Save(_entity, auditmessage);
  73. _entity.CommitChanges();
  74. SyncRow();
  75. }
  76. if (!Parent.HasItem(this))
  77. {
  78. Parent.CommitItem(this);
  79. }
  80. }
  81. public Task SaveAsync(string auditMessage)
  82. {
  83. return Task.Run(() => Save(auditMessage));
  84. }
  85. public void Cancel()
  86. {
  87. _entity?.CancelChanges();
  88. }
  89. private CoreRow _row = null!;
  90. public CoreRow Row
  91. {
  92. get => _row;
  93. set
  94. {
  95. _row = value;
  96. RowChanged();
  97. }
  98. }
  99. public TParent Parent { get; set; }
  100. ICoreRepository IShell.Parent => this.Parent;
  101. public Guid ID => Get<Guid>();
  102. public virtual string[] TextSearchValues() => [];
  103. public bool Match(string? text)
  104. {
  105. if (string.IsNullOrWhiteSpace(text))
  106. return true;
  107. foreach (var value in TextSearchValues())
  108. {
  109. if (value.Contains(text, StringComparison.InvariantCultureIgnoreCase))
  110. return true;
  111. }
  112. return false;
  113. }
  114. #region Row Get/Set Caching
  115. // We do this for three reasons:
  116. // 1. Rather than define properties in once class and columns in another,
  117. // we can define and link properties and columns in the one class,
  118. // using a _static_ constructor, which reduces complexity
  119. // 2. By caching based on the property name, we eliminate the need to convert
  120. // expressions to strings (expensive), while still retaining type-safety
  121. // 3. Using the Get/Set helper functions reduces code complexity when defining
  122. // shell properties, and distinguishes between data and calculated properties
  123. private static ShellColumns<TParent, TEntity>? _columns;
  124. public ShellColumns<TParent, TEntity> Columns
  125. {
  126. get
  127. {
  128. if (_columns == null)
  129. {
  130. _columns = new ShellColumns<TParent, TEntity>();
  131. _columns.Map(nameof(ID), x => x.ID);
  132. ConfigureColumns(_columns);
  133. }
  134. return _columns;
  135. }
  136. }
  137. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  138. protected virtual T Get<T>([CallerMemberName] string property = null)
  139. {
  140. if (_entity != null)
  141. {
  142. return (T)CoreUtils.GetPropertyValue(
  143. _entity,
  144. Columns[property]
  145. );
  146. }
  147. if (_row != null)
  148. {
  149. var col = _columns?.IndexOf(property);
  150. if (col != null)
  151. return Row.Get<T>(col.Value); //() Row.Values[];
  152. }
  153. return CoreUtils.GetDefault<T>();
  154. //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  155. }
  156. protected virtual void Set<T>(T value, bool donotify = true, [CallerMemberName] string property = null, params string[] notify)
  157. {
  158. CheckEntity();
  159. CoreUtils.SetPropertyValue(_entity, Columns[property], value);
  160. //Row.Values[Columns.IndexOf(property)] = value;
  161. // if (donotify)
  162. // {
  163. // DoPropertyChanged(property);
  164. // foreach (var _property in notify)
  165. // DoPropertyChanged(_property);
  166. // }
  167. }
  168. #endregion
  169. }