Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added project singleton service #48

Merged
merged 15 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
/src/NodeDev.Blazor.MAUI/bin/Debug/net9.0-windows10.0.19041.0/win10-x64
/src/NodeDev.Blazor.MAUI/obj
/src/NodeDev.Blazor.MAUI/bin/Release/net9.0-windows10.0.19041.0/win-x64
/src/NodeDev.Blazor.Server/AppOptions.json
1 change: 1 addition & 0 deletions src/Blazor.Diagrams
Submodule Blazor.Diagrams added at ac4eef
1 change: 1 addition & 0 deletions src/Dis2Msil
Submodule Dis2Msil added at 780f50
6 changes: 3 additions & 3 deletions src/NodeDev.Blazor.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseAntiforgery();
Expand Down
75 changes: 75 additions & 0 deletions src/NodeDev.Blazor/Components/OpenProjectDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@using System.Text.Json
@using NodeDev.Blazor.Services
@using NodeDev.Core
@inject AppOptionsContainer AppOptionsContainer
@inject IDialogService DialogService
@inject ProjectService ProjectService
@inject ISnackbar Snackbar

<MudDialog>
<DialogContent>
<MudStack>
<MudPaper Class="full-width">
<MudList T="string" @bind-SelectedValue="ProjectName" ReadOnly="false">
@foreach (var item in RecentProjects)
{
<MudListItem Text="@item"></MudListItem>
}
</MudList>
</MudPaper>
</MudStack>
</DialogContent>
<DialogActions>
<MudButton Color="Color.Primary" OnClick="LoadProject" Disabled="ProjectName is null">Open</MudButton>
<MudButton Color="Color.Primary" OnClick="Close">Cancel</MudButton>
</DialogActions>
</MudDialog>

@code {
[CascadingParameter]
private MudDialogInstance MudDialog { get; set; } = null!;

private string? ProjectName { get; set; }
private List<string> RecentProjects { get; set; } = new List<string>();

private void Close() => MudDialog.Close(DialogResult.Ok(true));

protected override void OnInitialized()
{
base.OnInitialized();
RecentProjects = ListRecentProjects();
}

private List<string> ListRecentProjects()
{
if (AppOptionsContainer.AppOptions.ProjectsDirectory is null)
{
return [];
}
if (!Directory.Exists(AppOptionsContainer.AppOptions.ProjectsDirectory))
{
return [];
}
return Directory.EnumerateFiles(AppOptionsContainer.AppOptions.ProjectsDirectory, "*.ndproj").Select(Path.GetFileNameWithoutExtension).ToList()!;
}

private async Task LoadProject()
{
if (string.IsNullOrWhiteSpace(ProjectName))
{
return;
}
try
{
var projectPath = Path.Combine(AppOptionsContainer.AppOptions.ProjectsDirectory!, $"{ProjectName}.ndproj");
await ProjectService.LoadProjectFromFileAsync(projectPath);
}
catch (Exception ex)
{
Snackbar.Configuration.VisibleStateDuration = 10000;
Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
Snackbar.Add(ex.Message, Severity.Error);
}
MudDialog.Close(DialogResult.Ok(true));
}
}
38 changes: 38 additions & 0 deletions src/NodeDev.Blazor/Components/OptionsDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@using NodeDev.Blazor.Services
@inject IDialogService DialogService
@inject Services.AppOptionsContainer AppOptionsContainer

<MudDialog>
<TitleContent>
Options
</TitleContent>
<DialogContent>
<MudTextField @bind-Value="AppOptions.ProjectsDirectory" Label="Projects Directory" Variant="Variant.Text" AutoGrow data-test-id="optionsProjectDirectory"></MudTextField>
</DialogContent>
<DialogActions>
<MudButton Color="Color.Primary" OnClick="Close" data-test-id="optionsCancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Accept" data-test-id="optionsAccept">Ok</MudButton>
</DialogActions>
</MudDialog>

@code {
[CascadingParameter]
private MudDialogInstance MudDialog { get; set; } = null!;

private AppOptions AppOptions { get; set; } = null!;

protected override void OnInitialized()
{
base.OnInitialized();
AppOptions = AppOptionsContainer.AppOptions with { };
}

private void Accept()
{
AppOptionsContainer.AppOptions = AppOptions;
MudDialog.Close();
}

private void Close() => MudDialog.Close();

}
88 changes: 88 additions & 0 deletions src/NodeDev.Blazor/Components/ProjectToolbar.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
@using Microsoft.AspNetCore.Components.Forms
@using NodeDev.Blazor.Services
@using NodeDev.Core
@inject ProjectService ProjectService
@inject ISnackbar Snackbar
@inject AppOptionsContainer AppOptionsContainer
@inject IDialogService DialogService


