WIP: Html view that prints to pdf ok.

This commit is contained in:
2026-01-17 20:40:49 -05:00
parent 0fe80d05c6
commit 04a7405ca4
16 changed files with 858 additions and 98 deletions

View File

@@ -1,5 +1,6 @@
import Dependencies
import DependenciesMacros
import Elementary
import ManualDClient
import ManualDCore
@@ -26,6 +27,10 @@ public struct ProjectClient: Sendable {
@Sendable (User.ID, Project.Create) async throws -> CreateProjectResponse
public var frictionRate: @Sendable (Project.ID) async throws -> FrictionRateResponse
// FIX: Name to something to do with generating a pdf, just experimenting now.
public var toMarkdown: @Sendable (Project.ID) async throws -> String
public var toHTML: @Sendable (Project.ID) async throws -> (any HTML & Sendable)
}
extension ProjectClient: TestDependencyKey {

View File

@@ -7,36 +7,53 @@ extension DatabaseClient {
func calculateDuctSizes(
projectID: Project.ID
) async throws -> DuctSizes {
) async throws -> (DuctSizes, DuctSizeSharedRequest, [Room]) {
@Dependency(\.manualD) var manualD
return try await manualD.calculateDuctSizes(
rooms: rooms.fetch(projectID),
trunks: trunkSizes.fetch(projectID),
sharedRequest: sharedDuctRequest(projectID)
let shared = try await sharedDuctRequest(projectID)
let rooms = try await rooms.fetch(projectID)
return try await (
manualD.calculateDuctSizes(
rooms: rooms,
trunks: trunkSizes.fetch(projectID),
sharedRequest: shared
),
shared,
rooms
)
}
func calculateRoomDuctSizes(
projectID: Project.ID
) async throws -> [DuctSizes.RoomContainer] {
) async throws -> ([DuctSizes.RoomContainer], DuctSizeSharedRequest) {
@Dependency(\.manualD) var manualD
return try await manualD.calculateRoomSizes(
rooms: rooms.fetch(projectID),
sharedRequest: sharedDuctRequest(projectID)
let shared = try await sharedDuctRequest(projectID)
return try await (
manualD.calculateRoomSizes(
rooms: rooms.fetch(projectID),
sharedRequest: shared
),
shared
)
}
func calculateTrunkDuctSizes(
projectID: Project.ID
) async throws -> [DuctSizes.TrunkContainer] {
) async throws -> ([DuctSizes.TrunkContainer], DuctSizeSharedRequest) {
@Dependency(\.manualD) var manualD
return try await manualD.calculateTrunkSizes(
rooms: rooms.fetch(projectID),
trunks: trunkSizes.fetch(projectID),
sharedRequest: sharedDuctRequest(projectID)
let shared = try await sharedDuctRequest(projectID)
return try await (
manualD.calculateTrunkSizes(
rooms: rooms.fetch(projectID),
trunks: trunkSizes.fetch(projectID),
sharedRequest: shared
),
shared
)
}

View File

@@ -0,0 +1,35 @@
import DatabaseClient
import Dependencies
import ManualDClient
import ManualDCore
extension ManualDClient {
func frictionRate(projectID: Project.ID) async throws -> ProjectClient.FrictionRateResponse {
@Dependency(\.database) var database
let componentLosses = try await database.componentLoss.fetch(projectID)
let lengths = try await database.effectiveLength.fetchMax(projectID)
let equipmentInfo = try await database.equipment.fetch(projectID)
guard let staticPressure = equipmentInfo?.staticPressure else {
return .init(componentLosses: componentLosses, equivalentLengths: lengths)
}
guard let totalEquivalentLength = lengths.total else {
return .init(componentLosses: componentLosses, equivalentLengths: lengths)
}
return try await .init(
componentLosses: componentLosses,
equivalentLengths: lengths,
frictionRate: frictionRate(
.init(
externalStaticPressure: staticPressure,
componentPressureLosses: database.componentLoss.fetch(projectID),
totalEffectiveLength: Int(totalEquivalentLength)
)
)
)
}
}

View File

@@ -3,22 +3,24 @@ import Dependencies
import Logging
import ManualDClient
import ManualDCore
import PdfClient
extension ProjectClient: DependencyKey {
public static var liveValue: Self {
@Dependency(\.database) var database
@Dependency(\.manualD) var manualD
@Dependency(\.pdfClient) var pdfClient
return .init(
calculateDuctSizes: { projectID in
try await database.calculateDuctSizes(projectID: projectID)
try await database.calculateDuctSizes(projectID: projectID).0
},
calculateRoomDuctSizes: { projectID in
try await database.calculateRoomDuctSizes(projectID: projectID)
try await database.calculateRoomDuctSizes(projectID: projectID).0
},
calculateTrunkDuctSizes: { projectID in
try await database.calculateTrunkDuctSizes(projectID: projectID)
try await database.calculateTrunkDuctSizes(projectID: projectID).0
},
createProject: { userID, request in
let project = try await database.projects.create(userID, request)
@@ -31,32 +33,44 @@ extension ProjectClient: DependencyKey {
)
},
frictionRate: { projectID in
let componentLosses = try await database.componentLoss.fetch(projectID)
let lengths = try await database.effectiveLength.fetchMax(projectID)
let equipmentInfo = try await database.equipment.fetch(projectID)
guard let staticPressure = equipmentInfo?.staticPressure else {
return .init(componentLosses: componentLosses, equivalentLengths: lengths)
}
guard let totalEquivalentLength = lengths.total else {
return .init(componentLosses: componentLosses, equivalentLengths: lengths)
}
return try await .init(
componentLosses: componentLosses,
equivalentLengths: lengths,
frictionRate: manualD.frictionRate(
.init(
externalStaticPressure: staticPressure,
componentPressureLosses: database.componentLoss.fetch(projectID),
totalEffectiveLength: Int(totalEquivalentLength)
)
)
)
try await manualD.frictionRate(projectID: projectID)
},
toMarkdown: { projectID in
try await pdfClient.markdown(database.makePdfRequest(projectID))
},
toHTML: { projectID in
try await pdfClient.html(database.makePdfRequest(projectID))
}
)
}
}
extension DatabaseClient {
fileprivate func makePdfRequest(_ projectID: Project.ID) async throws -> PdfClient.Request {
@Dependency(\.manualD) var manualD
guard let project = try await projects.get(projectID) else {
throw ProjectClientError("Project not found. id: \(projectID)")
}
let frictionRateResponse = try await manualD.frictionRate(projectID: projectID)
guard let frictionRate = frictionRateResponse.frictionRate else {
throw ProjectClientError("Friction rate not found. id: \(projectID)")
}
let (ductSizes, sharedInfo, rooms) = try await calculateDuctSizes(projectID: projectID)
return .init(
project: project,
rooms: rooms,
componentLosses: frictionRateResponse.componentLosses,
ductSizes: ductSizes,
equipmentInfo: sharedInfo.equipmentInfo,
maxSupplyTEL: sharedInfo.maxSupplyLength,
maxReturnTEL: sharedInfo.maxReturnLenght,
frictionRate: frictionRate,
projectSHR: sharedInfo.projectSHR
)
}
}