Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove colorScheme param from UniversalColor.color #38

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ struct PreviewWrapper<Content: View>: 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()
Expand All @@ -21,8 +19,8 @@ struct PreviewWrapper<Content: View>: 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,
Expand Down
4 changes: 1 addition & 3 deletions Examples/DemosApp/DemosApp/Demos/Login/SwiftUILogin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/ComponentsKit/Components/Button/SUButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
}
Expand Down
9 changes: 4 additions & 5 deletions Sources/ComponentsKit/Components/Checkbox/SUCheckbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/ComponentsKit/Components/Countdown/SUCountdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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)
)
}
}
4 changes: 1 addition & 3 deletions Sources/ComponentsKit/Components/Divider/SUDivider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions Sources/ComponentsKit/Components/InputField/SUInputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public struct SUInputField<FocusValue: Hashable>: 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.
Expand Down Expand Up @@ -61,18 +59,18 @@ public struct SUInputField<FocusValue: Hashable>: 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)
Expand All @@ -82,7 +80,7 @@ public struct SUInputField<FocusValue: Hashable>: 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
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/ComponentsKit/Components/Loading/SULoading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public struct SURadioGroup<ID: Hashable>: View {
@Binding public var selectedId: ID?

@State private var viewSizes: [ID: CGSize] = [:]
@Environment(\.colorScheme) private var colorScheme
@State private var tappingId: ID?

// MARK: Initialization
Expand All @@ -37,14 +36,14 @@ public struct SURadioGroup<ID: Hashable>: 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)
Expand All @@ -54,9 +53,7 @@ public struct SURadioGroup<ID: Hashable>: 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public struct SUSegmentedControl<ID: Hashable>: View {
@Binding public var selectedId: ID

@Namespace private var animationNamespace
@Environment(\.colorScheme) private var colorScheme

// MARK: Initialization

Expand All @@ -35,12 +34,8 @@ public struct SUSegmentedControl<ID: Hashable>: 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)
Expand All @@ -57,9 +52,7 @@ public struct SUSegmentedControl<ID: Hashable>: 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
Expand All @@ -71,7 +64,7 @@ public struct SUSegmentedControl<ID: Hashable>: 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())
)
Expand Down
9 changes: 4 additions & 5 deletions Sources/ComponentsKit/Components/TextInput/SUTextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public struct SUTextInput<FocusValue: Hashable>: 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
Expand Down Expand Up @@ -68,8 +67,8 @@ public struct SUTextInput<FocusValue: Hashable>: 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)
Expand All @@ -82,14 +81,14 @@ public struct SUTextInput<FocusValue: Hashable>: 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,
Expand Down
35 changes: 4 additions & 31 deletions Sources/ComponentsKit/Shared/Colors/UniversalColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Loading