<MudButton OnClick="Open" Class="ml-3" [email protected] data-test-id="openProject">Open</MudButton>
<MudButton OnClick="NewProject" Class="ml-3" [email protected] data-test-id="newProject">New Project</MudButton>
<MudButton OnClick="Save" Class="ml-3" data-test-id="save">Save</MudButton>
<MudButton OnClick="SaveAs" Class="ml-3" data-test-id="saveAs">Save As</MudButton>
<MudButton OnClick="Add" Class="ml-3">Add node</MudButton>
<MudButton OnClick="Run" Class="ml-3">Run</MudButton>
<MudButton OnClick="SwitchLiveDebugging">@(Project.IsLiveDebuggingEnabled ? "Stop Live Debugging" : "Start Live Debugging")</MudButton>
<MudSpacer />
<MudButton OnClick="OpenOptionsDialogAsync" Class="ml-3" data-test-id="options">Options</MudButton>
<MudIconButton Icon="@Icons.Material.Filled.MoreVert" Color="Color.Inherit" Edge="Edge.End" />


@code {

private Project Project => ProjectService.Project;

private DialogOptions DialogOptions => new DialogOptions { MaxWidth = MaxWidth.Medium, FullWidth = true };

private Task Open()
{
return DialogService.ShowAsync<OpenProjectDialog>("Open Project", DialogOptions);
}

private Task Save()
{
if (string.IsNullOrWhiteSpace(Project.Settings.ProjectName))
{
return SaveAs();
}

try
{
ProjectService.SaveProjectToFile();
Snackbar.Add("Project saved", Severity.Success);
}
catch (Exception ex)
{
Snackbar.Add(ex.Message, Severity.Error);
}

return Task.CompletedTask;
}

private Task SaveAs()
{
return DialogService.ShowAsync<SaveAsProjectDialog>("Save As Project", DialogOptions);
}

private void NewProject()
{
ProjectService.ChangeProject(Core.Project.CreateNewDefaultProject());
}

private void Add()
{
//GraphCanvas?.ShowAddNode();
}

public void Run()
{
new Thread(() =>
{
Project.Run(Project.IsLiveDebuggingEnabled ? Core.BuildOptions.Debug : Core.BuildOptions.Release);
}).Start();
}

private void SwitchLiveDebugging()
{
if (Project.IsLiveDebuggingEnabled)
Project.StopLiveDebugging();
else
Project.StartLiveDebugging();
}

private Task OpenOptionsDialogAsync()
{
return DialogService.ShowAsync<OptionsDialog>("Options", DialogOptions);
}
}
48 changes: 48 additions & 0 deletions src/NodeDev.Blazor/Components/SaveAsProjectDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@using System.Text.Json
@using NodeDev.Blazor.Services
@using NodeDev.Core
@inject AppOptionsContainer AppOptionsContainer
@inject IDialogService DialogService
@inject ProjectService ProjectService
@inject ISnackbar Snackbar

<MudDialog>
<DialogContent>
<MudTextField @bind-Value="ProjectName" Label="File Name" Variant="Variant.Text" data-test-id="saveAsProjectName"></MudTextField>
</DialogContent>
<DialogActions>
<MudButton Color="Color.Primary" OnClick="SaveProject" Disabled="ProjectName is null" data-test-id="saveAsSave">Save</MudButton>
<MudButton Color="Color.Primary" OnClick="Close" data-test-id="saveAsCancel">Cancel</MudButton>
</DialogActions>
</MudDialog>

@code {
[CascadingParameter]
private MudDialogInstance MudDialog { get; set; } = null!;

public string? ProjectName { get; set; }

private void Close() => MudDialog.Close(DialogResult.Ok(true));

protected override void OnInitialized()
{
base.OnInitialized();
ProjectName = ProjectService.Project.Settings.ProjectName;
}

private void SaveProject()
{
try
{
ProjectService.Project.Settings.ProjectName = ProjectName!;
ProjectService.SaveProjectToFile();
Snackbar.Add("Project saved", Severity.Success);
MudDialog.Close(DialogResult.Ok(ProjectName));
}
catch (Exception ex)
{
Snackbar.Add(ex.Message, Severity.Error);
}
}

}
Loading
Loading