ViewModelBase{TModel}.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using EventBus;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace IViewModel.ViewModels
  11. {
  12. public abstract class ViewModelBase<TModel>:ViewModelBase where TModel : IModel.IModel
  13. {
  14. public ViewModelBase(TModel model) : this()
  15. {
  16. UpDateModel(model);
  17. }
  18. private List<FieldInfo> AllFields = new List<FieldInfo>();
  19. private List<(string, IReadOnlyList<string>)> AllProperties = new List<(string, IReadOnlyList<string>)>();
  20. public ViewModelBase()
  21. {
  22. if (typeof(TModel).IsAnsiClass && !typeof(TModel).IsAbstract)
  23. {
  24. Model = Activator.CreateInstance<TModel>();
  25. AllFields = typeof(TModel).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
  26. GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
  27. .Where(x => x.GetCustomAttribute<PropertyAssociationAttribute>() != null)
  28. .ToList()
  29. .ForEach(x =>
  30. {
  31. var att = x.GetCustomAttribute<PropertyAssociationAttribute>();
  32. if (att == null || att.Names.Count == 0) return;
  33. AllProperties.Add((x.Name, att.Names));
  34. });
  35. }
  36. }
  37. [AllowNull]
  38. private TModel model = default;
  39. public virtual TModel Model { get => model; set => SetProperty(ref model, value); }
  40. public virtual void ReceiveServiceModel(TModel model)
  41. {
  42. if (typeof(TModel).IsAbstract || typeof(TModel).IsInterface) return;
  43. if (lastModel != null)
  44. {
  45. lastModel = (TModel)model.Clone();
  46. }
  47. UpDateModel(model);
  48. }
  49. public virtual void UpDateModel(TModel model)
  50. {
  51. if (typeof(TModel).IsAbstract || typeof(TModel).IsInterface) return;
  52. List<string> changedNames = new List<string>();
  53. if (model == null || AllFields.Count == 0) return;
  54. if (Model == null)
  55. {
  56. changedNames = AllFields.Select(x => x.Name).ToList();
  57. }
  58. else
  59. {
  60. AllFields.ForEach(x =>
  61. {
  62. var lastvalue = x.GetValue(Model);
  63. var fieldvalue = x.GetValue(model);
  64. if (!Equals(lastvalue, fieldvalue))
  65. {
  66. changedNames.Add(x.Name);
  67. }
  68. });
  69. }
  70. Model = model;
  71. if (changedNames.Count == 0) return;
  72. List<string> nochanged = new List<string>();
  73. changedNames.ForEach(x =>
  74. {
  75. bool state = false;
  76. AllProperties.ForEach(y =>
  77. {
  78. if (y.Item2.Contains(x))
  79. {
  80. OnPropertyChanged(y.Item1);
  81. state = true;
  82. }
  83. });
  84. if (!state) nochanged.Add(x);
  85. });
  86. if (nochanged.Count == 0) return;
  87. RefreshUI(nochanged);
  88. }
  89. [AllowNull]
  90. protected TModel lastModel;
  91. protected virtual void RefreshUI(List<string> changedNames)
  92. {
  93. }
  94. }
  95. }