diff --git a/Sources/FlaggedViews/FlaggedMessageLabel.swift b/Sources/FlaggedViews/FlaggedMessageLabel.swift new file mode 100644 index 0000000..8dc8498 --- /dev/null +++ b/Sources/FlaggedViews/FlaggedMessageLabel.swift @@ -0,0 +1,28 @@ +import SharedModels +import Styleguide +import SwiftUI + +/// Represents the message of a flagged value result, which is stylable using the +/// `.flaggedMessageLabelStyle` modifier on a view. +/// +/// By default the status label is colored using the default status color. +public struct FlaggedMessageLabel: View { + @Environment(\.flaggedMessageLabelStyle) private var style + let message: String? + + public init(message: String?) { + self.message = message + } + + public var body: some View { + if let message { + TextLabel(message) + .textLabelStyle(style) + } + } +} + +#Preview { + FlaggedStatusLabel(status: .warning) + .flaggedStatusLabelStyle(.heavyTitle2) +} diff --git a/Sources/FlaggedViews/FlaggedMessageView.swift b/Sources/FlaggedViews/FlaggedMessageView.swift index 8fd4e15..5922fd7 100644 --- a/Sources/FlaggedViews/FlaggedMessageView.swift +++ b/Sources/FlaggedViews/FlaggedMessageView.swift @@ -26,7 +26,3 @@ extension FlaggedMessageView { self.init(message: flagged.message, status: flagged.status) } } - -//#Preview { -// SwiftUIView() -//} diff --git a/Sources/FlaggedViews/FlaggedStatusLabel.swift b/Sources/FlaggedViews/FlaggedStatusLabel.swift new file mode 100644 index 0000000..01c527c --- /dev/null +++ b/Sources/FlaggedViews/FlaggedStatusLabel.swift @@ -0,0 +1,29 @@ +import SharedModels +import Styleguide +import SwiftUI + +/// Represents the status of a flagged value, which is stylable using the +/// `.flaggedStatusLabelStyle` modifier on a view. +/// +/// By default the status label is colored using the default status color. +public struct FlaggedStatusLabel: View { + @Environment(\.flaggedStatusLabelStyle) private var style + let status: Flagged.CheckResult.Status + + public init(status: Flagged.CheckResult.Status) { + self.status = status + } + + public var body: some View { + TextLabel(status.title) + .textLabelStyle( + .colored(status.flagColor) + .combining(style) + ) + } +} + +#Preview { + FlaggedStatusLabel(status: .warning) + .flaggedStatusLabelStyle(.heavyTitle2) +} diff --git a/Sources/FlaggedViews/Styles/FlaggedMessageLabelStyle.swift b/Sources/FlaggedViews/Styles/FlaggedMessageLabelStyle.swift new file mode 100644 index 0000000..557c7c8 --- /dev/null +++ b/Sources/FlaggedViews/Styles/FlaggedMessageLabelStyle.swift @@ -0,0 +1,24 @@ +import Styleguide +import SwiftUI + +private struct FlaggedMessageLabelStyleKey: EnvironmentKey { + static var defaultValue = AnyTextLabelStyle(style: .font(.caption)) +} + +extension EnvironmentValues { + var flaggedMessageLabelStyle: AnyTextLabelStyle { + get { self[FlaggedMessageLabelStyleKey.self] } + set { self[FlaggedMessageLabelStyleKey.self] = newValue } + } +} + +extension View { + public func flaggedMessageLabelStyle(_ style: AnyTextLabelStyle) -> some View { + environment(\.flaggedMessageLabelStyle, style) + } + + public func flaggedMessageLabelStyle(_ style: S) -> some View { + flaggedMessageLabelStyle(AnyTextLabelStyle(style: style)) + } +} + diff --git a/Sources/FlaggedViews/Styles/FlaggedMessageViewStyle.swift b/Sources/FlaggedViews/Styles/FlaggedMessageViewStyle.swift index 8cf6048..2a74fcf 100644 --- a/Sources/FlaggedViews/Styles/FlaggedMessageViewStyle.swift +++ b/Sources/FlaggedViews/Styles/FlaggedMessageViewStyle.swift @@ -40,16 +40,15 @@ public struct AnyFlaggedMessageViewStyle: FlaggedMessageViewStyle { public struct DefaultFlaggedMessageViewStyle: FlaggedMessageViewStyle { + + @ViewBuilder public func makeBody(configuration: Configuration) -> some View { - if let message = configuration.message { + if configuration.message != nil { HStack { - Text(configuration.status.title) - .bold() - .foregroundStyle(configuration.status.flagColor) - TextLabel(message) + FlaggedStatusLabel(status: configuration.status) + FlaggedMessageLabel(message: configuration.message) } - .font(.caption) } } } @@ -58,14 +57,11 @@ public struct VerticalFlaggedMessageViewStyle: FlaggedMessageViewStyle { @ViewBuilder public func makeBody(configuration: Configuration) -> some View { - if let message = configuration.message { + if configuration.message != nil { VStack(alignment: .leading) { - Text(configuration.status.title) - .bold() - .foregroundStyle(configuration.status.flagColor) - TextLabel(message) + FlaggedStatusLabel(status: configuration.status) + FlaggedMessageLabel(message: configuration.message) } - .font(.caption) } } } diff --git a/Sources/FlaggedViews/Styles/FlaggedStatusLabelStyle.swift b/Sources/FlaggedViews/Styles/FlaggedStatusLabelStyle.swift new file mode 100644 index 0000000..93b221c --- /dev/null +++ b/Sources/FlaggedViews/Styles/FlaggedStatusLabelStyle.swift @@ -0,0 +1,24 @@ +import SharedModels +import Styleguide +import SwiftUI + +private struct FlaggedStatusLabelStyleKey: EnvironmentKey { + static var defaultValue = AnyTextLabelStyle(style: .font(.caption, fontWeight: .bold)) +} + +extension EnvironmentValues { + public var flaggedStatusLabelStyle: AnyTextLabelStyle { + get { self[FlaggedStatusLabelStyleKey.self] } + set { self[FlaggedStatusLabelStyleKey.self] = newValue } + } +} + +extension View { + public func flaggedStatusLabelStyle(_ style: AnyTextLabelStyle) -> some View { + environment(\.flaggedStatusLabelStyle, style) + } + + public func flaggedStatusLabelStyle(_ style: S) -> some View { + flaggedStatusLabelStyle(AnyTextLabelStyle(style: style)) + } +} diff --git a/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift b/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift index 37d1cd2..eb7422b 100644 --- a/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift +++ b/Sources/PressureEstimationsFeature/FlaggedMeasurementsList.swift @@ -272,6 +272,8 @@ public struct FlaggedMeasurementListView: View { ? AnyFlaggedMessageViewStyle(.vertical) : AnyFlaggedMessageViewStyle(.horizontal) ) +// .flaggedStatusLabelStyle(.font(.caption)) +// .flaggedMessageLabelStyle(.font(.caption, fontWeight: .bold)) } } diff --git a/Sources/Styleguide/Styles/TextLabelStyle.swift b/Sources/Styleguide/Styles/TextLabelStyle.swift index 0a50c2a..1e0a1c9 100644 --- a/Sources/Styleguide/Styles/TextLabelStyle.swift +++ b/Sources/Styleguide/Styles/TextLabelStyle.swift @@ -166,7 +166,7 @@ extension View { } public func textLabelStyle(_ style: S) -> some View { - environment(\.textLabelStyle, AnyTextLabelStyle(style: style)) + textLabelStyle(AnyTextLabelStyle(style: style)) } public func sectionHeaderLabelStyle(_ style: S) -> some View {