From 16dbcf7928a8ef92a8b0f9d1481503da71eb6e91 Mon Sep 17 00:00:00 2001 From: jbe2277 Date: Sun, 19 Nov 2023 15:14:28 +0100 Subject: [PATCH] Simplify FileController by using the new CollectionItemChanged event --- .../Controllers/FileController.cs | 33 +------------------ .../Services/FileService.cs | 7 ++-- .../Services/IDocumentService.cs | 2 +- .../DesignData/MockDocumentService.cs | 2 +- .../DesignData/MockFileService.cs | 2 +- 5 files changed, 7 insertions(+), 39 deletions(-) diff --git a/src/DotNetPad/DotNetPad.Applications/Controllers/FileController.cs b/src/DotNetPad/DotNetPad.Applications/Controllers/FileController.cs index 317bd51..a331aa1 100644 --- a/src/DotNetPad/DotNetPad.Applications/Controllers/FileController.cs +++ b/src/DotNetPad/DotNetPad.Applications/Controllers/FileController.cs @@ -32,7 +32,6 @@ internal sealed class FileController private readonly DelegateCommand closeAllCommand; private readonly DelegateCommand saveCommand; private readonly DelegateCommand saveAsCommand; - private readonly List observedDocumentFiles; private DocumentFile? lastActiveDocumentFile; private IWeakEventProxy? activeDocumentPropertyChangedProxy; private int documentCounter; @@ -63,7 +62,6 @@ public FileController(IMessageService messageService, IFileDialogService fileDia this.fileService.SaveCommand = saveCommand; this.fileService.SaveAsCommand = saveAsCommand; - observedDocumentFiles = []; WeakEvent.PropertyChanged.Add(fileService, FileServicePropertyChanged); shellService.Closing += ShellServiceClosing; } @@ -76,7 +74,7 @@ private DocumentFile? ActiveDocumentFile private DocumentFile? LockedDocumentFile => fileService.LockedDocumentFile; - public void Initialize() => fileService.DocumentFiles.CollectionChanged += FileServiceDocumentsCollectionChanged; + public void Initialize() => fileService.DocumentFiles.CollectionItemChanged += DocumentFilePropertyChanged; public async void Run() { @@ -252,35 +250,6 @@ private void SaveCore(DocumentFile document, string fileName) } } - private void FileServiceDocumentsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) - { - if (e.Action == NotifyCollectionChangedAction.Add) - { - foreach (DocumentFile documentFile in e.NewItems!) - { - documentFile.PropertyChanged += DocumentFilePropertyChanged; - observedDocumentFiles.Add(documentFile); - } - } - else if (e.Action == NotifyCollectionChangedAction.Remove) - { - foreach (DocumentFile documentFile in e.OldItems!) - { - documentFile.PropertyChanged -= DocumentFilePropertyChanged; - observedDocumentFiles.Remove(documentFile); - } - } - else if (e.Action == NotifyCollectionChangedAction.Reset && !fileService.DocumentFiles.Any()) - { - foreach (DocumentFile documentFile in observedDocumentFiles) - { - documentFile.PropertyChanged -= DocumentFilePropertyChanged; - } - observedDocumentFiles.Clear(); - } - else throw new NotSupportedException("The CollectionChangedAction '" + e.Action + "' is not supported."); - } - private async void DocumentFilePropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(DocumentFile.LoadError)) diff --git a/src/DotNetPad/DotNetPad.Applications/Services/FileService.cs b/src/DotNetPad/DotNetPad.Applications/Services/FileService.cs index ff2f6de..7ff7c58 100644 --- a/src/DotNetPad/DotNetPad.Applications/Services/FileService.cs +++ b/src/DotNetPad/DotNetPad.Applications/Services/FileService.cs @@ -8,8 +8,7 @@ namespace Waf.DotNetPad.Applications.Services; [Export(typeof(IDocumentService)), Export(typeof(IFileService)), Export] internal sealed class FileService : Model, IFileService { - private readonly ObservableCollection documentFiles; - private readonly ReadOnlyObservableList readOnlyDocumentFiles; + private readonly ObservableList documentFiles; private DocumentFile? activeDocumentFile; private DocumentFile? lockedDocumentFile; private ICommand newCSharpCommand = null!; @@ -26,10 +25,10 @@ internal sealed class FileService : Model, IFileService public FileService() { documentFiles = []; - readOnlyDocumentFiles = new(documentFiles); + DocumentFiles = new(documentFiles); } - public IReadOnlyObservableList DocumentFiles => readOnlyDocumentFiles; + public ReadOnlyObservableList DocumentFiles { get; } public DocumentFile? ActiveDocumentFile { diff --git a/src/DotNetPad/DotNetPad.Applications/Services/IDocumentService.cs b/src/DotNetPad/DotNetPad.Applications/Services/IDocumentService.cs index 65b56ce..6701bb1 100644 --- a/src/DotNetPad/DotNetPad.Applications/Services/IDocumentService.cs +++ b/src/DotNetPad/DotNetPad.Applications/Services/IDocumentService.cs @@ -4,7 +4,7 @@ namespace Waf.DotNetPad.Applications.Services; public interface IDocumentService : INotifyPropertyChanged { - IReadOnlyObservableList DocumentFiles { get; } + ReadOnlyObservableList DocumentFiles { get; } DocumentFile? ActiveDocumentFile { get; set; } diff --git a/src/DotNetPad/DotNetPad.Presentation/DesignData/MockDocumentService.cs b/src/DotNetPad/DotNetPad.Presentation/DesignData/MockDocumentService.cs index e780c39..ffea0d7 100644 --- a/src/DotNetPad/DotNetPad.Presentation/DesignData/MockDocumentService.cs +++ b/src/DotNetPad/DotNetPad.Presentation/DesignData/MockDocumentService.cs @@ -5,7 +5,7 @@ namespace Waf.DotNetPad.Presentation.DesignData; internal sealed class MockDocumentService : Model, IDocumentService { - public IReadOnlyObservableList DocumentFiles { get; set; } = null!; + public ReadOnlyObservableList DocumentFiles { get; set; } = null!; public DocumentFile? ActiveDocumentFile { get; set; } diff --git a/src/DotNetPad/DotNetPad.Presentation/DesignData/MockFileService.cs b/src/DotNetPad/DotNetPad.Presentation/DesignData/MockFileService.cs index 4aca630..5e1ee78 100644 --- a/src/DotNetPad/DotNetPad.Presentation/DesignData/MockFileService.cs +++ b/src/DotNetPad/DotNetPad.Presentation/DesignData/MockFileService.cs @@ -25,7 +25,7 @@ public class MockFileService : Model, IFileService public ICommand SaveAsCommand { get; set; } = null!; - public IReadOnlyObservableList DocumentFiles { get; set; } = null!; + public ReadOnlyObservableList DocumentFiles { get; set; } = null!; public DocumentFile? ActiveDocumentFile { get; set; }