Skip to content

Commit

Permalink
Writer: simplify code by using new C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 25, 2023
1 parent 8a64e06 commit 894a28e
Show file tree
Hide file tree
Showing 29 changed files with 63 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Initialize()
var catalog = new AggregateCatalog();
OnCatalogInitialize(catalog);

Container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
Container = new(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(Container);
Container.Compose(batch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ public void DocumentControllerConstructorTest()
}


private class TestDocumentController : DocumentController
private class TestDocumentController(IFileService fileService) : DocumentController(fileService)
{
public TestDocumentController(IFileService fileService) : base(fileService)
{
}

protected override void OnDocumentAdded(IDocument document) => throw new NotSupportedException();

protected override void OnDocumentRemoved(IDocument document) => throw new NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void SaveChangesTest()
};

var fileDialogService = Get<MockFileDialogService>();
fileDialogService.Result = new FileDialogResult();
fileDialogService.Result = new();

// This time we let the SaveChangesView to save the modified document
shellViewModel.ExitCommand.Execute(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public void IllegalDocumentCollectionChangeTest()
var documents = (ObservableList<IDocument>)documentsInfo.GetValue(fileService)!;

// Now we call a method that is not supported by the DocumentController base class
AssertHelper.ExpectedException<NotSupportedException>(() => documents.Clear());
AssertHelper.ExpectedException<NotSupportedException>(documents.Clear);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ public void CheckBaseImplementation()
}


private class MockDocumentTypeBase : DocumentType
private sealed class MockDocumentTypeBase(string description, string fileExtension) : DocumentType(description, fileExtension)
{
public MockDocumentTypeBase(string description, string fileExtension) : base(description, fileExtension) { }

public IDocument CallNewCore() => NewCore();

public IDocument CallOpenCore(string fileName) => OpenCore(fileName);

public void CallSaveCore(IDocument document, string fileName) => SaveCore(document, fileName);
}

private class DocumentBaseMock : Document
private sealed class DocumentBaseMock(MockDocumentTypeBase documentType) : Document(documentType)
{
public DocumentBaseMock(MockDocumentTypeBase documentType) : base(documentType) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace Test.Writer.Applications.Documents;

public class MockRichTextDocument : Document, IRichTextDocument
public class MockRichTextDocument(MockRichTextDocumentType documentType) : Document(documentType), IRichTextDocument
{
public MockRichTextDocument(MockRichTextDocumentType documentType) : base(documentType)
{
}
public object Content { get; } = new();

public object Content { get; } = new object();

public object CloneContent() => new object();
public object CloneContent() => new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
namespace Test.Writer.Applications.Documents;

[Export(typeof(IRichTextDocumentType)), Export]
public class MockRichTextDocumentType : DocumentType, IRichTextDocumentType
public class MockRichTextDocumentType(string description, string fileExtension) : DocumentType(description, fileExtension), IRichTextDocumentType
{
public MockRichTextDocumentType() : this("Mock RTF", ".rtf") { }

public MockRichTextDocumentType(string description, string fileExtension) : base(description, fileExtension)
{
CanSaveResult = true;
}

public bool CanSaveResult { get; set; }
public bool CanSaveResult { get; set; } = true;

public DocumentOperation DocumentOperation { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
namespace Test.Writer.Applications.Documents;

[Export(typeof(IXpsExportDocumentType)), Export]
internal class MockXpsExportDocumentType : MockRichTextDocumentType, IXpsExportDocumentType
internal class MockXpsExportDocumentType() : MockRichTextDocumentType("Mock XPS", ".xps"), IXpsExportDocumentType
{
public MockXpsExportDocumentType() : base("Mock XPS", ".xps")
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public void SaveChangesViewModelCloseTest()
var documentType = new MockRichTextDocumentType();
var documents = new[]
{
documentType.New(),
documentType.New(),
documentType.New()
};
documentType.New(),
documentType.New(),
documentType.New()
};

var view = new MockSaveChangesView();
var viewModel = new SaveChangesViewModel(view) { Documents = documents };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void ShowAndClose()

// Close the ShellView via the ExitCommand
cancelClosing = false;
shellViewModel.ExitCommand = new DelegateCommand(() => shellViewModel.Close());
shellViewModel.ExitCommand = new DelegateCommand(shellViewModel.Close);
shellViewModel.ExitCommand.Execute(null);
Assert.IsFalse(shellView.IsVisible);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override void OnInitialize()
[TestMethod]
public void DefaultZoomsTest()
{
AssertHelper.SequenceEqual(new[] { "200%", "150%", "125%", "100%", "75%", "50%" }, zoomViewModel.DefaultZooms);
AssertHelper.SequenceEqual([ "200%", "150%", "125%", "100%", "75%", "50%" ], zoomViewModel.DefaultZooms);
}

[TestMethod]
Expand Down Expand Up @@ -68,12 +68,8 @@ public void SyncWithShellServiceTest()
}


private class MockZoomViewModel : ZoomViewModel<IView>
private class MockZoomViewModel(IView view, IShellService shellService) : ZoomViewModel<IView>(view, shellService)
{
public MockZoomViewModel(IView view, IShellService shellService) : base(view, shellService)
{
}

public bool FitToWidthCalled { get; set; }

protected override void FitToWidthCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected DocumentController(IFileService fileService)
{
this.fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
fileService.PropertyChanged += FileServicePropertyChanged;
((INotifyCollectionChanged)fileService.Documents).CollectionChanged += DocumentsCollectionChanged;
fileService.Documents.CollectionChanged += DocumentsCollectionChanged;
}

protected abstract void OnDocumentAdded(IDocument document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@ public FileController(IMessageService messageService, ISystemService systemServi
this.shellService = shellService;
this.fileService = fileService;
this.saveChangesViewModelFactory = saveChangesViewModelFactory;
documentTypes = new() { richTextDocumentType, xpsExportDocumentType };
newCommand = new DelegateCommand(NewCommand);
openCommand = new DelegateCommand(OpenCommand);
closeCommand = new DelegateCommand(CloseCommand, CanCloseCommand);
saveCommand = new DelegateCommand(SaveCommand, CanSaveCommand);
saveAsCommand = new DelegateCommand(SaveAsCommand, CanSaveAsCommand);
documentTypes = [ richTextDocumentType, xpsExportDocumentType ];
fileService.NewCommand = newCommand = new(NewCommand);
fileService.OpenCommand = openCommand = new(OpenCommand);
fileService.CloseCommand = closeCommand = new(CloseCommand, CanCloseCommand);
fileService.SaveCommand = saveCommand = new(SaveCommand, CanSaveCommand);
fileService.SaveAsCommand = saveAsCommand = new(SaveAsCommand, CanSaveAsCommand);
settings = settingsService.Get<AppSettings>();
this.fileService.NewCommand = newCommand;
this.fileService.OpenCommand = openCommand;
this.fileService.CloseCommand = closeCommand;
this.fileService.SaveCommand = saveCommand;
this.fileService.SaveAsCommand = saveAsCommand;
recentFileList = settings.RecentFileList ?? new RecentFileList();
recentFileList = settings.RecentFileList ?? new();
this.fileService.RecentFileList = recentFileList;
fileService.PropertyChanged += FileServicePropertyChanged;
}
Expand All @@ -67,7 +62,7 @@ private IDocument? ActiveDocument

public IDocument? Open(string fileName)
{
if (string.IsNullOrEmpty(fileName)) throw new ArgumentException("The argument fileName must not be null or empty.");
if (string.IsNullOrEmpty(fileName)) throw new ArgumentException("The argument fileName must not be null or empty.", nameof(fileName));
return OpenCore(fileName!);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ModuleController(ISystemService systemService, ShellService shellService,
this.startViewModel = startViewModel.Value;
shellService.ShellView = this.shellViewModel.View;
this.shellViewModel.Closing += ShellViewModelClosing;
exitCommand = new DelegateCommand(Close);
exitCommand = new(Close);
}

public void Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public PrintController(IFileService fileService, IPrintDialogService printDialog
this.printDialogService = printDialogService;
this.shellViewModel = shellViewModel;
this.printPreviewViewModelFactory = printPreviewViewModelFactory;
printPreviewCommand = new DelegateCommand(ShowPrintPreview, CanShowPrintPreview);
printCommand = new DelegateCommand(PrintDocument, CanPrintDocument);
closePrintPreviewCommand = new DelegateCommand(ClosePrintPreview);
printPreviewCommand = new(ShowPrintPreview, CanShowPrintPreview);
printCommand = new(PrintDocument, CanPrintDocument);
closePrintPreviewCommand = new(ClosePrintPreview);
fileService.PropertyChanged += FileServicePropertyChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ internal class RichTextDocumentController : DocumentController
private readonly IFileService fileService;
private readonly MainViewModel mainViewModel;
private readonly ExportFactory<RichTextViewModel> richTextViewModelFactory;
private readonly Dictionary<IRichTextDocument, RichTextViewModel> richTextViewModels;
private readonly Dictionary<IRichTextDocument, RichTextViewModel> richTextViewModels = [];

[ImportingConstructor]
public RichTextDocumentController(IFileService fileService, MainViewModel mainViewModel, ExportFactory<RichTextViewModel> richTextViewModelFactory) : base(fileService)
{
this.fileService = fileService;
this.mainViewModel = mainViewModel;
this.richTextViewModelFactory = richTextViewModelFactory;
richTextViewModels = new Dictionary<IRichTextDocument, RichTextViewModel>();
mainViewModel.PropertyChanged += MainViewModelPropertyChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ namespace Waf.Writer.Applications.Services;
[Export(typeof(IFileService)), Export]
internal class FileService : Model, IFileService
{
private readonly ObservableList<IDocument> documents;
private readonly ObservableList<IDocument> documents = [];
private IDocument? activeDocument;

[ImportingConstructor]
public FileService()
{
documents = new();
Documents = new ReadOnlyObservableList<IDocument>(documents);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public IZoomCommands ActiveZoomCommands
}


private class DisabledEditingCommands : Model, IEditingCommands
private sealed class DisabledEditingCommands : Model, IEditingCommands
{
public bool IsBold { get; set; }

Expand All @@ -58,9 +58,9 @@ private class DisabledEditingCommands : Model, IEditingCommands
public bool IsSpellCheckEnabled { get; set; }
}

private class DisabledZoomCommands : Model, IZoomCommands
private sealed class DisabledZoomCommands : Model, IZoomCommands
{
public IReadOnlyList<string> DefaultZooms => Array.Empty<string>();
public IReadOnlyList<string> DefaultZooms => [];

public double Zoom
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public MainViewModel(IMainView view, IShellService shellService, IFileService fi
{
this.shellService = shellService;
FileService = fileService;
DocumentViews = new();
DocumentViews.CollectionChanged += DocumentViewsCollectionChanged;
fileService.PropertyChanged += FileServicePropertyChanged;
}
Expand All @@ -27,7 +26,7 @@ public MainViewModel(IMainView view, IShellService shellService, IFileService fi

public object StartView { get; set; } = null!;

public ObservableList<object> DocumentViews { get; }
public ObservableList<object> DocumentViews { get; } = [];

public object? ActiveDocumentView
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class SaveChangesViewModel : ViewModel<ISaveChangesView>
[ImportingConstructor]
public SaveChangesViewModel(ISaveChangesView view) : base(view)
{
yesCommand = new DelegateCommand(() => Close(true));
noCommand = new DelegateCommand(() => Close(false));
yesCommand = new(() => Close(true));
noCommand = new(() => Close(false));
}

public string Title => ApplicationInfo.ProductName;
Expand All @@ -26,7 +26,7 @@ public SaveChangesViewModel(ISaveChangesView view) : base(view)

public ICommand NoCommand => noCommand;

public IReadOnlyList<IDocument> Documents { get; set; } = Array.Empty<IDocument>();
public IReadOnlyList<IDocument> Documents { get; set; } = [];

public bool? ShowDialog(object owner)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Waf.Writer.Applications.Views;

public enum ContentViewState
{
StartViewVisible,
DocumentViewVisible
}

public interface IMainView : IView
{
ContentViewState ContentViewState { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public void ConvertTest()
{
var converter = new TabFileNameConverter();

Assert.AreEqual("Document 1.rtf", converter.Convert(new object[] { "Document 1.rtf", false }, null, null, null));
Assert.AreEqual("Document 1.rtf*", converter.Convert(new object[] { "Document 1.rtf", true }, null, null, null));
Assert.AreEqual("This is a document with a very long f...", converter.Convert(new object[] { "This is a document with a very long file name.rtf", false }, null, null, null));
Assert.AreEqual("Document 1.rtf", converter.Convert([ "Document 1.rtf", false ], null, null, null));
Assert.AreEqual("Document 1.rtf*", converter.Convert([ "Document 1.rtf", true ], null, null, null));
Assert.AreEqual("This is a document with a very long f...", converter.Convert([ "This is a document with a very long file name.rtf", false ], null, null, null));

Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert(new[] { new object(), new object() }, typeof(string), null, null));
Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert([ new object(), new object() ], typeof(string), null, null));

AssertHelper.ExpectedException<NotSupportedException>(() => converter.ConvertBack(null, null, null, null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public void ConvertTest()
{
var converter = new TitleConverter();

Assert.AreEqual("App Title", converter.Convert(new[] { "App Title", null }, null, null, CultureInfo.InvariantCulture));
Assert.AreEqual("Document1.rtf - App Title", converter.Convert(new[] { "App Title", "Document1.rtf" }, null, null, CultureInfo.InvariantCulture));
Assert.AreEqual("App Title", converter.Convert([ "App Title", null ], null, null, CultureInfo.InvariantCulture));
Assert.AreEqual("Document1.rtf - App Title", converter.Convert([ "App Title", "Document1.rtf" ], null, null, CultureInfo.InvariantCulture));

Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert(null, null, null, null));
Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert(new[] { "Wrong" }, null, null, null));
Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert(new object[] { 4, 2 }, null, null, null));
Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert([ "Wrong" ], null, null, null));
Assert.AreEqual(DependencyProperty.UnsetValue, converter.Convert([ 4, 2 ], null, null, null));

AssertHelper.ExpectedException<NotSupportedException>(() => converter.ConvertBack(null, null, null, null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ namespace Test.Writer.Presentation
public class PresentationTest : ApplicationsTest
{
// List of exports which must use the real implementation instead of the mock (integration test)
private static readonly Type[] exportNames = new[]
{
private static readonly Type[] exportNames = [
typeof(IRichTextDocumentType), typeof(RichTextDocumentType),
typeof(IRichTextDocument), typeof(RichTextDocument),
typeof(IXpsExportDocumentType), typeof(XpsExportDocumentType)
};
];

protected override void OnCatalogInitialize(AggregateCatalog catalog)
{
Expand Down
4 changes: 2 additions & 2 deletions src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ protected override void OnStartup(StartupEventArgs e)
DispatcherUnhandledException += AppDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
#endif
catalog = new AggregateCatalog();
catalog = new();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IMessageService).Assembly)); // WinApplicationFramework
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); // Writer.Presentation
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ShellViewModel).Assembly)); // Writer.Applications
container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
container = new(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(container);
container.Compose(batch);
Expand Down
Loading

0 comments on commit 894a28e

Please sign in to comment.