Refactor report creation and property initialization
Replaced nullable fields with non-nullable properties for `Report` and `BasePdfBytes` to improve initialization and encapsulation. Updated `OnInitializedAsync` to use `BasePdfBytes` and added error handling for missing PDF bytes. Refactored report creation logic by removing `BuildFreshBaseReport` and `CreateReportInstance`, consolidating functionality into a new `CreateReport` method. Enhanced `CreateReport` to support both predefined and dynamically generated reports, simplifying the code structure and improving maintainability.
This commit is contained in:
@@ -19,34 +19,37 @@
|
|||||||
@code {
|
@code {
|
||||||
[Parameter] public string EnvelopeKey { get; set; } = string.Empty;
|
[Parameter] public string EnvelopeKey { get; set; } = string.Empty;
|
||||||
|
|
||||||
XtraReport? Report;
|
XtraReport Report { get; set; } = null!;
|
||||||
byte[]? _basePdfBytes;
|
|
||||||
|
byte[] BasePdfBytes { get; set; } = null!;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync() {
|
protected override async Task OnInitializedAsync() {
|
||||||
if (!AppOptions.Value.UsePredefinedReports) {
|
if (!AppOptions.Value.UsePredefinedReports) {
|
||||||
var (pdfBytes, _) = await DocumentService.GetDocumentAsync(EnvelopeKey);
|
var (pdfBytes, _) = await DocumentService.GetDocumentAsync(EnvelopeKey);
|
||||||
if (pdfBytes is { Length: > 0 })
|
if (pdfBytes is { Length: > 0 })
|
||||||
_basePdfBytes = pdfBytes;
|
BasePdfBytes = pdfBytes;
|
||||||
|
else
|
||||||
|
throw new InvalidOperationException($"No PDF bytes found for EnvelopeKey: {EnvelopeKey}");
|
||||||
}
|
}
|
||||||
|
|
||||||
Report = BuildFreshBaseReport();
|
Report = CreateReport();
|
||||||
}
|
}
|
||||||
|
|
||||||
XtraReport BuildFreshBaseReport() {
|
XtraReport CreateReport()
|
||||||
if (_basePdfBytes is { Length: > 0 }) {
|
{
|
||||||
|
if (AppOptions.Value.UsePredefinedReports)
|
||||||
|
{
|
||||||
|
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
|
||||||
|
? savedReport
|
||||||
|
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
var report = new XtraReport();
|
var report = new XtraReport();
|
||||||
var detail = new DevExpress.XtraReports.UI.DetailBand();
|
var detail = new DevExpress.XtraReports.UI.DetailBand();
|
||||||
report.Bands.Add(detail);
|
report.Bands.Add(detail);
|
||||||
detail.Controls.Add(new DevExpress.XtraReports.UI.XRPdfContent { Source = _basePdfBytes, GenerateOwnPages = true });
|
detail.Controls.Add(new DevExpress.XtraReports.UI.XRPdfContent { Source = BasePdfBytes, GenerateOwnPages = true });
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
return CreateReportInstance();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
XtraReport CreateReportInstance() {
|
|
||||||
return ReportStorage.TryGetReport("LargeDatasetReport", out var savedReport)
|
|
||||||
? savedReport
|
|
||||||
: PredefinedReports.ReportsFactory.GetReport("LargeDatasetReport");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user