WIP: Add iOS version #10

Draft
Deadpikle wants to merge 67 commits from feature/ios into main
2 changed files with 52 additions and 52 deletions
Showing only changes of commit 004647008f - Show all commits
+7 -22
View File
@@ -83,7 +83,7 @@ class ReportPDFCreator : ChangeNotifier
} }
// https://forum.pdfsharp.net/viewtopic.php?f=2&t=1025 // 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) public async Task<string?> CreatePDF(List<ReportFile> reportFiles, string reportTitle, string outputFilePathWithName, PDFFontResolver fontResolver, Settings appSettings)
{ {
// setup globals and consts... // setup globals and consts...
GlobalFontSettings.FontResolver = fontResolver; GlobalFontSettings.FontResolver = fontResolver;
@@ -102,23 +102,7 @@ class ReportPDFCreator : ChangeNotifier
Width = Unit.FromPoint(2), Width = Unit.FromPoint(2),
};; };;
// start making PDF! // start making PDF!
var outputFileName = reportTitle + ".pdf";
var convertedDir = Utilities.GetTempConvertedImagesFolderPath(); var convertedDir = Utilities.GetTempConvertedImagesFolderPath();
var folderName = new DirectoryInfo(outputFolderPath).Name;
if (folderName.Contains('-'))
{
// see if year/month format
var parts = folderName.Split('-');
if (parts[0].Length == 4 &&
parts[1].Length <= 2 &&
int.TryParse(parts[0], out int year) && int.TryParse(parts[1], out int month))
{
outputFileName = string.Format("{0} {1} Receipts.pdf",
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month),
year);
_logger?.LogInfo("Auto-changed output file name to " + outputFileName);
}
}
// create doc and setup initial section (for page characteristics) // create doc and setup initial section (for page characteristics)
var pdfDoc = new Document(); var pdfDoc = new Document();
var section = pdfDoc.AddSection(); var section = pdfDoc.AddSection();
@@ -160,10 +144,12 @@ class ReportPDFCreator : ChangeNotifier
// First page only: add report title // First page only: add report title
MakeParagraph(section, reportTitle, true, 16, "TitlePar"); MakeParagraph(section, reportTitle, true, 16, "TitlePar");
// //
var outputFilePathNoName = Path.GetDirectoryName(outputFilePathWithName) ?? Utilities.GetInternalDataPath();
var outputFileName = Path.GetFileName(outputFilePathWithName);
var pdfRenderer = new PdfDocumentRenderer var pdfRenderer = new PdfDocumentRenderer
{ {
Document = pdfDoc, Document = pdfDoc,
WorkingDirectory = outputFolderPath WorkingDirectory = outputFilePathNoName
}; };
var hasAddedData = false; var hasAddedData = false;
for (var i = 0; i < reportFiles.Count; i++) for (var i = 0; i < reportFiles.Count; i++)
@@ -394,13 +380,12 @@ class ReportPDFCreator : ChangeNotifier
pdfRenderer.DocumentRenderer.PrepareDocument(); // needed if you make edits after first PrepareDocument() is called pdfRenderer.DocumentRenderer.PrepareDocument(); // needed if you make edits after first PrepareDocument() is called
pdfRenderer.RenderDocument(); pdfRenderer.RenderDocument();
// actually save to disk now // actually save to disk now
string outputPDFFilePath = Path.Join(outputFolderPath, outputFileName);
_logger?.LogInfo("Saving PDF document to disk..."); _logger?.LogInfo("Saving PDF document to disk...");
pdfRenderer.PdfDocument.Save(outputPDFFilePath); pdfRenderer.PdfDocument.Save(outputFilePathWithName);
_logger?.LogInfo("Finished saving PDF output to: " + outputPDFFilePath); _logger?.LogInfo("Finished saving PDF output to: " + outputFilePathWithName);
// clean up converted files data dir // clean up converted files data dir
Directory.Delete(convertedDir, true); Directory.Delete(convertedDir, true);
// return output path // return output path
return outputPDFFilePath; return outputFilePathWithName;
} }
} }
@@ -12,6 +12,7 @@ using MayShow.Models;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Avalonia.Threading; using Avalonia.Threading;
using MayShow.Enums;
namespace MayShow.ViewModels; namespace MayShow.ViewModels;
@@ -454,7 +455,20 @@ class CreatePDFReportViewModel : BaseViewModel, ICanCheckShutdown, ILogger
{ {
try try
{ {
await Task.Run(() => CreatePDF()); var outputFilePath = await DeterminePDFSaveLocation();
if (outputFilePath == null)
{
await DialogHost.Show(new WarningViewModel("Error: Output file path could not be determined. Current save location set in settings: " + Enum.GetName(_settings.PDFOutputSaveLocation)));
}
else if (_settings.PDFOutputSaveLocation == PDFSaveLocation.OtherChosenDir &&
!Directory.Exists(_settings.OutputPdfDir))
{
await DialogHost.Show(new WarningViewModel("Error: Output directory not found! Please adjust the application Settings before continuing. Output directory: " + _settings.OutputPdfDir));
}
else
{
await Task.Run(() => CreatePDF(outputFilePath));
}
} catch (Exception e) } catch (Exception e)
{ {
LogInfo("PDF process failed! Reason: " + e.Message); LogInfo("PDF process failed! Reason: " + e.Message);
@@ -511,18 +525,15 @@ class CreatePDFReportViewModel : BaseViewModel, ICanCheckShutdown, ILogger
} }
} }
private async Task CreatePDF() private async Task<string?> DeterminePDFSaveLocation()
{ {
IsCreatingPDF = true; var fileName = ReportTitle + ".pdf";
var reportCreator = new ReportPDFCreator(this);
var outputDir = "";
switch (_settings.PDFOutputSaveLocation) switch (_settings.PDFOutputSaveLocation)
{ {
case Enums.PDFSaveLocation.BaseFolder: case PDFSaveLocation.BaseFolder:
outputDir = _pdfReport.BaseFolder; return Path.Combine(_pdfReport.BaseFolder, fileName);
break; case PDFSaveLocation.AlwaysAsk:
case Enums.PDFSaveLocation.AlwaysAsk: Func<Task<string?>> getSaveFilePath = async () =>
await Dispatcher.UIThread.InvokeAsync(async () =>
{ {
var topLevel = TopLevelGrabber?.GetTopLevel(); var topLevel = TopLevelGrabber?.GetTopLevel();
if (topLevel != null) if (topLevel != null)
@@ -539,31 +550,35 @@ class CreatePDFReportViewModel : BaseViewModel, ICanCheckShutdown, ILogger
if (result.File is not null) if (result.File is not null)
{ {
var path = result.File.Path.AbsolutePath; var path = result.File.Path.AbsolutePath;
Console.WriteLine(path); // <--- this is what I want if (!path.EndsWith(".pdf"))
}
}
});
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)); // should be fine, but juuuust in case...
path += ".pdf";
} }
else // Console.WriteLine(path);
return path;
}
}
return null;
};
// must invoke on UI thread because getting file picker
return await Dispatcher.UIThread.InvokeAsync(getSaveFilePath);
case PDFSaveLocation.OtherChosenDir:
return Path.Combine(_settings.OutputPdfDir, fileName);
}
return null;
}
private async Task CreatePDF(string outputFilePath)
{ {
var outputPdfFile = await reportCreator.CreatePDF(ReportFiles.ToList(), ReportTitle, outputDir, new PDFFontResolver(_processDir, this), _settings); IsCreatingPDF = true;
var reportCreator = new ReportPDFCreator(this);
var outputPdfFile = await reportCreator.CreatePDF(ReportFiles.ToList(), ReportTitle, outputFilePath, new PDFFontResolver(_processDir, this), _settings);
if (!string.IsNullOrWhiteSpace(outputPdfFile)) if (!string.IsNullOrWhiteSpace(outputPdfFile))
{ {
await CreateAndSaveReportObjectAfterReportCreation(); await CreateAndSaveReportObjectAfterReportCreation();
OpenFolderForFileInFileViewer(outputPdfFile); OpenFolderForFileInFileViewer(outputPdfFile);
} }
}
IsCreatingPDF = false; IsCreatingPDF = false;
} }