FileHistoryHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Management.Instrumentation;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Xml.Serialization;
  12. namespace ComPDFKit.Controls.Helper
  13. {
  14. public interface IHasFileHistory
  15. {
  16. string FilePath { get; set; }
  17. }
  18. [Serializable]
  19. public class PDFFileInfo : IHasFileHistory
  20. {
  21. public string FilePath { get; set; } = string.Empty;
  22. public string FileSize { get; set; } = string.Empty;
  23. public string FileName { get; set; } = string.Empty;
  24. public string OpenDate { get; set; } = string.Empty;
  25. }
  26. public class FileHistoryHelper<T> : INotifyPropertyChanged where T: class, IHasFileHistory
  27. {
  28. private ObservableCollection<T> _history;
  29. public ObservableCollection<T> History
  30. {
  31. get => _history;
  32. set
  33. {
  34. _history = value;
  35. UpdateProper(ref _history, value);
  36. }
  37. }
  38. private static FileHistoryHelper<T> instance;
  39. public static FileHistoryHelper<T> Instance
  40. {
  41. get
  42. {
  43. if (instance == null)
  44. {
  45. instance = new FileHistoryHelper<T>();
  46. }
  47. return instance;
  48. }
  49. }
  50. public int DefaultHistoryCount { get; private set; } = int.MaxValue;
  51. public string DefaultFilePath { get; private set; } = string.Empty;
  52. private FileHistoryHelper()
  53. {
  54. History = new ObservableCollection<T>();
  55. DefaultFilePath = "History.xml";
  56. DefaultHistoryCount = 10;
  57. }
  58. public void AddHistory(T item)
  59. {
  60. if (item == null)
  61. {
  62. return;
  63. }
  64. T existingItem = History.FirstOrDefault(i => i.FilePath == item.FilePath);
  65. if (existingItem != null)
  66. {
  67. History.Remove(existingItem);
  68. }
  69. History.Insert(0, item);
  70. if (History.Count > DefaultHistoryCount)
  71. {
  72. History.RemoveAt(History.Count - 1);
  73. }
  74. }
  75. public void SaveHistory(string filePath = "")
  76. {
  77. if (string.IsNullOrEmpty(filePath))
  78. {
  79. filePath = DefaultFilePath;
  80. }
  81. try
  82. {
  83. using (FileStream fs = new FileStream(filePath, FileMode.Create))
  84. {
  85. XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<T>));
  86. serializer.Serialize(fs, History);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. ClearHistory();
  92. if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
  93. {
  94. File.Delete(filePath);
  95. }
  96. }
  97. }
  98. public void LoadHistory(string filePath = "")
  99. {
  100. if (string.IsNullOrEmpty(filePath))
  101. {
  102. filePath = DefaultFilePath;
  103. }
  104. try
  105. {
  106. using (FileStream fs = new FileStream(filePath, FileMode.Open))
  107. {
  108. XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<T>));
  109. History = (ObservableCollection<T>)serializer.Deserialize(fs);
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. ClearHistory();
  115. if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
  116. {
  117. File.Delete(filePath);
  118. }
  119. }
  120. }
  121. public void ClearHistory()
  122. {
  123. History.Clear();
  124. }
  125. public event PropertyChangedEventHandler PropertyChanged;
  126. protected void UpdateProper<T>(ref T properValue,
  127. T newValue,
  128. [CallerMemberName] string properName = "")
  129. {
  130. properValue = newValue;
  131. OnPropertyChanged(properName);
  132. }
  133. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  134. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  135. }
  136. }