Skip to content

Commit

Permalink
Cleanup project
Browse files Browse the repository at this point in the history
  • Loading branch information
MRmlik12 committed May 20, 2024
1 parent c29739a commit e1c2352
Show file tree
Hide file tree
Showing 47 changed files with 232 additions and 269 deletions.
7 changes: 2 additions & 5 deletions src/NoteSHR.Browser/BrowserFilePicker.cs
Original file line number Diff line number Diff line change
@@ -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<string?> GetFileUrl()
{
var url = await FilePickerEmbed.OpenFilePicker();

return url;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NoteSHR.Browser/NoteSHR.Browser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="Avalonia.Browser" Version="11.0.10"/>
<PackageReference Include="Avalonia.Browser.Blazor" Version="11.0.10" />
<PackageReference Include="Avalonia.Browser.Blazor" Version="11.0.10"/>
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions src/NoteSHR.Browser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ private static Task Main(string[] args)
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.AfterSetup(a =>
{
App.FilePicker = new BrowserFilePicker();
});
.AfterSetup(a => { App.FilePicker = new BrowserFilePicker(); });
}
}
2 changes: 1 addition & 1 deletion src/NoteSHR.Core/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ReactiveUI />
<ReactiveUI/>
</Weavers>
2 changes: 1 addition & 1 deletion src/NoteSHR.Core/Models/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class Board
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Note> Notes { get; set; }
public List<Note> Notes { get; set; }
}
4 changes: 2 additions & 2 deletions src/NoteSHR.Core/Models/IDataPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/NoteSHR.Core/Models/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ namespace NoteSHR.Core.Models;

public class Note(double x, double y, string headerColor) : INotifyPropertyChanged
{
private ObservableCollection<Node> _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();

Expand Down Expand Up @@ -42,14 +41,7 @@ public double Width
}
}

public ObservableCollection<Node> Nodes
{
get => _nodes;
private set
{
_nodes = value;
}
}
public ObservableCollection<Node> Nodes { get; private set; } = new();

public string HeaderColor
{
Expand All @@ -60,6 +52,7 @@ public string HeaderColor
OnPropertyChanged(nameof(HeaderColor));
}
}

public string BackgroundColor
{
get => _backgroundColor;
Expand Down
4 changes: 2 additions & 2 deletions src/NoteSHR.Core/NoteSHR.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41"/>
</ItemGroup>

<ItemGroup>
<Folder Include="Helpers\" />
<Folder Include="Helpers\"/>
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion src/NoteSHR.Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Avalonia;
using Avalonia.ReactiveUI;
using Avalonia.Svg.Skia;

namespace NoteSHR.Desktop;

Expand Down
57 changes: 24 additions & 33 deletions src/NoteSHR.File/BoardConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Note> notes)
{
var scheme = new BoardScheme
{
Id = Guid.NewGuid(),
Name = boardName,
LastModifiedAt = DateTime.Now,
LastModifiedAt = DateTime.Now
};

scheme.Notes = notes.Select(note => new NoteScheme
Expand All @@ -39,28 +36,27 @@ internal static BoardScheme ConvertToScheme(string boardName, List<Note> 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,
Expand All @@ -71,55 +67,50 @@ internal static BoardScheme ConvertToScheme(string boardName, List<Note> notes)
};
})
});

return scheme;
}

internal static Board ConvertBack(BoardScheme scheme)
{
var notes = new List<Note>();

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);
}

Expand Down
7 changes: 2 additions & 5 deletions src/NoteSHR.File/BoardExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ public static async Task<string> ExportToFile(List<Note> 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;
Expand Down
11 changes: 4 additions & 7 deletions src/NoteSHR.File/BoardImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ public static class BoardImporter
public static async Task<Board> 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<BoardScheme>(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;
Expand Down
39 changes: 18 additions & 21 deletions src/NoteSHR.File/FileBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ 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;
}

internal async Task<string> 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);

Expand All @@ -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();
}
}
}
4 changes: 2 additions & 2 deletions src/NoteSHR.File/NoteSHR.File.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NoteSHR.Core\NoteSHR.Core.csproj" />
<ProjectReference Include="..\NoteSHR.Core\NoteSHR.Core.csproj"/>
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions src/NoteSHR.File/Utils/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
2 changes: 1 addition & 1 deletion src/NoteSHR/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NoteSHR;
public class App : Application
{
public static IFilePicker FilePicker { get; set; }

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
Expand Down
Loading

0 comments on commit e1c2352

Please sign in to comment.