diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/CalculateAtFeature.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/CalculateAtFeature.xcscheme new file mode 100644 index 0000000..2680bb4 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/CalculateAtFeature.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/EstimatedPressureDependency.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/EstimatedPressureDependency.xcscheme new file mode 100644 index 0000000..c519916 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/EstimatedPressureDependency.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/FlaggedViews.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/FlaggedViews.xcscheme new file mode 100644 index 0000000..1ef74b3 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/FlaggedViews.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/InfoViewFeature.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/InfoViewFeature.xcscheme new file mode 100644 index 0000000..48d44f5 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/InfoViewFeature.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/PressureEstimationsFeature.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/PressureEstimationsFeature.xcscheme new file mode 100644 index 0000000..a339421 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/PressureEstimationsFeature.xcscheme @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/PressureEstimationsFeatureTests.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/PressureEstimationsFeatureTests.xcscheme new file mode 100644 index 0000000..d69d0c8 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/PressureEstimationsFeatureTests.xcscheme @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/SharedModels.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/SharedModels.xcscheme new file mode 100644 index 0000000..7cac3b7 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/SharedModels.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/Styleguide.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/Styleguide.xcscheme new file mode 100644 index 0000000..9d0d579 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/Styleguide.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/swift-estimated-pressures-core-Package.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/swift-estimated-pressures-core-Package.xcscheme new file mode 100644 index 0000000..85d1ef2 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/swift-estimated-pressures-core-Package.xcscheme @@ -0,0 +1,173 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Package.swift b/Package.swift index c897afb..2b19032 100644 --- a/Package.swift +++ b/Package.swift @@ -112,5 +112,9 @@ let package = Package( ], swiftSettings: settings ), + .testTarget( + name: "PressureEstimationsFeatureTests", + dependencies: ["PressureEstimationsFeature"] + ), ] ) diff --git a/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift b/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift index fa699b2..083b610 100644 --- a/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift +++ b/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift @@ -173,39 +173,102 @@ public struct FlaggedMeasurementsList: Sendable { logger: logger ) } - + + guard let estimationState = ensureHasChanges( + formState: form, + flaggedEstimations: state.sharedSettings.flaggedEstimations + ) else { + logger.debug("No changes found, not generating a new flagged estimation measurement.") + print("No changes found, not generating a new flagged estimation measurement.") + return .none + } +// +// let estimationState = form.estimationState +// // Check if there's an existing estimation with the same id, and +// // check for changes. If no changes, then short circuit and return nothing. +// if let id = form.id, +// let existingState = state.sharedSettings.flaggedEstimations[id: id]?.estimationState, +// existingState.hasChanges(estimationState) +// { +// return .none +// } + return .receive(action: \.receive) { [sharedSettings = state.sharedSettings] in - var filterPressureDrop: Positive? = nil - - if sharedSettings.flaggedEquipmentMeasurement?.filterPressureDrop.wrappedValue != 0 { - filterPressureDrop = form.filterPressureDrop != nil - ? Positive(wrappedValue: form.filterPressureDrop!) - : nil - } - - let measurement = try await estimatedPressuresClient.estimatedPressure( - equipmentMeasurement: equipmentMeasurement, - airflow: form.airflow, - filterPressureDrop: filterPressureDrop - ) - - let flaggedMeasurement = EquipmentMeasurement.FlaggedMeasurement( + let flaggedMeasurement = try await flaggedMeasurement( + airflow: estimationState.cfm.airflow, budgets: budgets, - measurement: measurement, - ratedPressures: sharedSettings.ratedStaticPressures, + equipmentMeasurement: equipmentMeasurement, + filterPressureDrop: parseFilterPressureDrop(formState: form, sharedSettings: sharedSettings), + ratedStaticPresures: sharedSettings.ratedStaticPressures, tons: form.coolingCapacity ) - + return .estimatedFlaggedMeasurement(.init( id: form.id ?? uuid(), - estimationState: .init(state: form), + estimationState: estimationState, flaggedMeasurement: flaggedMeasurement )) } - } - + + func ensureHasChanges( + formState: EstimationForm.State, + flaggedEstimations: IdentifiedArrayOf + ) -> SharedPressureEstimationState.FlaggedEstimationContainer.EstimationState? { + let estimationState = formState.estimationState + + guard let id = formState.id, + let existingState = flaggedEstimations[id: id]?.estimationState + else { return estimationState } + + if existingState.hasChanges(estimationState) { + return estimationState + } + return nil + } + + private func parseFilterPressureDrop( + formState: EstimationForm.State, + sharedSettings: SharedPressureEstimationState + ) -> Positive? { + guard sharedSettings.flaggedEquipmentMeasurement?.filterPressureDrop.wrappedValue != 0, + let filterPressureDrop = formState.filterPressureDrop + else { + return nil + } + return Positive(filterPressureDrop) + } + + private func flaggedMeasurement( + airflow: Double, + budgets: BudgetedPercentEnvelope, + equipmentMeasurement: EquipmentMeasurement, + filterPressureDrop: Positive?, + ratedStaticPresures: RatedStaticPressures, + tons: EquipmentMetadata.CoolingCapacity + ) async throws -> EquipmentMeasurement.FlaggedMeasurement { + + let measurement = try await estimatedPressuresClient.estimatedPressure( + equipmentMeasurement: equipmentMeasurement, + airflow: airflow, + filterPressureDrop: filterPressureDrop + ) + + return .init( + budgets: budgets, + measurement: measurement, + ratedPressures: ratedStaticPresures, + tons: tons + ) + } +} + +fileprivate extension EstimationForm.State { + + var estimationState: SharedPressureEstimationState.FlaggedEstimationContainer.EstimationState { + .init(state: self) + } } @ViewAction(for: FlaggedMeasurementsList.self) diff --git a/Sources/PressureEstimationsFeature/SharedPressureEstimationState.swift b/Sources/PressureEstimationsFeature/SharedPressureEstimationState.swift index 9b406d7..3c5c02b 100644 --- a/Sources/PressureEstimationsFeature/SharedPressureEstimationState.swift +++ b/Sources/PressureEstimationsFeature/SharedPressureEstimationState.swift @@ -36,7 +36,6 @@ public struct SharedPressureEstimationState: Equatable, Sendable { set { equipmentMetadata[keyPath: keyPath] = newValue } } - #warning("Needs to hold onto estimation state, so it can be editable") @dynamicMemberLookup public struct FlaggedEstimationContainer: Equatable, Identifiable, Sendable { public let id: UUID @@ -92,6 +91,11 @@ public struct SharedPressureEstimationState: Equatable, Sendable { return name } + // Compare relevant values on if two states have changes. + func hasChanges(_ other: Self) -> Bool { + cfm != other.cfm || filterPressureDrop != other.filterPressureDrop + } + public enum CFMContainer: Equatable, Sendable { case cfm(Int) case cfmPerTon(Int, EquipmentMetadata.CoolingCapacity) diff --git a/Tests/PressureEstimationsFeatureTests/FlaggedMeasurementListTests.swift b/Tests/PressureEstimationsFeatureTests/FlaggedMeasurementListTests.swift new file mode 100644 index 0000000..0da2cf2 --- /dev/null +++ b/Tests/PressureEstimationsFeatureTests/FlaggedMeasurementListTests.swift @@ -0,0 +1,28 @@ +import ComposableArchitecture +import Testing +@testable import PressureEstimationsFeature + +struct FlaggedMeasurementListTests { + + @Test( + "Ensure Estimation Has Changes", + .tags(.flaggedMeasurementList) + ) + func ensureHasChanges() { + let reducer = FlaggedMeasurementsList() + let result = reducer.ensureHasChanges( + formState: .init( + existingMeasurement: SharedReader(Shared(.mock(type: .airHandler))), + cfmTextField: 350, + filterPressureDrop: 0.1 + ), + flaggedEstimations: [] + ) + #expect(result != nil) + } + +} + +extension Tag { + @Tag static var flaggedMeasurementList: Self +}