Browse Source

compdfkit(win) - 历史文件记录持久化

liuaoran 1 year ago
parent
commit
eabd03200a

+ 154 - 0
Demo/Examples/Compdfkit_Tools/Common/Helper/FileHistoryHelper.cs

@@ -0,0 +1,154 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using System.Management.Instrumentation;
+using System.Runtime.CompilerServices;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace Compdfkit_Tools.Helper
+{
+    public interface IHasFileHistory
+    {
+        string FilePath { get; set; }
+    }
+
+    [Serializable]
+    public class PDFFileInfo : IHasFileHistory
+    {
+        public string FilePath { get; set; } = string.Empty;
+        public string FileSize { get; set; } = string.Empty;    
+        public string FileName { get; set; } = string.Empty;
+        public string OpenDate { get; set; } = string.Empty;
+    }
+
+    public class FileHistoryHelper<T> : INotifyPropertyChanged where T: class, IHasFileHistory
+    {
+
+        private ObservableCollection<T> _history;
+
+        public ObservableCollection<T> History
+        {
+            get => _history;
+            set
+            {
+                _history = value;
+                UpdateProper(ref _history, value);
+            }
+        }
+
+        private static FileHistoryHelper<T> instance;
+        public static FileHistoryHelper<T> Instance
+        {
+            get
+            {
+                if (instance == null)
+                {
+                    instance = new FileHistoryHelper<T>();
+                }
+                return instance;
+            }
+        }
+
+        public int DefaultHistoryCount { get; private set; } = int.MaxValue;
+        public string DefaultFilePath { get; private set; } = string.Empty;
+
+        private FileHistoryHelper()
+        {
+            History = new ObservableCollection<T>();
+            DefaultFilePath = "History.xml";
+            DefaultHistoryCount = 10;
+        }
+
+        public void AddHistory(T item)
+        {
+            if (item == null)
+            {
+                return;
+            }
+            T existingItem = History.FirstOrDefault(i => i.FilePath == item.FilePath);
+
+            if (existingItem != null)
+            {
+                History.Remove(existingItem);
+            }
+            History.Insert(0, item);
+        
+            if (History.Count > DefaultHistoryCount)
+            {
+                History.RemoveAt(History.Count - 1);
+            }
+        }
+
+        public void SaveHistory(string filePath = "")
+        {
+            if (string.IsNullOrEmpty(filePath))
+            {
+                filePath = DefaultFilePath;
+            }
+
+            try
+            {
+                using (FileStream fs = new FileStream(filePath, FileMode.Create))
+                {
+                    XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<T>));
+                    serializer.Serialize(fs, History);
+                }
+            }
+            catch (Exception ex)
+            {
+                ClearHistory();
+                if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
+                {
+                    File.Delete(filePath);
+                }
+            }
+        }
+
+        public void LoadHistory(string filePath = "")
+        {
+            if (string.IsNullOrEmpty(filePath))
+            {
+                filePath = DefaultFilePath;
+            }
+
+            try
+            {
+                using (FileStream fs = new FileStream(filePath, FileMode.Open))
+                {
+                    XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<T>));
+                    History = (ObservableCollection<T>)serializer.Deserialize(fs);
+                }
+            }
+            catch (Exception ex)
+            {
+                ClearHistory();
+                if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
+                {
+                    File.Delete(filePath);
+                }
+            }
+        }
+
+        public void ClearHistory()
+        {
+            History.Clear();
+        }
+
+        public event PropertyChangedEventHandler PropertyChanged;
+        protected void UpdateProper<T>(ref T properValue,
+                            T newValue,
+                            [CallerMemberName] string properName = "")
+        {
+            properValue = newValue;
+            OnPropertyChanged(properName);
+        }
+
+        protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+    }
+}

+ 1 - 1
Demo/Examples/Compdfkit_Tools/Common/HomePage/FeaturesListControl.xaml.cs

@@ -204,7 +204,7 @@ namespace Compdfkit_Tools.PDFControl
     }
     public class CustomItem : Control, INotifyPropertyChanged
     {
-        static public double ItemWidth { get; set; } = 500;
+        static public double ItemWidth { get; set; } = 400;
         static public double ItemHeight { get; set; } = 128;
         static public double ItemMargin { get; set; } = 10;
 

+ 0 - 2
Demo/Examples/Compdfkit_Tools/Common/HomePage/HomePageControl.xaml.cs

@@ -225,7 +225,6 @@ namespace Compdfkit_Tools.PDFControl
                 };
 
 
