Skip to content

Commit

Permalink
Merge pull request #65 from hhru/MOB-36409__zpDay_colors_generation_s…
Browse files Browse the repository at this point in the history
…upport

Mob 36409  zp day colors generation support
  • Loading branch information
timbaev committed Feb 8, 2024
2 parents 360b4f4 + dfa7a6d commit e3e87bb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct ColorToken: Encodable {

let dayTheme: Theme
let nightTheme: Theme
let zpDayTheme: Theme
let name: String
let path: [String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,50 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {

// MARK: - Instance Methods

private func fallbackWarning(tokenName: String) {
logger.warning("Night value for token '\(tokenName)' not found, using day value.")
private func fallbackWarning(warningPrefix: String, tokenName: String) {
logger.warning("\(warningPrefix) value for token '\(tokenName)' not found, using day value.")
}

private func resolveNightValue(
private func resolveColorToken(
tokenName: String,
fallbackValue: String,
tokenValues: TokenValues
) throws -> String {
guard let nightToken = tokenValues.hhNight.first(where: { $0.name == tokenName }) else {
fallbackWarning(tokenName: tokenName)
return fallbackValue
fallbackColorToken: ColorToken.Theme,
tokenValues: TokenValues,
theme: Theme
) throws -> ColorToken.Theme {
// Resolve theme data
let themeData: (tokenValues: [TokenValue], warningPrefix: String) = switch theme {
case .night:
(tokenValues.hhNight, "Night")

case .zpDay:
(tokenValues.zpDay, "ZpDay")

case .day, .undefined:
([], "")
}

// Resolve token and value
guard let themeToken = themeData.tokenValues.first(where: { $0.name == tokenName }) else {
fallbackWarning(warningPrefix: themeData.warningPrefix, tokenName: tokenName)
return fallbackColorToken
}

guard case .color(let nightValue) = nightToken.type else {
fallbackWarning(tokenName: tokenName)
return fallbackValue
guard case .color(let themeValue) = themeToken.type else {
fallbackWarning(warningPrefix: themeData.warningPrefix, tokenName: tokenName)
return fallbackColorToken
}

return try tokensResolver.resolveHexColorValue(
nightValue,
// Resolve hex color value
let themeHexColorValue = try tokensResolver.resolveHexColorValue(
themeValue,
tokenValues: tokenValues,
theme: .night
theme: theme
)
}

private func resolveNightReference(
tokenName: String,
fallbackRefence: String,
tokenValues: TokenValues
) throws -> String {
guard let nightToken = tokenValues.hhNight.first(where: { $0.name == tokenName }) else {
fallbackWarning(tokenName: tokenName)
return fallbackRefence
}

guard case .color(let nightValue) = nightToken.type else {
fallbackWarning(tokenName: tokenName)
return fallbackRefence
}
// Resolve reference
let themeReference = try tokensResolver.resolveBaseReference(themeValue, tokenValues: themeData.tokenValues)

return try tokensResolver.resolveBaseReference(nightValue, tokenValues: tokenValues.hhNight)
return ColorToken.Theme(value: themeHexColorValue, reference: themeReference)
}

private func makeColorToken(
Expand All @@ -64,32 +66,35 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {
tokenValues: TokenValues,
path: [String]
) throws -> ColorToken {
let dayHexColorValue = try tokensResolver.resolveHexColorValue(
dayValue,
tokenValues: tokenValues,
theme: .day
)

let dayReference = try tokensResolver.resolveBaseReference(
dayValue,
tokenValues: tokenValues.hhDay
let dayColorToken = ColorToken.Theme(
value: try tokensResolver.resolveHexColorValue(
dayValue,
tokenValues: tokenValues,
theme: .day
),
reference: try tokensResolver.resolveBaseReference(
dayValue,
tokenValues: tokenValues.hhDay
)
)

let nightReference = try resolveNightReference(
let nightColorToken = try resolveColorToken(
tokenName: tokenName,
fallbackRefence: dayValue,
tokenValues: tokenValues
fallbackColorToken: dayColorToken,
tokenValues: tokenValues,
theme: .night
)

let nightHexColorValue = try resolveNightValue(
let zpDayColorToken = try resolveColorToken(
tokenName: tokenName,
fallbackValue: dayHexColorValue,
tokenValues: tokenValues
fallbackColorToken: dayColorToken,
tokenValues: tokenValues,
theme: .zpDay
)

return ColorToken(
dayTheme: ColorToken.Theme(value: dayHexColorValue, reference: dayReference),
nightTheme: ColorToken.Theme(value: nightHexColorValue, reference: nightReference),
dayTheme: dayColorToken,
nightTheme: nightColorToken,
zpDayTheme: zpDayColorToken,
name: tokenName,
path: path
)
Expand Down
1 change: 1 addition & 0 deletions Sources/FigmaGen/Models/Token/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import Foundation
enum Theme: Codable {
case day
case night
case zpDay
case undefined
}
5 changes: 4 additions & 1 deletion Sources/FigmaGen/Models/Token/TokenValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ struct TokenValues: Hashable {
case .night:
return [hhNight, core, semantic, colors, typography].flatMap { $0 }

case .zpDay:
return [zpDay, core, semantic, colors, typography].flatMap { $0 }

case .undefined:
return [core, semantic, colors, typography, hhDay, hhNight].flatMap { $0 }
return [core, semantic, colors, typography, hhDay, hhNight, zpDay].flatMap { $0 }
}
}
}
Expand Down

0 comments on commit e3e87bb

Please sign in to comment.