Skip to content

Commit

Permalink
Simplify FileController by using the new CollectionItemChanged event
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 19, 2023
1 parent ca78071 commit 16dbcf7
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ internal sealed class FileController
private readonly DelegateCommand closeAllCommand;
private readonly DelegateCommand saveCommand;
private readonly DelegateCommand saveAsCommand;
private readonly List<DocumentFile> observedDocumentFiles;
private DocumentFile? lastActiveDocumentFile;
private IWeakEventProxy? activeDocumentPropertyChangedProxy;
private int documentCounter;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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()
{
Expand Down Expand Up @@ -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))
Expand Down
7 changes: 3 additions & 4 deletions src/DotNetPad/DotNetPad.Applications/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentFile> documentFiles;
private readonly ReadOnlyObservableList<DocumentFile> readOnlyDocumentFiles;
private readonly ObservableList<DocumentFile> documentFiles;
private DocumentFile? activeDocumentFile;
private DocumentFile? lockedDocumentFile;
private ICommand newCSharpCommand = null!;
Expand All @@ -26,10 +25,10 @@ internal sealed class FileService : Model, IFileService
public FileService()
{
documentFiles = [];
readOnlyDocumentFiles = new(documentFiles);
DocumentFiles = new(documentFiles);
}

public IReadOnlyObservableList<DocumentFile> DocumentFiles => readOnlyDocumentFiles;
public ReadOnlyObservableList<DocumentFile> DocumentFiles { get; }

public DocumentFile? ActiveDocumentFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Waf.DotNetPad.Applications.Services;

public interface IDocumentService : INotifyPropertyChanged
{
IReadOnlyObservableList<DocumentFile> DocumentFiles { get; }
ReadOnlyObservableList<DocumentFile> DocumentFiles { get; }

DocumentFile? ActiveDocumentFile { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Waf.DotNetPad.Presentation.DesignData;

internal sealed class MockDocumentService : Model, IDocumentService
{
public IReadOnlyObservableList<DocumentFile> DocumentFiles { get; set; } = null!;
public ReadOnlyObservableList<DocumentFile> DocumentFiles { get; set; } = null!;

public DocumentFile? ActiveDocumentFile { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MockFileService : Model, IFileService

public ICommand SaveAsCommand { get; set; } = null!;

public IReadOnlyObservableList<DocumentFile> DocumentFiles { get; set; } = null!;
public ReadOnlyObservableList<DocumentFile> DocumentFiles { get; set; } = null!;

public DocumentFile? ActiveDocumentFile { get; set; }

Expand Down

0 comments on commit 16dbcf7

Please sign in to comment.