Shell.cs 5.7 KB

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