diff --git a/Examples/DemosApp/DemosApp/ComponentsPreview/Helpers/PreviewWrapper.swift b/Examples/DemosApp/DemosApp/ComponentsPreview/Helpers/PreviewWrapper.swift index 17cee0b..755174c 100644 --- a/Examples/DemosApp/DemosApp/ComponentsPreview/Helpers/PreviewWrapper.swift +++ b/Examples/DemosApp/DemosApp/ComponentsPreview/Helpers/PreviewWrapper.swift @@ -5,8 +5,6 @@ struct PreviewWrapper: View { let title: String @ViewBuilder let content: () -> Content - @Environment(\.colorScheme) private var colorScheme - var body: some View { ZStack(alignment: Alignment(horizontal: .leading, vertical: .top)) { self.content() @@ -21,8 +19,8 @@ struct PreviewWrapper: View { LinearGradient( gradient: Gradient( colors: [ - UniversalColor.blue.color(for: self.colorScheme), - UniversalColor.purple.color(for: self.colorScheme), + UniversalColor.blue.color, + UniversalColor.purple.color, ] ), startPoint: .topLeading, diff --git a/Examples/DemosApp/DemosApp/Demos/Login/SwiftUILogin.swift b/Examples/DemosApp/DemosApp/Demos/Login/SwiftUILogin.swift index cbbf57f..62d69dc 100644 --- a/Examples/DemosApp/DemosApp/Demos/Login/SwiftUILogin.swift +++ b/Examples/DemosApp/DemosApp/Demos/Login/SwiftUILogin.swift @@ -34,11 +34,9 @@ struct SwiftUILogin: View { ) } - @Environment(\.colorScheme) var colorScheme - var body: some View { ZStack { - UniversalColor.background.color(for: self.colorScheme) + UniversalColor.background.color ScrollView { VStack(spacing: 20) { diff --git a/Sources/ComponentsKit/Components/Button/SUButton.swift b/Sources/ComponentsKit/Components/Button/SUButton.swift index 01b45df..6a60d5e 100644 --- a/Sources/ComponentsKit/Components/Button/SUButton.swift +++ b/Sources/ComponentsKit/Components/Button/SUButton.swift @@ -62,9 +62,9 @@ private struct CustomButtonStyle: SwiftUI.ButtonStyle { .padding(.trailing, self.model.horizontalPadding) .frame(maxWidth: self.model.width) .frame(height: self.model.height) - .foregroundStyle(self.model.foregroundColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.foregroundColor.color) .background( - self.model.backgroundColor?.color(for: self.colorScheme) ?? Color.clear + self.model.backgroundColor?.color ?? Color.clear ) .clipShape( RoundedRectangle( @@ -76,7 +76,7 @@ private struct CustomButtonStyle: SwiftUI.ButtonStyle { cornerRadius: self.model.cornerRadius.value() ) .stroke( - self.model.borderColor?.color(for: self.colorScheme) ?? .clear, + self.model.borderColor?.color ?? .clear, lineWidth: self.model.borderWidth ) } diff --git a/Sources/ComponentsKit/Components/Checkbox/SUCheckbox.swift b/Sources/ComponentsKit/Components/Checkbox/SUCheckbox.swift index ec9315a..0029aca 100644 --- a/Sources/ComponentsKit/Components/Checkbox/SUCheckbox.swift +++ b/Sources/ComponentsKit/Components/Checkbox/SUCheckbox.swift @@ -12,7 +12,6 @@ public struct SUCheckbox: View { @State private var checkmarkStroke: CGFloat @State private var borderOpacity: CGFloat - @Environment(\.colorScheme) private var colorScheme // MARK: Initialization @@ -35,7 +34,7 @@ public struct SUCheckbox: View { public var body: some View { HStack(spacing: self.model.spacing) { ZStack { - self.model.backgroundColor.color(for: self.colorScheme) + self.model.backgroundColor.color .clipShape( RoundedRectangle(cornerRadius: self.model.checkboxCornerRadius) ) @@ -66,12 +65,12 @@ public struct SUCheckbox: View { lineCap: .round, lineJoin: .round )) - .foregroundStyle(self.model.foregroundColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.foregroundColor.color) } .overlay { RoundedRectangle(cornerRadius: self.model.checkboxCornerRadius) .stroke( - self.model.borderColor.color(for: self.colorScheme), + self.model.borderColor.color, lineWidth: self.model.borderWidth ) .opacity(self.borderOpacity) @@ -84,7 +83,7 @@ public struct SUCheckbox: View { if let title = self.model.title { Text(title) - .foregroundStyle(self.model.titleColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.titleColor.color) .font(self.model.titleFont.font) } } diff --git a/Sources/ComponentsKit/Components/Countdown/SUCountdown.swift b/Sources/ComponentsKit/Components/Countdown/SUCountdown.swift index e2dbcc2..b9a6fe6 100644 --- a/Sources/ComponentsKit/Components/Countdown/SUCountdown.swift +++ b/Sources/ComponentsKit/Components/Countdown/SUCountdown.swift @@ -12,8 +12,6 @@ public struct SUCountdown: View { /// The countdown manager handling the countdown logic. @StateObject private var manager = CountdownManager() - @Environment(\.colorScheme) private var colorScheme - // MARK: - Initializer /// Initializer. @@ -86,7 +84,7 @@ public struct SUCountdown: View { private var colonView: some View { Text(":") .font(self.model.preferredMainFont.font) - .foregroundColor(self.model.colonColor.color(for: self.colorScheme)) + .foregroundColor(self.model.colonColor.color) } private func lightStyledTime( @@ -97,7 +95,7 @@ public struct SUCountdown: View { .frame(minHeight: self.model.lightBackgroundMinHight) .frame(minWidth: self.model.lightBackgroundMinWidth) .background(RoundedRectangle(cornerRadius: 8) - .fill(self.model.backgroundColor.color(for: self.colorScheme)) + .fill(self.model.backgroundColor.color) ) } } diff --git a/Sources/ComponentsKit/Components/Divider/SUDivider.swift b/Sources/ComponentsKit/Components/Divider/SUDivider.swift index 391c538..c9b00e2 100644 --- a/Sources/ComponentsKit/Components/Divider/SUDivider.swift +++ b/Sources/ComponentsKit/Components/Divider/SUDivider.swift @@ -7,8 +7,6 @@ public struct SUDivider: View { /// A model that defines the appearance properties. public var model: DividerVM - @Environment(\.colorScheme) private var colorScheme - // MARK: - Initialization /// Initializer. @@ -22,7 +20,7 @@ public struct SUDivider: View { public var body: some View { Rectangle() - .fill(self.model.lineColor.color(for: self.colorScheme)) + .fill(self.model.lineColor.color) .frame( maxWidth: self.model.orientation == .vertical ? self.model.lineSize : nil, maxHeight: self.model.orientation == .horizontal ? self.model.lineSize : nil diff --git a/Sources/ComponentsKit/Components/InputField/SUInputField.swift b/Sources/ComponentsKit/Components/InputField/SUInputField.swift index 583869d..a217a25 100644 --- a/Sources/ComponentsKit/Components/InputField/SUInputField.swift +++ b/Sources/ComponentsKit/Components/InputField/SUInputField.swift @@ -26,8 +26,6 @@ public struct SUInputField: View { /// text inputs and input fields within the same view can be independently focused based on the shared `globalFocus`. public var localFocus: FocusValue - @Environment(\.colorScheme) private var colorScheme - // MARK: Initialization /// Initializer. @@ -61,18 +59,18 @@ public struct SUInputField: View { if self.model.isSecureInput { SecureField(text: self.$text, label: { Text(self.model.placeholder ?? "") - .foregroundStyle(self.model.placeholderColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.placeholderColor.color) }) } else { TextField(text: self.$text, label: { Text(self.model.placeholder ?? "") - .foregroundStyle(self.model.placeholderColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.placeholderColor.color) }) } } - .tint(self.model.tintColor.color(for: self.colorScheme)) + .tint(self.model.tintColor.color) .font(self.model.preferredFont.font) - .foregroundStyle(self.model.foregroundColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.foregroundColor.color) .focused(self.$globalFocus, equals: self.localFocus) .disabled(!self.model.isEnabled) .keyboardType(self.model.keyboardType) @@ -82,7 +80,7 @@ public struct SUInputField: View { } .padding(.horizontal, self.model.horizontalPadding) .frame(height: self.model.height) - .background(self.model.backgroundColor.color(for: self.colorScheme)) + .background(self.model.backgroundColor.color) .onTapGesture { self.globalFocus = self.localFocus } diff --git a/Sources/ComponentsKit/Components/Loading/SULoading.swift b/Sources/ComponentsKit/Components/Loading/SULoading.swift index fbce0cd..553e764 100644 --- a/Sources/ComponentsKit/Components/Loading/SULoading.swift +++ b/Sources/ComponentsKit/Components/Loading/SULoading.swift @@ -9,7 +9,6 @@ public struct SULoading: View { public var model: LoadingVM @State private var rotationAngle: CGFloat = 0.0 - @Environment(\.colorScheme) private var colorScheme // MARK: Initialization @@ -34,7 +33,7 @@ public struct SULoading: View { } .trim(from: 0, to: 0.75) .stroke( - self.model.color.main.color(for: self.colorScheme), + self.model.color.main.color, style: StrokeStyle( lineWidth: self.model.loadingLineWidth, lineCap: .round, diff --git a/Sources/ComponentsKit/Components/RadioGroup/SwiftUI/SURadioGroup.swift b/Sources/ComponentsKit/Components/RadioGroup/SwiftUI/SURadioGroup.swift index 732d4c1..2ed8016 100644 --- a/Sources/ComponentsKit/Components/RadioGroup/SwiftUI/SURadioGroup.swift +++ b/Sources/ComponentsKit/Components/RadioGroup/SwiftUI/SURadioGroup.swift @@ -11,7 +11,6 @@ public struct SURadioGroup: View { @Binding public var selectedId: ID? @State private var viewSizes: [ID: CGSize] = [:] - @Environment(\.colorScheme) private var colorScheme @State private var tappingId: ID? // MARK: Initialization @@ -37,14 +36,14 @@ public struct SURadioGroup: View { ZStack { Circle() .strokeBorder( - self.model.radioItemColor(for: item, isSelected: self.selectedId == item.id).color(for: self.colorScheme), + self.model.radioItemColor(for: item, isSelected: self.selectedId == item.id).color, lineWidth: self.model.lineWidth ) .frame(width: self.model.circleSize, height: self.model.circleSize) if self.selectedId == item.id { Circle() .fill( - self.model.radioItemColor(for: item, isSelected: true).color(for: self.colorScheme) + self.model.radioItemColor(for: item, isSelected: true).color ) .frame(width: self.model.innerCircleSize, height: self.model.innerCircleSize) .transition(.scale) @@ -54,9 +53,7 @@ public struct SURadioGroup: View { .scaleEffect(self.tappingId == item.id ? self.model.animationScale.value : 1.0) Text(item.title) .font(self.model.preferredFont(for: item.id).font) - .foregroundColor( - self.model.textColor(for: item).color(for: self.colorScheme) - ) + .foregroundColor(self.model.textColor(for: item).color) } .background( GeometryReader { proxy in diff --git a/Sources/ComponentsKit/Components/SegmentedControl/SUSegmentedControl.swift b/Sources/ComponentsKit/Components/SegmentedControl/SUSegmentedControl.swift index c62da1d..12e068e 100644 --- a/Sources/ComponentsKit/Components/SegmentedControl/SUSegmentedControl.swift +++ b/Sources/ComponentsKit/Components/SegmentedControl/SUSegmentedControl.swift @@ -11,7 +11,6 @@ public struct SUSegmentedControl: View { @Binding public var selectedId: ID @Namespace private var animationNamespace - @Environment(\.colorScheme) private var colorScheme // MARK: Initialization @@ -35,12 +34,8 @@ public struct SUSegmentedControl: View { Text(itemVM.title) .lineLimit(1) .font(self.model.preferredFont(for: itemVM.id).font) - .foregroundStyle(self.model - .foregroundColor( - id: itemVM.id, - selectedId: self.selectedId - ) - .color(for: self.colorScheme) + .foregroundStyle( + self.model.foregroundColor(id: itemVM.id, selectedId: self.selectedId).color ) .frame(maxWidth: self.model.width, maxHeight: self.model.height) .padding(.horizontal, self.model.horizontalInnerPaddings) @@ -57,9 +52,7 @@ public struct SUSegmentedControl: View { RoundedRectangle( cornerRadius: self.model.selectedSegmentCornerRadius() ) - .fill(self.model.selectedSegmentColor.color( - for: self.colorScheme - )) + .fill(self.model.selectedSegmentColor.color) .matchedGeometryEffect( id: "segment", in: self.animationNamespace @@ -71,7 +64,7 @@ public struct SUSegmentedControl: View { } .padding(.all, self.model.outerPaddings) .frame(height: self.model.height) - .background(self.model.backgroundColor.color(for: self.colorScheme)) + .background(self.model.backgroundColor.color) .clipShape( RoundedRectangle(cornerRadius: self.model.cornerRadius.value()) ) diff --git a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift index 70add76..3616297 100644 --- a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift @@ -26,7 +26,6 @@ public struct SUTextInput: View { /// text inputs and input fields within the same view can be independently focused based on the shared `globalFocus`. public var localFocus: FocusValue - @Environment(\.colorScheme) private var colorScheme @State private var textEditorPreferredHeight: CGFloat = 0 // MARK: - Initialization @@ -68,8 +67,8 @@ public struct SUTextInput: View { ) .lineSpacing(0) .font(self.model.preferredFont.font) - .foregroundStyle(self.model.foregroundColor.color(for: self.colorScheme)) - .tint(self.model.tintColor.color(for: self.colorScheme)) + .foregroundStyle(self.model.foregroundColor.color) + .tint(self.model.tintColor.color) .focused(self.$globalFocus, equals: self.localFocus) .disabled(!self.model.isEnabled) .keyboardType(self.model.keyboardType) @@ -82,14 +81,14 @@ public struct SUTextInput: View { Text(placeholder) .font(self.model.preferredFont.font) .foregroundStyle( - self.model.placeholderColor.color(for: self.colorScheme) + self.model.placeholderColor.color ) .padding(self.model.contentPadding) } } .background( GeometryReader { geometry in - self.model.backgroundColor.color(for: self.colorScheme) + self.model.backgroundColor.color .onAppear { self.textEditorPreferredHeight = TextInputHeightCalculator.preferredHeight( for: self.text, diff --git a/Sources/ComponentsKit/Shared/Colors/UniversalColor.swift b/Sources/ComponentsKit/Shared/Colors/UniversalColor.swift index 49749a5..19aa5f3 100644 --- a/Sources/ComponentsKit/Shared/Colors/UniversalColor.swift +++ b/Sources/ComponentsKit/Shared/Colors/UniversalColor.swift @@ -86,23 +86,6 @@ public struct UniversalColor: Hashable { return UIColor(color) } } - - /// Converts the `ColorRepresentable` to a SwiftUI `Color` instance. - fileprivate var color: Color { - switch self { - case .rgba(let r, let g, let b, let a): - return Color( - red: r / 255, - green: g / 255, - blue: b / 255, - opacity: a - ) - case .uiColor(let uiColor): - return Color(uiColor: uiColor) - case .color(let color): - return color - } - } } // MARK: - Properties @@ -161,7 +144,7 @@ public struct UniversalColor: Hashable { // MARK: - Colors - /// Returns the `UIColor` representation of the color, adapting to the current system theme. + /// Returns the `UIColor` representation of the color. public var uiColor: UIColor { return UIColor { trait in switch trait.userInterfaceStyle { @@ -175,18 +158,8 @@ public struct UniversalColor: Hashable { } } - /// Returns the `Color` representation of the color for a given SwiftUI `ColorScheme`. - /// - /// - Parameter colorScheme: The current color scheme (`.light` or `.dark`). - /// - Returns: The corresponding `Color` instance. - public func color(for colorScheme: ColorScheme) -> Color { - switch colorScheme { - case .light: - return self.light.color - case .dark: - return self.dark.color - @unknown default: - return self.light.color - } + /// Returns the `Color` representation of the color. + public var color: Color { + return Color(self.uiColor) } }