Start working on letting user pick output location

Not done yet. May need to refactor some code before CreatePDF() is called and do the file handling stuff before
This commit is contained in:
2026-04-23 11:48:30 +09:00
parent e813504453
commit 981114a222
2 changed files with 51 additions and 16 deletions
+1 -10
View File
@@ -85,15 +85,6 @@ class ReportPDFCreator : ChangeNotifier
// https://forum.pdfsharp.net/viewtopic.php?f=2&t=1025
public async Task<string?> CreatePDF(List<ReportFile> reportFiles, string reportTitle, string outputFolderPath, PDFFontResolver fontResolver, Settings appSettings)
{
// safety checks
var outputDir = appSettings.PDFOutputSaveLocation == Enums.PDFSaveLocation.BaseFolder
? outputFolderPath
: appSettings.OutputPdfDir; // TODO: adjust for new logic
if (!Directory.Exists(outputDir))
{
await DialogHost.Show(new WarningViewModel("Output directory not found! Please adjust your application Settings before continuing. Output directory: " + outputDir));
return null;
}
// setup globals and consts...
GlobalFontSettings.FontResolver = fontResolver;
GlobalFontSettings.FallbackFontResolver = new FailsafeFontResolver();
@@ -403,7 +394,7 @@ class ReportPDFCreator : ChangeNotifier
pdfRenderer.DocumentRenderer.PrepareDocument(); // needed if you make edits after first PrepareDocument() is called
pdfRenderer.RenderDocument();
// actually save to disk now
string outputPDFFilePath = Path.Join(outputDir, outputFileName);
string outputPDFFilePath = Path.Join(outputFolderPath, outputFileName);
_logger?.LogInfo("Saving PDF document to disk...");
pdfRenderer.PdfDocument.Save(outputPDFFilePath);
_logger?.LogInfo("Finished saving PDF output to: " + outputPDFFilePath);
@@ -11,6 +11,7 @@ using MayShow.Interfaces;
using MayShow.Models;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Avalonia.Threading;
namespace MayShow.ViewModels;
@@ -453,7 +454,7 @@ class CreatePDFReportViewModel : BaseViewModel, ICanCheckShutdown, ILogger
{
try
{
await Task.Run(() => CreatePDF(outputFolderPath: _pdfReport.BaseFolder));
await Task.Run(() => CreatePDF());
} catch (Exception e)
{
LogInfo("PDF process failed! Reason: " + e.Message);
@@ -510,16 +511,59 @@ class CreatePDFReportViewModel : BaseViewModel, ICanCheckShutdown, ILogger
}
}
private async Task CreatePDF(string outputFolderPath)
private async Task CreatePDF()
{
IsCreatingPDF = true;
var reportCreator = new ReportPDFCreator(this);
var outputPdfFile = await reportCreator.CreatePDF(ReportFiles.ToList(), ReportTitle, outputFolderPath, new PDFFontResolver(_processDir, this), _settings);
var outputDir = "";
switch (_settings.PDFOutputSaveLocation)
{
case Enums.PDFSaveLocation.BaseFolder:
outputDir = _pdfReport.BaseFolder;
break;
case Enums.PDFSaveLocation.AlwaysAsk:
await Dispatcher.UIThread.InvokeAsync(async () =>
{
var topLevel = TopLevelGrabber?.GetTopLevel();
if (topLevel != null)
{
var result = await topLevel.StorageProvider.SaveFilePickerWithResultAsync(new FilePickerSaveOptions
{
Title = "Choose PDF save location...",
FileTypeChoices = [FilePickerFileTypes.Pdf],
SuggestedFileType = FilePickerFileTypes.Pdf,
DefaultExtension = "pdf",
ShowOverwritePrompt = true,
});
if (result.File is not null)
{
var path = result.File.Path.AbsolutePath;
Console.WriteLine(path); // <--- this is what I want
}
}
});
IsCreatingPDF = false;
return;
break;
case Enums.PDFSaveLocation.OtherChosenDir:
outputDir = _settings.OutputPdfDir;
break;
}
// safety checks
if (!Directory.Exists(outputDir))
{
await DialogHost.Show(new WarningViewModel("Output directory not found! Please adjust your application Settings before continuing. Output directory: " + outputDir));
}
else
{
var outputPdfFile = await reportCreator.CreatePDF(ReportFiles.ToList(), ReportTitle, outputDir, new PDFFontResolver(_processDir, this), _settings);
if (!string.IsNullOrWhiteSpace(outputPdfFile))
{
await CreateAndSaveReportObjectAfterReportCreation();
OpenFolderForFileInFileViewer(outputPdfFile);
}
}
IsCreatingPDF = false;
}