Skip to content

Commit

Permalink
fix: πŸ› Custom colors were not updated when appearance changed
Browse files Browse the repository at this point in the history
βœ… Closes: #481

(cherry picked from commit 6b3499d)
  • Loading branch information
MarcoEidinger committed Nov 17, 2022
1 parent cda2480 commit 3f3588b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
26 changes: 21 additions & 5 deletions Apps/Examples/Examples/FioriThemeManager/Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ struct CustomColors: View {
}
ForEach(colorStyles,
id: \.self) { colorStyle in
ColorView(colorStyle: colorStyle)
ColorViewWithNoHexDescription(colorStyle: colorStyle)
}
}
.onAppear(perform: {
switch testData {
case .customPalette(let provider):
StyleSheetSettings.reset()
ThemeManager.shared.setPalette(Palette(provider))
case .programmatic(let color):
case .programmatic(let lightColor, let darkColor):
StyleSheetSettings.reset()
ThemeManager.shared.setPalette(PaletteVersion.latest.rawValue)
ThemeManager.shared.setColor(color, for: .primaryLabel, variant: .light)
ThemeManager.shared.setColor(color, for: .primaryLabel, variant: .dark)
ThemeManager.shared.setColor(lightColor, for: .primaryLabel, variant: .light)
ThemeManager.shared.setColor(darkColor, for: .primaryLabel, variant: .dark)
case .styleSheet(let content):
StyleSheetSettings.reset()
ThemeManager.shared.setPalette(PaletteVersion.latest.rawValue)
Expand Down Expand Up @@ -86,9 +86,25 @@ struct ColorView: View {
}
}

struct ColorViewWithNoHexDescription: View {
var colorStyle: ColorStyle

var body: some View {
HStack {
Circle()
.fill(Color.preferredColor(colorStyle))
.frame(width: 50, height: 50)
.padding()
VStack {
Text(colorStyle.rawValue)
}
}
}
}

enum ColorTestData {
case customPalette(PaletteProvider)
case programmatic(Color)
case programmatic(Color, Color)
case styleSheet(String)

static var sampleStyleSheet: String { """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct FioriThemeManagerContentView: View {
Text("Colors - custom palette (random)")
}
NavigationLink(
destination: CustomColors(testData: .programmatic(.green))) {
destination: CustomColors(testData: .programmatic(.green, .red))) {
Text("Colors - developer override")
}
NavigationLink(
Expand Down
51 changes: 24 additions & 27 deletions Sources/FioriThemeManager/ThemeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,45 +156,42 @@ public class ThemeManager {
internal func color(for style: ColorStyle, background scheme: BackgroundColorScheme?, interface level: InterfaceLevel?, display mode: ColorDisplayMode?) -> Color {
let uiColor = self.uiColor(for: style, background: scheme, interface: level, display: mode)
let color = Color(uiColor)

guard let hexColor = self.hexColor(for: style) else { return color }

let variant = hexColor.getVariant(traits: UITraitCollection.current, background: scheme, interface: level, display: mode)

if let ssOverrideColor = self.styleSheetOverrides[style, default: [:]][variant] {
return ssOverrideColor
}

if let ssHexColor = nssHexColor(for: style, with: variant) {
if let ssColor: Color = StyleSheetConverter.toColor(value: ssHexColor) {
self.styleSheetOverrides[style, default: [:]].updateValue(ssColor, forKey: variant)
return ssColor
}
}

if let devOverrideColor = developerOverrides[style, default: [:]][variant] {
return devOverrideColor
}

return color
}

func uiColor(for style: ColorStyle, background scheme: BackgroundColorScheme?, interface level: InterfaceLevel?, display mode: ColorDisplayMode?) -> UIColor {
guard let hc = self.hexColor(for: style) else { return UIColor() }
let uc = UIColor { traitCollection in
let variant: ColorVariant = hc.getVariant(traits: traitCollection, background: scheme, interface: level, display: mode)
guard let hc = self.hexColor(for: style) else { return .clear }
let uc = UIColor { [weak self] traitCollection in
guard let self = self else { return .clear }
guard let hexColor = self.hexColor(for: style) else { return .clear }
let variant = hexColor.getVariant(traits: traitCollection, background: scheme, interface: level, display: mode)

let hexColorString: String = hc.hex(variant)
let components = hc.rgba(hexColorString)
return UIColor(red: CGFloat(components.r), green: CGFloat(components.g),
blue: CGFloat(components.b), alpha: CGFloat(components.a))

if let ssOverrideColor = self.styleSheetOverrides[style, default: [:]][variant] {
return ssOverrideColor.uiColor()
}

if let ssHexColor = self.nssHexColor(for: style, with: variant) {
if let ssColor: Color = StyleSheetConverter.toColor(value: ssHexColor) {
self.styleSheetOverrides[style, default: [:]].updateValue(ssColor, forKey: variant)
return ssColor.uiColor()
}
}

if let devOverrideColor = self.developerOverrides[style, default: [:]][variant] {
return devOverrideColor.uiColor()
}

return Color(hex: hexColorString)?.uiColor() ?? .clear
}
return uc
}

func nssHexColor(for style: ColorStyle, with variant: ColorVariant) -> String? {
var developerOverriddenColors = [String: String]()

// // Get the developer defined color definitions from the .nss file
// Get the developer defined color definitions from the .nss file
developerOverriddenColors = StyleSheetSettings.shared.globalDefinitions // NUISettings.getInstance()._globalDefinitions

// In .nss file color is defined using background scheme, so check for inversed color variant
Expand Down

0 comments on commit 3f3588b

Please sign in to comment.