From e1c235202d2ac23a17856aa68ff621802d37af9b Mon Sep 17 00:00:00 2001 From: MRmlik12 Date: Mon, 20 May 2024 22:56:59 +0200 Subject: [PATCH] Cleanup project --- src/NoteSHR.Browser/BrowserFilePicker.cs | 7 +-- src/NoteSHR.Browser/NoteSHR.Browser.csproj | 2 +- src/NoteSHR.Browser/Program.cs | 5 +- src/NoteSHR.Core/FodyWeavers.xml | 2 +- src/NoteSHR.Core/Models/Board.cs | 2 +- src/NoteSHR.Core/Models/IDataPersistence.cs | 4 +- src/NoteSHR.Core/Models/Note.cs | 15 ++--- src/NoteSHR.Core/NoteSHR.Core.csproj | 4 +- src/NoteSHR.Desktop/Program.cs | 1 - src/NoteSHR.File/BoardConverter.cs | 57 ++++++++---------- src/NoteSHR.File/BoardExporter.cs | 7 +-- src/NoteSHR.File/BoardImporter.cs | 11 ++-- src/NoteSHR.File/FileBlob.cs | 39 ++++++------ src/NoteSHR.File/NoteSHR.File.csproj | 4 +- src/NoteSHR.File/Utils/PathUtils.cs | 6 +- src/NoteSHR/App.axaml.cs | 2 +- src/NoteSHR/Assets/Icons/add-outline.svg | 3 +- .../Assets/Icons/caret-down-outline.svg | 3 +- src/NoteSHR/Assets/Icons/caret-up-outline.svg | 3 +- src/NoteSHR/Assets/Icons/close-outline.svg | 5 +- .../Assets/Icons/color-palette-outline.svg | 4 +- src/NoteSHR/Assets/Icons/pencil-outline.svg | 3 +- src/NoteSHR/Assets/Icons/save-outline.svg | 4 +- src/NoteSHR/Assets/Icons/trash-outline.svg | 10 +++- .../Components/Check/CheckComponent.axaml | 3 +- .../Check/CheckComponentViewModel.cs | 2 +- .../Image/ImageComponentViewModel.cs | 22 +++---- .../Components/List/ListComponentViewModel.cs | 5 +- .../Components/NoteNode/NoteNodeComponent.cs | 33 ++++++----- .../Components/Text/TextComponentViewModel.cs | 2 +- .../Core/Converters/ColorToHexConverter.cs | 2 +- src/NoteSHR/Core/Helpers/ColorHelper.cs | 4 +- src/NoteSHR/Core/Helpers/HttpHelper.cs | 4 +- src/NoteSHR/Core/Helpers/IFilePicker.cs | 4 +- src/NoteSHR/NoteSHR.csproj | 8 +-- src/NoteSHR/Styles/InputStyle.axaml | 6 +- src/NoteSHR/ViewLocator.cs | 1 - .../ViewModels/BoardToolbarViewModel.cs | 4 -- src/NoteSHR/ViewModels/BoardViewModel.cs | 59 +++++++------------ src/NoteSHR/Views/BoardToolbar.axaml | 9 +-- src/NoteSHR/Views/BoardToolbar.axaml.cs | 22 +++---- src/NoteSHR/Views/BoardView.axaml | 2 +- src/NoteSHR/Views/MainView.axaml | 4 +- .../NoteSHR.File.Tests/BoardConverterTests.cs | 22 +++---- test/NoteSHR.File.Tests/BoardExporterTests.cs | 14 ++--- test/NoteSHR.File.Tests/BoardImporterTests.cs | 17 +++--- .../NoteSHR.File.Tests.csproj | 49 +++++++-------- 47 files changed, 232 insertions(+), 269 deletions(-) diff --git a/src/NoteSHR.Browser/BrowserFilePicker.cs b/src/NoteSHR.Browser/BrowserFilePicker.cs index b9b3e02..db3bbed 100644 --- a/src/NoteSHR.Browser/BrowserFilePicker.cs +++ b/src/NoteSHR.Browser/BrowserFilePicker.cs @@ -1,23 +1,20 @@ using System.Runtime.InteropServices.JavaScript; using System.Threading.Tasks; -using Microsoft.AspNetCore.Components; -using Microsoft.JSInterop; using NoteSHR.Core.Services; namespace NoteSHR.Browser; public class BrowserFilePicker : IFilePicker { - public BrowserFilePicker() { Task.Run(async () => await JSHost.ImportAsync("FilePicker", "./filePicker.js")); } - + public async Task GetFileUrl() { var url = await FilePickerEmbed.OpenFilePicker(); - + return url; } } diff --git a/src/NoteSHR.Browser/NoteSHR.Browser.csproj b/src/NoteSHR.Browser/NoteSHR.Browser.csproj index a421f55..99e3fc3 100644 --- a/src/NoteSHR.Browser/NoteSHR.Browser.csproj +++ b/src/NoteSHR.Browser/NoteSHR.Browser.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/NoteSHR.Browser/Program.cs b/src/NoteSHR.Browser/Program.cs index 36fc7e7..b29aaa3 100644 --- a/src/NoteSHR.Browser/Program.cs +++ b/src/NoteSHR.Browser/Program.cs @@ -21,9 +21,6 @@ private static Task Main(string[] args) public static AppBuilder BuildAvaloniaApp() { return AppBuilder.Configure() - .AfterSetup(a => - { - App.FilePicker = new BrowserFilePicker(); - }); + .AfterSetup(a => { App.FilePicker = new BrowserFilePicker(); }); } } \ No newline at end of file diff --git a/src/NoteSHR.Core/FodyWeavers.xml b/src/NoteSHR.Core/FodyWeavers.xml index 63fc148..a42ba18 100644 --- a/src/NoteSHR.Core/FodyWeavers.xml +++ b/src/NoteSHR.Core/FodyWeavers.xml @@ -1,3 +1,3 @@  - + \ No newline at end of file diff --git a/src/NoteSHR.Core/Models/Board.cs b/src/NoteSHR.Core/Models/Board.cs index 74c31af..8df0f0b 100644 --- a/src/NoteSHR.Core/Models/Board.cs +++ b/src/NoteSHR.Core/Models/Board.cs @@ -4,5 +4,5 @@ public class Board { public Guid Id { get; set; } public string Name { get; set; } - public List Notes { get; set; } + public List Notes { get; set; } } \ No newline at end of file diff --git a/src/NoteSHR.Core/Models/IDataPersistence.cs b/src/NoteSHR.Core/Models/IDataPersistence.cs index 9346580..f51d0e8 100644 --- a/src/NoteSHR.Core/Models/IDataPersistence.cs +++ b/src/NoteSHR.Core/Models/IDataPersistence.cs @@ -11,9 +11,9 @@ void ConvertValues(object data) foreach (var property in properties) { var value = property.GetValue(data); - if (value == null) + if (value == null) continue; - + GetType().GetProperty(property.Name)?.SetValue(this, value); } } diff --git a/src/NoteSHR.Core/Models/Note.cs b/src/NoteSHR.Core/Models/Note.cs index aee19cb..742aeb5 100644 --- a/src/NoteSHR.Core/Models/Note.cs +++ b/src/NoteSHR.Core/Models/Note.cs @@ -5,10 +5,9 @@ namespace NoteSHR.Core.Models; public class Note(double x, double y, string headerColor) : INotifyPropertyChanged { - private ObservableCollection _nodes = new (); - private double _width = 200.0d; + private string _backgroundColor = "#222222"; private string _headerColor = headerColor; - private string _backgroundColor = "#222222"; + private double _width = 200.0d; public Guid Id { get; init; } = Guid.NewGuid(); @@ -42,14 +41,7 @@ public double Width } } - public ObservableCollection Nodes - { - get => _nodes; - private set - { - _nodes = value; - } - } + public ObservableCollection Nodes { get; private set; } = new(); public string HeaderColor { @@ -60,6 +52,7 @@ public string HeaderColor OnPropertyChanged(nameof(HeaderColor)); } } + public string BackgroundColor { get => _backgroundColor; diff --git a/src/NoteSHR.Core/NoteSHR.Core.csproj b/src/NoteSHR.Core/NoteSHR.Core.csproj index 8a5cc4a..f848ae7 100644 --- a/src/NoteSHR.Core/NoteSHR.Core.csproj +++ b/src/NoteSHR.Core/NoteSHR.Core.csproj @@ -7,11 +7,11 @@ - + - + diff --git a/src/NoteSHR.Desktop/Program.cs b/src/NoteSHR.Desktop/Program.cs index 414f2b2..59c32dc 100644 --- a/src/NoteSHR.Desktop/Program.cs +++ b/src/NoteSHR.Desktop/Program.cs @@ -1,7 +1,6 @@ using System; using Avalonia; using Avalonia.ReactiveUI; -using Avalonia.Svg.Skia; namespace NoteSHR.Desktop; diff --git a/src/NoteSHR.File/BoardConverter.cs b/src/NoteSHR.File/BoardConverter.cs index 4575834..d45f8e4 100644 --- a/src/NoteSHR.File/BoardConverter.cs +++ b/src/NoteSHR.File/BoardConverter.cs @@ -10,22 +10,19 @@ internal static class BoardConverter private static bool FilterByBlobUrl(PropertyInfo property, object data) { var value = property.GetValue(data); - - if (value is string str) - { - return str.Contains("blob://"); - } + + if (value is string str) return str.Contains("blob://"); return false; } - + internal static BoardScheme ConvertToScheme(string boardName, List notes) { var scheme = new BoardScheme { Id = Guid.NewGuid(), Name = boardName, - LastModifiedAt = DateTime.Now, + LastModifiedAt = DateTime.Now }; scheme.Notes = notes.Select(note => new NoteScheme @@ -39,28 +36,27 @@ internal static BoardScheme ConvertToScheme(string boardName, List notes) Nodes = note.Nodes.Select(node => { object? data = null; - + if (node.ViewModel is IDataPersistence) { data = ((IDataPersistence)node.ViewModel).ExportValues(); - foreach (var property in data.GetType().GetProperties().Where(x => x.PropertyType == typeof(FileBlob))) + foreach (var property in data.GetType().GetProperties() + .Where(x => x.PropertyType == typeof(FileBlob))) { var fileBlob = property.GetValue(data) as FileBlob; - + if (fileBlob != null) - { Task.Run(async () => { var fileId = Guid.NewGuid(); - - var path = await fileBlob.Write( fileId.ToString()); + + var path = await fileBlob.Write(fileId.ToString()); property.SetValue(data, path); }); - } - } + } } - + return new NodeScheme { Id = node.Id, @@ -71,55 +67,50 @@ internal static BoardScheme ConvertToScheme(string boardName, List notes) }; }) }); - + return scheme; } internal static Board ConvertBack(BoardScheme scheme) { var notes = new List(); - + foreach (var noteScheme in scheme.Notes) { var note = new Note(noteScheme.X, noteScheme.Y, noteScheme.HeaderColor) { Id = noteScheme.Id, BackgroundColor = noteScheme.BackgroundColor, - Width = noteScheme.Width, + Width = noteScheme.Width }; - + foreach (var nodeScheme in noteScheme.Nodes) { var assembly = Assembly.Load($"{nodeScheme.Assembly}"); - + var type = assembly?.GetType(nodeScheme.Component); - if (type == null) - { - continue; - } + if (type == null) continue; var viewModelType = assembly?.GetType(nodeScheme.ViewModelType); - if (viewModelType == null) - { - continue; - } + if (viewModelType == null) continue; var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType); if (viewModel is IDataPersistence persistence) { - foreach (var properties in nodeScheme.Data.GetType().GetProperties().Where(x => FilterByBlobUrl(x, nodeScheme.Data))) + foreach (var properties in nodeScheme.Data.GetType().GetProperties() + .Where(x => FilterByBlobUrl(x, nodeScheme.Data))) { var path = properties.GetValue(nodeScheme.Data) as string; var blob = new FileBlob(scheme.Id, path); properties.SetValue(nodeScheme.Data, blob); } - + persistence?.ConvertValues(nodeScheme.Data); } - + note.Nodes.Add(new Node(Guid.NewGuid(), type, viewModel)); } - + notes.Add(note); } diff --git a/src/NoteSHR.File/BoardExporter.cs b/src/NoteSHR.File/BoardExporter.cs index fd2e166..2cae937 100644 --- a/src/NoteSHR.File/BoardExporter.cs +++ b/src/NoteSHR.File/BoardExporter.cs @@ -21,11 +21,8 @@ public static async Task ExportToFile(List notes, string name, str boardFile.Close(); var destinationPath = $"{path}/{name}.zip"; - if (System.IO.File.Exists(destinationPath)) - { - System.IO.File.Delete(destinationPath); - } - + if (System.IO.File.Exists(destinationPath)) System.IO.File.Delete(destinationPath); + ZipFile.CreateFromDirectory(tempFolder, destinationPath); return destinationPath; diff --git a/src/NoteSHR.File/BoardImporter.cs b/src/NoteSHR.File/BoardImporter.cs index ea08b3e..d61b35d 100644 --- a/src/NoteSHR.File/BoardImporter.cs +++ b/src/NoteSHR.File/BoardImporter.cs @@ -11,17 +11,14 @@ public static class BoardImporter public static async Task ImportFromFile(string path) { var zipFile = ZipFile.Open(path, ZipArchiveMode.Read); - + var schemeFile = zipFile.Entries.First(e => e.Name == "board.json"); await using var schemeStream = schemeFile.Open(); var jsonContent = await new StreamReader(schemeStream).ReadToEndAsync(); var scheme = JsonConvert.DeserializeObject(jsonContent); - - if (scheme is null) - { - throw new CorruptedBoardFileException("Cannot read board contents from file"); - } - + + if (scheme is null) throw new CorruptedBoardFileException("Cannot read board contents from file"); + var board = BoardConverter.ConvertBack(scheme); return board; diff --git a/src/NoteSHR.File/FileBlob.cs b/src/NoteSHR.File/FileBlob.cs index b1ff9ef..855c4d0 100644 --- a/src/NoteSHR.File/FileBlob.cs +++ b/src/NoteSHR.File/FileBlob.cs @@ -4,9 +4,25 @@ namespace NoteSHR.File; public class FileBlob(Stream? stream = null) : IDisposable, IAsyncDisposable { + internal FileBlob(Guid projectId, string blobUri) : this() + { + ProjectId = projectId; + Uri = new Uri(blobUri); + } + private Guid ProjectId { get; set; } private Uri Uri { get; } - + + public async ValueTask DisposeAsync() + { + if (stream != null) await stream.DisposeAsync(); + } + + public void Dispose() + { + stream?.Dispose(); + } + internal void SetProjectId(Guid projectId) { ProjectId = projectId; @@ -14,7 +30,7 @@ internal void SetProjectId(Guid projectId) internal async Task Write(string filename) { - var internalPath = $"/assets/{filename}"; + var internalPath = $"/assets/{filename}"; await using var fileStream = System.IO.File.Create($"{PathUtils.GetTemporaryPath(ProjectId)}/{internalPath}"); await stream!.CopyToAsync(fileStream); @@ -30,23 +46,4 @@ private string GetFilePath(string filename) { return $"{PathUtils.GetTemporaryPath(ProjectId)}/assets/{filename}"; } - - public void Dispose() - { - stream?.Dispose(); - } - - internal FileBlob(Guid projectId, string blobUri) : this() - { - ProjectId = projectId; - Uri = new Uri(blobUri); - } - - public async ValueTask DisposeAsync() - { - if (stream != null) - { - await stream.DisposeAsync(); - } - } } \ No newline at end of file diff --git a/src/NoteSHR.File/NoteSHR.File.csproj b/src/NoteSHR.File/NoteSHR.File.csproj index 5492a96..f3833db 100644 --- a/src/NoteSHR.File/NoteSHR.File.csproj +++ b/src/NoteSHR.File/NoteSHR.File.csproj @@ -7,11 +7,11 @@ - + - + diff --git a/src/NoteSHR.File/Utils/PathUtils.cs b/src/NoteSHR.File/Utils/PathUtils.cs index 5c23c19..7fe3ab9 100644 --- a/src/NoteSHR.File/Utils/PathUtils.cs +++ b/src/NoteSHR.File/Utils/PathUtils.cs @@ -2,6 +2,8 @@ public static class PathUtils { - public static string GetTemporaryPath(Guid guid) - => Path.Combine(Path.GetTempPath(), $"NoteSHR/{guid}"); + public static string GetTemporaryPath(Guid guid) + { + return Path.Combine(Path.GetTempPath(), $"NoteSHR/{guid}"); + } } \ No newline at end of file diff --git a/src/NoteSHR/App.axaml.cs b/src/NoteSHR/App.axaml.cs index 823d5aa..508f1af 100644 --- a/src/NoteSHR/App.axaml.cs +++ b/src/NoteSHR/App.axaml.cs @@ -10,7 +10,7 @@ namespace NoteSHR; public class App : Application { public static IFilePicker FilePicker { get; set; } - + public override void Initialize() { AvaloniaXamlLoader.Load(this); diff --git a/src/NoteSHR/Assets/Icons/add-outline.svg b/src/NoteSHR/Assets/Icons/add-outline.svg index 114f55a..b1ee85b 100644 --- a/src/NoteSHR/Assets/Icons/add-outline.svg +++ b/src/NoteSHR/Assets/Icons/add-outline.svg @@ -1,3 +1,4 @@ - + \ No newline at end of file diff --git a/src/NoteSHR/Assets/Icons/caret-down-outline.svg b/src/NoteSHR/Assets/Icons/caret-down-outline.svg index ab261b9..b8e80c8 100644 --- a/src/NoteSHR/Assets/Icons/caret-down-outline.svg +++ b/src/NoteSHR/Assets/Icons/caret-down-outline.svg @@ -1,3 +1,4 @@ - + \ No newline at end of file diff --git a/src/NoteSHR/Assets/Icons/caret-up-outline.svg b/src/NoteSHR/Assets/Icons/caret-up-outline.svg index 7108885..2744552 100644 --- a/src/NoteSHR/Assets/Icons/caret-up-outline.svg +++ b/src/NoteSHR/Assets/Icons/caret-up-outline.svg @@ -1,3 +1,4 @@ - + \ No newline at end of file diff --git a/src/NoteSHR/Assets/Icons/close-outline.svg b/src/NoteSHR/Assets/Icons/close-outline.svg index 5d783ae..9498d96 100644 --- a/src/NoteSHR/Assets/Icons/close-outline.svg +++ b/src/NoteSHR/Assets/Icons/close-outline.svg @@ -1 +1,4 @@ - \ No newline at end of file + + + \ No newline at end of file diff --git a/src/NoteSHR/Assets/Icons/color-palette-outline.svg b/src/NoteSHR/Assets/Icons/color-palette-outline.svg index 5498f60..bd2349c 100644 --- a/src/NoteSHR/Assets/Icons/color-palette-outline.svg +++ b/src/NoteSHR/Assets/Icons/color-palette-outline.svg @@ -1,5 +1,7 @@ - + diff --git a/src/NoteSHR/Assets/Icons/pencil-outline.svg b/src/NoteSHR/Assets/Icons/pencil-outline.svg index 656722c..45ead28 100644 --- a/src/NoteSHR/Assets/Icons/pencil-outline.svg +++ b/src/NoteSHR/Assets/Icons/pencil-outline.svg @@ -1,3 +1,4 @@ - + \ No newline at end of file diff --git a/src/NoteSHR/Assets/Icons/save-outline.svg b/src/NoteSHR/Assets/Icons/save-outline.svg index bc041cd..ab127b4 100644 --- a/src/NoteSHR/Assets/Icons/save-outline.svg +++ b/src/NoteSHR/Assets/Icons/save-outline.svg @@ -1,3 +1,5 @@ - + \ No newline at end of file diff --git a/src/NoteSHR/Assets/Icons/trash-outline.svg b/src/NoteSHR/Assets/Icons/trash-outline.svg index 92c6332..e762c7d 100644 --- a/src/NoteSHR/Assets/Icons/trash-outline.svg +++ b/src/NoteSHR/Assets/Icons/trash-outline.svg @@ -1,5 +1,9 @@ - - - + + + \ No newline at end of file diff --git a/src/NoteSHR/Components/Check/CheckComponent.axaml b/src/NoteSHR/Components/Check/CheckComponent.axaml index a7184f3..f578458 100644 --- a/src/NoteSHR/Components/Check/CheckComponent.axaml +++ b/src/NoteSHR/Components/Check/CheckComponent.axaml @@ -8,7 +8,8 @@ x:DataType="vm:CheckComponentViewModel"> - + diff --git a/src/NoteSHR/Components/Check/CheckComponentViewModel.cs b/src/NoteSHR/Components/Check/CheckComponentViewModel.cs index c116f97..0bdb976 100644 --- a/src/NoteSHR/Components/Check/CheckComponentViewModel.cs +++ b/src/NoteSHR/Components/Check/CheckComponentViewModel.cs @@ -8,7 +8,7 @@ public class CheckComponentViewModel : ViewModelBase, IDataPersistence { [Reactive] public bool Checked { get; set; } [Reactive] public string? Text { get; set; } - + public object ExportValues() { return new diff --git a/src/NoteSHR/Components/Image/ImageComponentViewModel.cs b/src/NoteSHR/Components/Image/ImageComponentViewModel.cs index f74ac4d..ee2cf56 100644 --- a/src/NoteSHR/Components/Image/ImageComponentViewModel.cs +++ b/src/NoteSHR/Components/Image/ImageComponentViewModel.cs @@ -14,7 +14,7 @@ namespace NoteSHR.Components.Image; -public class ImageComponentViewModel : ViewModelBase, IDataPersistence +public class ImageComponentViewModel : ViewModelBase, IDataPersistence { public ImageComponentViewModel() { @@ -23,15 +23,12 @@ public ImageComponentViewModel() if (OperatingSystem.IsBrowser()) { var url = await App.FilePicker.GetFileUrl(); - if (string.IsNullOrEmpty(url)) - { - return; - } - + if (string.IsNullOrEmpty(url)) return; + Image = await HttpHelper.GetBitmatFromUrl(url); return; } - + var topLevel = TopLevel.GetTopLevel(args.Source as Button); var fileOptions = new FilePickerOpenOptions { @@ -47,14 +44,11 @@ public ImageComponentViewModel() Image = await ImageHelper.LoadFromFileSystem(file[0]); }); - + this.WhenAnyValue(vm => vm.Image) .Subscribe(image => { - if (image != null) - { - ImageSelected = true; - } + if (image != null) ImageSelected = true; }); } @@ -67,10 +61,10 @@ public object ExportValues() { var stream = new MemoryStream(); Image?.Save(stream); - + return new { - Image = new FileBlob(stream) + Image = new FileBlob(stream) }; } } \ No newline at end of file diff --git a/src/NoteSHR/Components/List/ListComponentViewModel.cs b/src/NoteSHR/Components/List/ListComponentViewModel.cs index a2c2d0c..66a61da 100644 --- a/src/NoteSHR/Components/List/ListComponentViewModel.cs +++ b/src/NoteSHR/Components/List/ListComponentViewModel.cs @@ -5,7 +5,6 @@ using Avalonia.Input; using NoteSHR.Core.Models; using NoteSHR.Core.ViewModel; -using NoteSHR.ViewModels; using ReactiveUI; using ReactiveUI.Fody.Helpers; @@ -37,13 +36,13 @@ public ListComponentViewModel() public ReactiveCommand AddRowCommand { get; set; } public ReactiveCommand ChangePrefixTypeCommand { get; set; } - + public object ExportValues() { return new { Rows, - PrefixType, + PrefixType }; } } \ No newline at end of file diff --git a/src/NoteSHR/Components/NoteNode/NoteNodeComponent.cs b/src/NoteSHR/Components/NoteNode/NoteNodeComponent.cs index e865c2a..d2dabe1 100644 --- a/src/NoteSHR/Components/NoteNode/NoteNodeComponent.cs +++ b/src/NoteSHR/Components/NoteNode/NoteNodeComponent.cs @@ -12,7 +12,6 @@ using Avalonia.Media; using NoteSHR.Components.NoteNode.EventArgs; using NoteSHR.Core.Models; -using NoteSHR.ViewModels; namespace NoteSHR.Components.NoteNode; @@ -42,9 +41,10 @@ public class NoteNodeComponent : UserControl public static readonly RoutedEvent MoveNodeEvent = RoutedEvent.Register(nameof(MoveNodeEvent), RoutingStrategies.Direct); - private readonly StackPanel _stackPanel; private readonly List _nodeGrids = new(); + private readonly StackPanel _stackPanel; + public NoteNodeComponent() { MinHeight = 200; @@ -104,7 +104,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang { if (deleteMode) { - var deleteButton = GetDeleteButtonControl((Guid)x.DataContext); + var deleteButton = GetDeleteButtonControl((Guid)x.DataContext); x.Children.Add(deleteButton); } @@ -148,13 +148,14 @@ private void NodesOnCollectionChanged(object? sender, NotifyCollectionChangedEve _nodeGrids.Clear(); foreach (var nodeVm in Nodes) { - var grid = InitializeNodeGrid(nodeVm); + var grid = InitializeNodeGrid(nodeVm); _stackPanel.Children.Add(grid); } return; } - else if (e.Action == NotifyCollectionChangedAction.Remove) + + if (e.Action == NotifyCollectionChangedAction.Remove) { foreach (var nodeVm in e.OldItems.Cast()) { @@ -242,9 +243,9 @@ private Grid GetEditModeControl(Guid id) { Name = MoveUpButtonName, Path = "avares://NoteSHR/Assets/Icons/caret-up-outline.svg", - DataContext = id, + DataContext = id }; - + Avalonia.Svg.Skia.Svg.SetCss(caretUpIcon, ".foreground { fill: #FFFFFF; }"); moveUpButton.Children.Add(caretUpIcon); @@ -261,10 +262,10 @@ private Grid GetEditModeControl(Guid id) Path = "avares://NoteSHR/Assets/Icons/caret-down-outline.svg", DataContext = id }; - + Avalonia.Svg.Skia.Svg.SetCss(caretDownIcon, ".foreground { fill: #FFFFFF; }"); moveDownButton.Children.Add(caretDownIcon); - + moveUpButton.PointerPressed += EditModeButtonClicked; moveDownButton.PointerPressed += EditModeButtonClicked; @@ -277,33 +278,33 @@ private Grid GetEditModeControl(Guid id) editModeGrid.Children.Add(moveUpButton); editModeGrid.Children.Add(moveDownButton); - + return editModeGrid; } - private Button GetDeleteButtonControl(Guid id) + private Button GetDeleteButtonControl(Guid id) { var deleteButton = new Button { Name = DeleteButtonName, Background = Brushes.Transparent, - DataContext = id, + DataContext = id }; - + var deleteIcon = new Avalonia.Svg.Skia.Svg(new Uri("avares://NoteSHR/Assets/Icons/trash-outline.svg")) { Path = "avares://NoteSHR/Assets/Icons/trash-outline.svg", Width = 16, - Height = 16, + Height = 16 }; Avalonia.Svg.Skia.Svg.SetCss(deleteIcon, ".foreground { stroke: #FF0000; }"); deleteButton.Content = deleteIcon; - + Grid.SetColumn(deleteButton, 0); deleteButton.Click += (_, _) => RaiseEvent(new DeleteNodeEventArgs(DeleteNodeEvent, NoteId, id)); - + return deleteButton; } diff --git a/src/NoteSHR/Components/Text/TextComponentViewModel.cs b/src/NoteSHR/Components/Text/TextComponentViewModel.cs index f2cb3ae..7c82570 100644 --- a/src/NoteSHR/Components/Text/TextComponentViewModel.cs +++ b/src/NoteSHR/Components/Text/TextComponentViewModel.cs @@ -7,7 +7,7 @@ namespace NoteSHR.Components.Text; public class TextComponentViewModel : ViewModelBase, IDataPersistence { [Reactive] public string? Text { get; set; } - + public object ExportValues() { return new diff --git a/src/NoteSHR/Core/Converters/ColorToHexConverter.cs b/src/NoteSHR/Core/Converters/ColorToHexConverter.cs index 919a644..cf32891 100644 --- a/src/NoteSHR/Core/Converters/ColorToHexConverter.cs +++ b/src/NoteSHR/Core/Converters/ColorToHexConverter.cs @@ -15,7 +15,7 @@ public class ColorToHexConverter : IValueConverter public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) { var color = (Color)value; - + return color.ToString(); } } \ No newline at end of file diff --git a/src/NoteSHR/Core/Helpers/ColorHelper.cs b/src/NoteSHR/Core/Helpers/ColorHelper.cs index e2d7d42..23b9f8b 100644 --- a/src/NoteSHR/Core/Helpers/ColorHelper.cs +++ b/src/NoteSHR/Core/Helpers/ColorHelper.cs @@ -5,12 +5,12 @@ namespace NoteSHR.Core.Helpers; public static class ColorHelper { private static readonly string[] NoteColors = { "0F528C", "9ADE86", "FFC000" }; - + public static string GenerateColor() { var random = new Random(); var color = $"#{NoteColors[random.Next(0, NoteColors.Length)]}"; - + return color; } } \ No newline at end of file diff --git a/src/NoteSHR/Core/Helpers/HttpHelper.cs b/src/NoteSHR/Core/Helpers/HttpHelper.cs index d4f4d12..f0c659c 100644 --- a/src/NoteSHR/Core/Helpers/HttpHelper.cs +++ b/src/NoteSHR/Core/Helpers/HttpHelper.cs @@ -9,10 +9,10 @@ public static class HttpHelper public static async Task GetBitmatFromUrl(string url) { using var client = new HttpClient(); - + var response = await client.GetAsync(url); await using var stream = await response.Content.ReadAsStreamAsync(); - + return new Bitmap(stream); } } \ No newline at end of file diff --git a/src/NoteSHR/Core/Helpers/IFilePicker.cs b/src/NoteSHR/Core/Helpers/IFilePicker.cs index 171c53c..4cb18cf 100644 --- a/src/NoteSHR/Core/Helpers/IFilePicker.cs +++ b/src/NoteSHR/Core/Helpers/IFilePicker.cs @@ -3,6 +3,6 @@ namespace NoteSHR.Core.Services; public interface IFilePicker -{ - Task GetFileUrl(); +{ + Task GetFileUrl(); } \ No newline at end of file diff --git a/src/NoteSHR/NoteSHR.csproj b/src/NoteSHR/NoteSHR.csproj index 85d121b..88e531d 100644 --- a/src/NoteSHR/NoteSHR.csproj +++ b/src/NoteSHR/NoteSHR.csproj @@ -12,9 +12,9 @@ - + - + @@ -29,7 +29,7 @@ - - + + diff --git a/src/NoteSHR/Styles/InputStyle.axaml b/src/NoteSHR/Styles/InputStyle.axaml index 4b55ef7..f91c125 100644 --- a/src/NoteSHR/Styles/InputStyle.axaml +++ b/src/NoteSHR/Styles/InputStyle.axaml @@ -1,9 +1,9 @@  - + - - + + \ No newline at end of file diff --git a/src/NoteSHR/ViewLocator.cs b/src/NoteSHR/ViewLocator.cs index a594f23..5cb776e 100644 --- a/src/NoteSHR/ViewLocator.cs +++ b/src/NoteSHR/ViewLocator.cs @@ -2,7 +2,6 @@ using Avalonia.Controls; using Avalonia.Controls.Templates; using NoteSHR.Core.ViewModel; -using NoteSHR.ViewModels; namespace NoteSHR; diff --git a/src/NoteSHR/ViewModels/BoardToolbarViewModel.cs b/src/NoteSHR/ViewModels/BoardToolbarViewModel.cs index 918f2dd..e3394a8 100644 --- a/src/NoteSHR/ViewModels/BoardToolbarViewModel.cs +++ b/src/NoteSHR/ViewModels/BoardToolbarViewModel.cs @@ -4,8 +4,4 @@ namespace NoteSHR.ViewModels; public class BoardToolbarViewModel : ViewModelBase { - public BoardToolbarViewModel() - { - - } } \ No newline at end of file diff --git a/src/NoteSHR/ViewModels/BoardViewModel.cs b/src/NoteSHR/ViewModels/BoardViewModel.cs index dd349d6..bdea74a 100644 --- a/src/NoteSHR/ViewModels/BoardViewModel.cs +++ b/src/NoteSHR/ViewModels/BoardViewModel.cs @@ -32,23 +32,22 @@ public BoardViewModel() LastMousePosition = args.GetPosition(null); if (args.Source is not Canvas) { - if (args.Source is not Grid header) + if (args.Source is not Grid header) return; - if (header?.Name == "Header") - { - _noteMoveState = true; - } + if (header?.Name == "Header") _noteMoveState = true; return; - }; + } + + ; if (!args.GetCurrentPoint(null).Properties.IsLeftButtonPressed) return; var position = args.GetPosition(null); - + var note = new Note(position.X, position.Y, ColorHelper.GenerateColor()); - + Notes.Add(note); }); @@ -59,30 +58,24 @@ public BoardViewModel() MoveNoteCommand = ReactiveCommand.Create((PointerEventArgs args) => { - if (!_noteMoveState) - { - return; - } + if (!_noteMoveState) return; var noteHeader = (Grid)args.Source!; var noteComponent = (Grid)noteHeader.Parent; var p = args.GetPosition(null); var note = Notes.SingleOrDefault(x => x.Id == ((Note)noteComponent.DataContext).Id); - + if (note != null) { note.X = p.X; note.Y = p.Y; - } + } }); UpdateNoteLocation = ReactiveCommand.Create((PointerReleasedEventArgs args) => { - if (!_noteMoveState) - { - return; - } - + if (!_noteMoveState) return; + var id = ((args.Source as Grid)?.DataContext as Note)?.Id; if (id == null) return; @@ -90,7 +83,7 @@ public BoardViewModel() if (pointerPoint.Properties.PointerUpdateKind != PointerUpdateKind.LeftButtonReleased) return; _noteMoveState = false; - + var note = Notes.SingleOrDefault(x => x.Id == id); if (note != null) { @@ -117,15 +110,9 @@ public BoardViewModel() note.Nodes.Add(new Node(Guid.NewGuid(), componentType, componentVm)); }); - ChangeDeleteModeStateCommand = ReactiveCommand.Create(() => - { - DeleteMode = !DeleteMode; - }); + ChangeDeleteModeStateCommand = ReactiveCommand.Create(() => { DeleteMode = !DeleteMode; }); - ChangeEditModeStateCommand = ReactiveCommand.Create(() => - { - EditMode = !EditMode; - }); + ChangeEditModeStateCommand = ReactiveCommand.Create(() => { EditMode = !EditMode; }); DeleteNoteNodeCommand = ReactiveCommand.Create((DeleteNodeEventArgs args) => { @@ -136,11 +123,12 @@ public BoardViewModel() MoveNoteNodeCommand = ReactiveCommand.Create((MoveNodeEventArgs args) => { var sourceNoteIndex = Notes.IndexOf(Notes.Where(x => x.Id == args.NoteId).Single()); - var nodeToMoveIndex = Notes[sourceNoteIndex].Nodes.IndexOf(Notes[sourceNoteIndex].Nodes.Single(x => x.Id == args.NodeToMoveId)); + var nodeToMoveIndex = Notes[sourceNoteIndex].Nodes + .IndexOf(Notes[sourceNoteIndex].Nodes.Single(x => x.Id == args.NodeToMoveId)); var sourceNodeIndex = nodeToMoveIndex + (int)args.MoveOptions; - + if (sourceNodeIndex > Notes[sourceNoteIndex].Nodes.Count - 1 || sourceNodeIndex < 0) return; - + Notes[sourceNoteIndex].Nodes.Move(sourceNodeIndex, nodeToMoveIndex); }); @@ -152,14 +140,11 @@ public BoardViewModel() Title = "Select path to export board", AllowMultiple = false }; - + var folderPicker = await topLevel.StorageProvider.OpenFolderPickerAsync(saveFilePickerOptions); - if (folderPicker.Count == 0) - { - return; - } - + if (folderPicker.Count == 0) return; + await BoardExporter.ExportToFile(Notes.ToList(), Name!, folderPicker[0].Path.AbsolutePath); }); } diff --git a/src/NoteSHR/Views/BoardToolbar.axaml b/src/NoteSHR/Views/BoardToolbar.axaml index f78f3cb..840ad0f 100644 --- a/src/NoteSHR/Views/BoardToolbar.axaml +++ b/src/NoteSHR/Views/BoardToolbar.axaml @@ -5,11 +5,12 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="NoteSHR.Views.BoardToolbar"> - + - - - + \ No newline at end of file diff --git a/src/NoteSHR/Views/BoardToolbar.axaml.cs b/src/NoteSHR/Views/BoardToolbar.axaml.cs index 4c2d3ef..be28d12 100644 --- a/src/NoteSHR/Views/BoardToolbar.axaml.cs +++ b/src/NoteSHR/Views/BoardToolbar.axaml.cs @@ -6,31 +6,31 @@ namespace NoteSHR.Views; -public partial class BoardToolbar : UserControl +public partial class BoardToolbar : UserControl { public static readonly RoutedEvent ExportBoardEvent = RoutedEvent.Register(nameof(ExportBoard), RoutingStrategies.Direct); - + public static readonly StyledProperty BoardNameProperty = AvaloniaProperty.Register(nameof(BoardName), defaultBindingMode: BindingMode.TwoWay); - public event EventHandler ExportBoard - { - add => AddHandler(ExportBoardEvent, value); - remove => RemoveHandler(ExportBoardEvent, value); + public BoardToolbar() + { + InitializeComponent(); } - + public string BoardName { get => GetValue(BoardNameProperty); set => SetValue(BoardNameProperty, value); } - - public BoardToolbar() + + public event EventHandler ExportBoard { - InitializeComponent(); + add => AddHandler(ExportBoardEvent, value); + remove => RemoveHandler(ExportBoardEvent, value); } - + private void ExportBoardButton_OnClick(object? sender, RoutedEventArgs e) { RaiseEvent(new RoutedEventArgs(ExportBoardEvent)); diff --git a/src/NoteSHR/Views/BoardView.axaml b/src/NoteSHR/Views/BoardView.axaml index 6f40841..e2d3628 100644 --- a/src/NoteSHR/Views/BoardView.axaml +++ b/src/NoteSHR/Views/BoardView.axaml @@ -26,7 +26,7 @@ - + diff --git a/src/NoteSHR/Views/MainView.axaml b/src/NoteSHR/Views/MainView.axaml index 7d1e03e..5605f80 100644 --- a/src/NoteSHR/Views/MainView.axaml +++ b/src/NoteSHR/Views/MainView.axaml @@ -7,11 +7,11 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="NoteSHR.Views.MainView" x:DataType="vm:MainViewModel"> - + - + diff --git a/test/NoteSHR.File.Tests/BoardConverterTests.cs b/test/NoteSHR.File.Tests/BoardConverterTests.cs index 76bdf01..f413119 100644 --- a/test/NoteSHR.File.Tests/BoardConverterTests.cs +++ b/test/NoteSHR.File.Tests/BoardConverterTests.cs @@ -12,11 +12,11 @@ public void ExportBoardToScheme_VerifyProperties() { var notes = new List { - new (100, 200, "#ffffff"), - new (200, 600, "#000000"), - new (0, 0, "#ffffff") + new(100, 200, "#ffffff"), + new(200, 600, "#000000"), + new(0, 0, "#ffffff") }; - + notes[0].Nodes.Add(new Node(Guid.NewGuid(), typeof(TextComponentControl), new TextComponentViewModel { Text = "Hello, World!" @@ -25,16 +25,16 @@ public void ExportBoardToScheme_VerifyProperties() { Text = "What's up?" })); - + const string boardName = "Hello"; - + var scheme = BoardConverter.ConvertToScheme(boardName, notes); scheme.LastModifiedAt.Should().BeSameDateAs(DateTime.Now); scheme.Name.Should().Be(boardName); scheme.Notes.Count().Should().Be(3); - foreach (var item in scheme.Notes.Select((value, i) => new {i, value })) + foreach (var item in scheme.Notes.Select((value, i) => new { i, value })) { item.value.Id.Should().Be(notes[item.i].Id); item.value.X.Should().Be(notes[item.i].X); @@ -45,12 +45,12 @@ public void ExportBoardToScheme_VerifyProperties() item.value.Nodes.Count().Should().Be(notes[item.i].Nodes.Count); } } - + [Fact] public void ExportBoardToScheme_ShouldNotesBeEmpty() { var notes = new List(); - + const string boardName = "empty"; var scheme = BoardConverter.ConvertToScheme(boardName, notes); @@ -96,7 +96,7 @@ public void ImportBoardFromScheme_VerifyProperties() Component = typeof(TextComponentControl).FullName, Assembly = nameof(NoteSHR), ViewModelType = typeof(TextComponentViewModel).FullName, - Data = new + Data = new { Text = "What's up?" } @@ -111,7 +111,7 @@ public void ImportBoardFromScheme_VerifyProperties() board.Name.Should().Be(scheme.Name); board.Id.Should().Be(scheme.Id); board.Notes.Count.Should().Be(scheme.Notes.Count()); - + foreach (var item in scheme.Notes.Select((value, i) => new { i, value })) { var note = board.Notes[item.i]; diff --git a/test/NoteSHR.File.Tests/BoardExporterTests.cs b/test/NoteSHR.File.Tests/BoardExporterTests.cs index dd5d118..59e1139 100644 --- a/test/NoteSHR.File.Tests/BoardExporterTests.cs +++ b/test/NoteSHR.File.Tests/BoardExporterTests.cs @@ -10,17 +10,17 @@ public class BoardExporterTests { private const string TestImagePath = "./Assets/image.png"; private const string BoardName = "test-board"; - + [Fact] public async Task TryExportBoard_OK() { var notes = new List { - new (100, 200, "#ffffff"), - new (200, 600, "#000000"), - new (0, 0, "#ffffff") + new(100, 200, "#ffffff"), + new(200, 600, "#000000"), + new(0, 0, "#ffffff") }; - + notes[0].Nodes.Add(new Node(Guid.NewGuid(), typeof(TextComponentControl), new TextComponentViewModel { Text = "Hello, World!" @@ -29,7 +29,7 @@ public async Task TryExportBoard_OK() { Text = "What's up?" })); - + notes[1].Nodes.Add(new Node(Guid.NewGuid(), typeof(CheckComponent), new CheckComponentViewModel { Text = "Prepare meal", @@ -37,7 +37,7 @@ public async Task TryExportBoard_OK() })); var outputPath = await BoardExporter.ExportToFile(notes, BoardName, "."); - + var zip = ZipFile.Open(outputPath, ZipArchiveMode.Read); zip.Entries.Select(x => x.FullName).Count().Should().Be(1); } diff --git a/test/NoteSHR.File.Tests/BoardImporterTests.cs b/test/NoteSHR.File.Tests/BoardImporterTests.cs index 0bcfb90..07a97a2 100644 --- a/test/NoteSHR.File.Tests/BoardImporterTests.cs +++ b/test/NoteSHR.File.Tests/BoardImporterTests.cs @@ -7,19 +7,18 @@ namespace NoteSHR.File.Tests; public class BoardImporterTests { - private const string BoardName = "import-test"; - + [Fact] public async Task TryImportBoardFromFile_VerifyProperties() { var notes = new List { - new (100, 200, "#ffffff"), - new (200, 600, "#000000"), - new (0, 0, "#ffffff") + new(100, 200, "#ffffff"), + new(200, 600, "#000000"), + new(0, 0, "#ffffff") }; - + notes[0].Nodes.Add(new Node(Guid.NewGuid(), typeof(TextComponentControl), new TextComponentViewModel { Text = "Hello, World!" @@ -28,16 +27,16 @@ public async Task TryImportBoardFromFile_VerifyProperties() { Text = "What's up?" })); - + notes[1].Nodes.Add(new Node(Guid.NewGuid(), typeof(CheckComponent), new CheckComponentViewModel { Text = "Prepare meal", Checked = true })); - + var outputPath = await BoardExporter.ExportToFile(notes, BoardName, "."); var importedNotes = await BoardImporter.ImportFromFile(outputPath); - + importedNotes.Notes.Should().HaveCount(notes.Count); } } \ No newline at end of file diff --git a/test/NoteSHR.File.Tests/NoteSHR.File.Tests.csproj b/test/NoteSHR.File.Tests/NoteSHR.File.Tests.csproj index 1650175..fd675a4 100644 --- a/test/NoteSHR.File.Tests/NoteSHR.File.Tests.csproj +++ b/test/NoteSHR.File.Tests/NoteSHR.File.Tests.csproj @@ -1,30 +1,33 @@ - - net8.0 - enable - enable - - false - true - + + net8.0 + enable + enable - - - - - - - + false + true + - - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + - - - - - + + + + + + + + +