ViewModelBase{T}.cs 3.3 KB

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