-                // 创建四个矩形并添加到 Canvas
                 for (int i = 0; i < 4; i++)
                 {
                     Rectangle rectangle = new Rectangle
@@ -238,7 +237,6 @@ namespace Compdfkit_Tools.PDFControl
                         Opacity = 0.5
                     };
 
-                    // 设置每个矩形的位置
                     switch (i)
                     {
                         case 0:

+ 17 - 1
Demo/Examples/Compdfkit_Tools/Common/HomePage/RecentFilesControl.xaml

@@ -6,7 +6,23 @@
              xmlns:local="clr-namespace:Compdfkit_Tools.PDFControl"
              mc:Ignorable="d" 
              d:DesignHeight="450" d:DesignWidth="800" Background="AliceBlue">
+    <UserControl.Resources>
+        <Style TargetType="ListBox" x:Key="HistoryListBoxStyle">
+            <Setter Property="BorderThickness" Value="0" />
+            <Setter Property="BorderBrush" Value="Transparent" />
+            <Setter Property="Padding" Value="0" />
+        </Style>
+    </UserControl.Resources>
     <Grid>
-            
+        <ListBox x:Name="HistoryListBox" Style="{StaticResource HistoryListBoxStyle}" ItemsSource="{Binding History}" d:ItemsSource="{d:SampleData ItemCount=10}" SelectionMode="Single">
+            <ListBox.ItemTemplate>
+                <DataTemplate>
+                    <StackPanel Orientation="Horizontal">
+                        <TextBlock Text="{Binding FilePath}"  Margin="0,0,20,0"/>
+                        <TextBlock Text="{Binding FileSize}" />
+                    </StackPanel>
+                </DataTemplate>
+            </ListBox.ItemTemplate>
+        </ListBox>
     </Grid>
 </UserControl>

+ 3 - 1
Demo/Examples/Compdfkit_Tools/Common/HomePage/RecentFilesControl.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using Compdfkit_Tools.Helper;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -22,6 +23,7 @@ namespace Compdfkit_Tools.PDFControl
     {
         public RecentFilesControl()
         {
+            this.DataContext = FileHistoryHelper<PDFFileInfo>.Instance;
             InitializeComponent();
         }
     }

+ 1 - 0
Demo/Examples/Compdfkit_Tools/Compdfkit_Tools.csproj

@@ -138,6 +138,7 @@
     <Compile Include="Common\DeviceSerial\DeviceSerialControl.xaml.cs">
       <DependentUpon>DeviceSerialControl.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Common\Helper\FileHistoryHelper.cs" />
     <Compile Include="Common\Helper\PasswordHelper.cs" />
     <Compile Include="Common\PasswordControl\PasswordBoxControl.xaml.cs">
       <DependentUpon>PasswordBoxControl.xaml</DependentUpon>

+ 23 - 1
Demo/Examples/PDFViewer/App.xaml.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
+using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Threading.Tasks;
@@ -18,13 +19,14 @@ namespace PDFViewer
     public partial class App: Application
     {
         static public bool DefaultPDFLoaded = false;
-        public static List<string> OpenedFilePathList = new List<string>();
+        public static FilePathList OpenedFilePathList = new FilePathList();
 
         protected override void OnStartup(StartupEventArgs e)
         {
             string str = this.GetType().Assembly.Location;
             base.OnStartup(e);
             LicenseVerify();
+            FileHistoryHelper<PDFFileInfo>.Instance.LoadHistory();
         }
         
         private static bool LicenseVerify()
@@ -42,4 +44,24 @@ namespace PDFViewer
             return result;
         }
     }
+    public class FilePathList : List<string>
+    {
+        public new void Add(string item)
+        {
+            base.Add(item);
+            YourCustomFunction(item);
+        }
+
+        private void YourCustomFunction(string item)
+        {
+            PDFFileInfo fileInfo = new PDFFileInfo();
+            fileInfo.FilePath = item;
+            fileInfo.FileSize = CommonHelper.GetFileSize(fileInfo.FilePath);
+            fileInfo.OpenDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+            FileHistoryHelper<PDFFileInfo>.Instance.AddHistory(fileInfo);
+            FileHistoryHelper<PDFFileInfo>.Instance.SaveHistory();
+        }
+    }
+
+
 }