Skip to content

Commit

Permalink
review: use computed properties over locals
Browse files Browse the repository at this point in the history
  • Loading branch information
Tayebsed93 committed Nov 13, 2024
1 parent 059134b commit fe04c40
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 125 deletions.
36 changes: 14 additions & 22 deletions Showcase/Showcase/Pages/Tokens/Border/BorderTokenPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct BorderTokenPage: View {
@Environment(\.theme) private var theme
@Environment(\.colorScheme) private var colorScheme

/// A theme to force for this 'View' whatever the environnement theme, including the color scheme is (for UI tests purposes)
/// A theme to force for this `View` whatever the environnement `theme`, including the `colorScheme` is (for UI tests purposes)
private let forcedTheme: OUDSTheme?
private let forcedColorScheme: ColorScheme?

Expand All @@ -30,14 +30,21 @@ struct BorderTokenPage: View {
self.forcedColorScheme = colorScheme
}

/// Computed property for colorScheme
/// Returns `forcedColorScheme` if available, otherwise falls back to the environment `colorScheme`
var activeColorScheme: ColorScheme {
forcedColorScheme ?? colorScheme
}

/// Computed property for theme
/// Returns `forcedTheme` if available, otherwise falls back to the environment `theme`
var activeTheme: OUDSTheme {
forcedTheme ?? theme
}

// MARK: Body

var body: some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme

VStack(alignment: .leading, spacing: activeTheme.spaceFixedMedium) {
Section {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
Expand Down Expand Up @@ -85,16 +92,11 @@ struct BorderTokenPage: View {

private var rectangle: some View {
Rectangle()
.fill(forcedTheme?.colorBgSecondary.color(for: forcedColorScheme ?? colorScheme) ?? theme.colorBgSecondary.color(for: colorScheme))
.fill(activeTheme.colorBgSecondary.color(for: activeColorScheme))
.frame(width: 64, height: 64)
}

public func illustration(for namedWidth: NamedBorderWidth) -> some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme

let token = namedWidth.token(from: activeTheme)
let name = namedWidth.rawValue
let value = String(format: "(%.0f) pt", token)
Expand All @@ -109,11 +111,6 @@ struct BorderTokenPage: View {
}

public func illustration(for namedRadius: NamedBorderRadius) -> some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme

let token = namedRadius.token(from: activeTheme)
let name = namedRadius.rawValue
let value = String(format: "(%.0f) pt", token)
Expand All @@ -128,11 +125,6 @@ struct BorderTokenPage: View {
}

public func illustration(for namedStyle: NamedBorderStyle) -> some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme

let token = namedStyle.token(from: activeTheme)
let name = namedStyle.rawValue
let value = token
Expand Down
55 changes: 31 additions & 24 deletions Showcase/Showcase/Pages/Tokens/Color/ColorTokenPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ColorTokenPage: View {
@Environment(\.theme) private var theme
@Environment(\.colorScheme) private var colorScheme

/// A theme to force for this 'View' whatever the environnement theme, including the color scheme is (for UI tests purposes)
/// A theme to force for this `View` whatever the environnement `theme`, including the `colorScheme` is (for UI tests purposes)
private let forcedTheme: OUDSTheme?
private let forcedColorScheme: ColorScheme?

Expand All @@ -30,6 +30,18 @@ struct ColorTokenPage: View {
self.forcedColorScheme = colorScheme
}

/// Computed property for colorScheme
/// Returns `forcedColorScheme` if available, otherwise falls back to the environment `colorScheme`
var activeColorScheme: ColorScheme {
forcedColorScheme ?? colorScheme
}

/// Computed property for theme
/// Returns `forcedTheme` if available, otherwise falls back to the environment `theme`
var activeTheme: OUDSTheme {
forcedTheme ?? theme
}

// MARK: Body

var body: some View {
Expand Down Expand Up @@ -57,73 +69,73 @@ struct ColorTokenPage: View {
}

private func illustrationForBackground() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Background.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForAction() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Action.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForAlways() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Always.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForChart() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Chart.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForBorder() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Border.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForContent() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Content.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForContentOnBackground() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.ContentOnBg.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForDecorative() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Decorative.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}

private func illustrationForeElevation() -> some View {
VStack(alignment: .leading, spacing: forcedTheme?.spaceFixedNone ?? theme.spaceFixedNone) {
VStack(alignment: .leading, spacing: activeTheme.spaceFixedNone) {
ForEach(NamedColor.Elevation.allCases, id: \.rawValue) { namedColorToken in
illustration(for: namedColorToken.token(from: forcedTheme ?? theme), name: namedColorToken.rawValue)
illustration(for: namedColorToken.token(from: activeTheme), name: namedColorToken.rawValue)
}
}
}
Expand All @@ -132,11 +144,6 @@ struct ColorTokenPage: View {

@ViewBuilder
public func illustration(for token: ColorSemanticToken?, name: String) -> some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme

if let token {
let colorRawToken = activeColorScheme == .dark ? token.dark : token.light

Expand Down
30 changes: 18 additions & 12 deletions Showcase/Showcase/Pages/Tokens/Dimension/Size/SizeTokenPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ struct SizeTokenPage: View {
self.forcedHorizontalSizeClass = horizontalSizeClass
}

/// Computed property for colorScheme
/// Returns `forcedColorScheme` if available, otherwise falls back to the environment `colorScheme`
var activeColorScheme: ColorScheme {
forcedColorScheme ?? colorScheme
}

/// Computed property for theme
/// Returns `forcedTheme` if available, otherwise falls back to the environment `theme`
var activeTheme: OUDSTheme {
forcedTheme ?? theme
}

/// Computed property for horizontalSizeClass
/// Returns `forcedHorizontalSizeClass` if available, otherwise falls back to the environment `horizontalSizeClass`
var activeHorizontalSizeClass: UserInterfaceSizeClass {
forcedHorizontalSizeClass ?? horizontalSizeClass ?? .regular
}

// MARK: Body

var body: some View {
Expand All @@ -58,11 +76,6 @@ struct SizeTokenPage: View {
}

public func illustrationIconDecorative(for namedSize: NamedSize.IconDecorative) -> some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme

let token = namedSize.token(from: activeTheme)
let name = namedSize.rawValue
let value = String(format: "(%.0f) pt", token)
Expand Down Expand Up @@ -93,13 +106,6 @@ struct SizeTokenPage: View {

@ViewBuilder
public func illustrationIconWithLabel(for namedSize: NamedSize.IconWithTypography) -> some View {
/// Move activeColorScheme here to ensure colorScheme is accessible (for UI tests purposes)
let activeColorScheme = forcedColorScheme ?? colorScheme
/// Move activeTheme here to ensure theme is accessible (for UI tests purposes)
let activeTheme = forcedTheme ?? theme
/// Move activeHorizontalSizeClass here to ensure horizontalSizeClass is accessible (for UI tests purposes)
let activeHorizontalSizeClass = forcedHorizontalSizeClass ?? horizontalSizeClass ?? .regular

let token = namedSize.token(fot: activeTheme, userInterfaceSizeClass: activeHorizontalSizeClass)
let namedTypography = namedSize.namedTypography
let value = String(format: "\(namedSize.rawValue) (%.0f) pt", token)
Expand Down
Loading

0 comments on commit fe04c40

Please sign in to comment.