Skip to content

Commit f1d9fa4

Browse files
authored
[iOS] Update LinearGradientInfo to use SwiftUI Color (microsoft#2137)
* Update `LinearGradientInfo` to use SwiftUI `Color` * Remove unused `@objc` marking * Whitespace
1 parent 5ce543a commit f1d9fa4

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

Demos/FluentUIDemo_iOS/FluentUI.Demo/Demos/NotificationViewDemoController.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
import FluentUI
7+
import SwiftUI
78
import UIKit
89

910
class NotificationViewDemoController: DemoController {
@@ -214,10 +215,10 @@ class NotificationViewDemoController: DemoController {
214215
notification.state.message = "The background of this notification has been customized with a gradient."
215216
notification.state.image = UIImage(named: "play-in-circle-24x24")
216217
// It's a lovely blue-to-pink gradient
217-
let colors: [UIColor] = [UIColor(light: GlobalTokens.sharedColor(.pink, .tint50),
218-
dark: GlobalTokens.sharedColor(.pink, .shade40)),
219-
UIColor(light: GlobalTokens.sharedColor(.cyan, .tint50),
220-
dark: GlobalTokens.sharedColor(.cyan, .shade40))]
218+
let colors: [Color] = [Color(light: GlobalTokens.sharedSwiftUIColor(.pink, .tint50),
219+
dark: GlobalTokens.sharedSwiftUIColor(.pink, .shade40)),
220+
Color(light: GlobalTokens.sharedSwiftUIColor(.cyan, .tint50),
221+
dark: GlobalTokens.sharedSwiftUIColor(.cyan, .shade40))]
221222
notification.state.backgroundGradient = LinearGradientInfo(colors: colors,
222223
startPoint: .init(x: 0.0, y: 1.0),
223224
endPoint: .init(x: 1.0, y: 0.0))

Demos/FluentUIDemo_iOS/FluentUI.Demo/Demos/NotificationViewDemoController_SwiftUI.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ struct NotificationDemoView: View {
292292

293293
private var backgroundGradient: LinearGradientInfo {
294294
// It's a lovely blue-to-pink gradient
295-
let colors: [UIColor] = [UIColor(light: GlobalTokens.sharedColor(.pink, .tint50),
296-
dark: GlobalTokens.sharedColor(.pink, .shade40)),
297-
UIColor(light: GlobalTokens.sharedColor(.cyan, .tint50),
298-
dark: GlobalTokens.sharedColor(.cyan, .shade40))]
295+
let colors: [Color] = [Color(light: GlobalTokens.sharedSwiftUIColor(.pink, .tint50),
296+
dark: GlobalTokens.sharedSwiftUIColor(.pink, .shade40)),
297+
Color(light: GlobalTokens.sharedSwiftUIColor(.cyan, .tint50),
298+
dark: GlobalTokens.sharedSwiftUIColor(.cyan, .shade40))]
299299
return LinearGradientInfo(colors: colors,
300300
startPoint: .init(x: 0.0, y: 1.0),
301301
endPoint: .init(x: 1.0, y: 0.0))

Sources/FluentUI_iOS/Core/Extensions/Color+Extensions.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension Color {
1414
/// - Parameter hexValue: The color value to store, in 24-bit (three-channel, 8-bit) RGB.
1515
///
1616
/// - Returns: A color object that stores the provided color information.
17-
init(hexValue: UInt32) {
17+
public init(hexValue: UInt32) {
1818
let red: CGFloat = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
1919
let green: CGFloat = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
2020
let blue: CGFloat = CGFloat(hexValue & 0x000000FF) / 255.0
@@ -28,14 +28,14 @@ extension Color {
2828
/// - Parameter light: The default `Color` for a light context. Required.
2929
/// - Parameter dark: The override `Color` for a dark context. Optional.
3030
/// - Parameter darkElevated: The override `Color` for a dark elevated context. Optional.
31-
init(light: Color,
32-
dark: Color? = nil,
33-
darkElevated: Color? = nil) {
31+
public init(light: Color,
32+
dark: Color? = nil,
33+
darkElevated: Color? = nil) {
3434

3535
let dynamicColor = DynamicColor(light: light, dark: dark, darkElevated: darkElevated)
3636
self.init(dynamicColor: dynamicColor)
3737
}
38-
38+
3939
/// Creates a custom `Color` from a prebuilt `DynamicColor` structure.
4040
///
4141
/// - Parameter dynamicColor: A dynmic color structure that describes the `Color` to be created.

Sources/FluentUI_iOS/Core/Theme/Tokens/LinearGradientInfo.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
// Licensed under the MIT License.
44
//
55

6-
import CoreGraphics
76
import Foundation
87
import SwiftUI
98

109
/// Represents a linear gradient as used by FluentUI.
11-
@objc public class LinearGradientInfo: NSObject {
10+
public class LinearGradientInfo: NSObject {
1211
/// Initializes a linear gradient to be used in Fluent.
1312
///
1413
/// - Parameters:
1514
/// - colors: The array of colors to apply to this linear gradient.
1615
/// - locations: An optional array of values defining the location of each gradient stop. Must be in the range `[0, 1]`.
1716
/// - startPoint: The starting point for this gradient. Values should range from 0.0 to 1.0.
1817
/// - endPoint: The ending point for this gradient. Values should range from 0.0 to 1.0.
19-
public init(colors: [UIColor],
18+
public init(colors: [Color],
2019
locations: [CGFloat]? = nil,
2120
startPoint: CGPoint,
2221
endPoint: CGPoint) {
@@ -27,7 +26,7 @@ import SwiftUI
2726
}
2827

2928
/// The array of colors to apply to this linear gradient.
30-
public let colors: [UIColor]
29+
public let colors: [Color]
3130

3231
/// An optional array of values defining the location of each gradient stop.
3332
///
@@ -49,14 +48,14 @@ extension LinearGradient {
4948
if let locations = gradientInfo.locations {
5049
// Map the colors and locations together.
5150
let stops: [Gradient.Stop] = zip(gradientInfo.colors, locations).map({ (color, location) in
52-
return Gradient.Stop(color: Color(color),
51+
return Gradient.Stop(color: color,
5352
location: location)
5453
})
5554
self.init(stops: stops,
5655
startPoint: UnitPoint(x: gradientInfo.startPoint.x, y: gradientInfo.startPoint.y),
5756
endPoint: UnitPoint(x: gradientInfo.endPoint.x, y: gradientInfo.endPoint.y))
5857
} else {
59-
self.init(colors: gradientInfo.colors.map({ Color($0) }),
58+
self.init(colors: gradientInfo.colors,
6059
startPoint: UnitPoint(x: gradientInfo.startPoint.x, y: gradientInfo.startPoint.y),
6160
endPoint: UnitPoint(x: gradientInfo.endPoint.x, y: gradientInfo.endPoint.y))
6261
}

0 commit comments

Comments
 (0)