Skip to content

Commit 5f9938e

Browse files
authored
[CP] MAPSIOS-1674: Style imports color-theme (#2481)
1 parent d6dbf7b commit 5f9938e

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

Sources/Examples/SwiftUI Examples/StandardStyleImportExample.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct StandardStyleImportExample: View {
1717
/// This enables the greater encapsulation and allows to reload the basemap without reloading of the fragment.
1818
let importId = "real-estate-fragment"
1919
StyleImport(id: importId, uri: StyleURI(url: styleURL)!)
20+
.colorTheme(ColorTheme(uiimage: monochromeTheme))
2021

2122
/// The contents of the imported style are private, meaning all the implementation details such as layers and sources are not accessible at runtime.
2223
/// However the style defines a "hotels-price" featureset that represents a portion of features available for interaction.
@@ -151,6 +152,7 @@ private extension FeaturesetFeature {
151152
}
152153

153154
private let styleURL = Bundle.main.url(forResource: "fragment-realestate-NY", withExtension: "json")!
155+
private let monochromeTheme = UIImage(named: "monochrome_lut")!
154156

155157
struct StandardStyleImportExample_Previews: PreviewProvider {
156158
static var previews: some View {

Sources/MapboxMaps/ContentBuilders/MapStyleContent/MountedStyleImport.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ extension StyleImport: MapStyleContent, PrimitiveMapContent {
66
node.mount(MountedStyleImport(
77
id: id ?? node.id.stringId,
88
styleData: style.data,
9-
configuration: style.configuration
9+
configuration: style.configuration,
10+
colorTheme: colorTheme
1011
))
1112
}
1213
}
@@ -15,6 +16,7 @@ struct MountedStyleImport: MapContentMountedComponent {
1516
let id: String
1617
let styleData: MapStyle.Data
1718
let configuration: JSONObject?
19+
let colorTheme: ColorTheme?
1820

1921
func mount(with context: MapContentNodeContext) throws {
2022
let importPosition = context.resolveImportPosition()
@@ -38,6 +40,10 @@ struct MountedStyleImport: MapContentMountedComponent {
3840
importPosition: importPosition.corePosition)
3941
}
4042
}
43+
44+
if let colorTheme, let coreColorTheme = colorTheme.core {
45+
try handleExpected { context.style.styleManager.setImportColorThemeForImportId(id, colorTheme: coreColorTheme) }
46+
}
4147
}
4248

4349
func unmount(with context: MapContentNodeContext) throws {
@@ -84,6 +90,12 @@ struct MountedStyleImport: MapContentMountedComponent {
8490
}
8591
}
8692

93+
if colorTheme != old.colorTheme {
94+
try handleExpected {
95+
context.style.styleManager.setImportColorThemeForImportId(id, colorTheme: colorTheme?.core)
96+
}
97+
}
98+
8799
return true
88100
}
89101

Sources/MapboxMaps/Style/StyleImport.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
public struct StyleImport: Sendable {
3333
let id: String?
3434
let style: MapStyle
35+
var colorTheme: ColorTheme?
3536

3637
/// Creates a ``StyleImport`` using a Mapbox Style JSON.
3738
///
@@ -77,3 +78,13 @@ public struct StyleImport: Sendable {
7778
self.style = style
7879
}
7980
}
81+
82+
extension StyleImport {
83+
/// The color theme to apply on ``StyleImport``. Overrides any config values for color-theme and allow to set theme different from the root style in the import.
84+
/// Default value: none
85+
@_documentation(visibility: public)
86+
@_spi(Experimental)
87+
public func colorTheme(_ colorTheme: ColorTheme?) -> Self {
88+
with(self, setter(\.colorTheme, colorTheme))
89+
}
90+
}

Sources/MapboxMaps/Style/StyleManager.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,41 @@ public class StyleManager {
15351535
}
15361536
}
15371537

1538+
/// Set coolor theme for the style import with specified `importId`.
1539+
///
1540+
/// - Parameters:
1541+
/// - importId: Identifier of the style import to remove.
1542+
/// - colorTheme: Color theme to set for style import.
1543+
///
1544+
/// - Throws:
1545+
/// - An error describing why the operation was unsuccessful.
1546+
@_spi(Experimental)
1547+
@_documentation(visibility: public)
1548+
public func setStyleImportColorTheme(importId: String, colorTheme: ColorTheme) throws {
1549+
guard let coreTheme = colorTheme.core else {
1550+
throw StyleError(message: "Cannot construct UIImage object.")
1551+
}
1552+
1553+
try handleExpected {
1554+
return styleManager.setImportColorThemeForImportId(importId, colorTheme: coreTheme)
1555+
}
1556+
}
1557+
1558+
/// Remove color theme from the style import.
1559+
///
1560+
/// - Parameters:
1561+
/// - importId: Identifier of the style import to remove color theme from.
1562+
///
1563+
/// - Throws:
1564+
/// - An error describing why the operation was unsuccessful.
1565+
@_spi(Experimental)
1566+
@_documentation(visibility: public)
1567+
public func removeStyleImportColorTheme(importId: String) throws {
1568+
try handleExpected {
1569+
return styleManager.setImportColorThemeForImportId(importId, colorTheme: nil)
1570+
}
1571+
}
1572+
15381573
}
15391574

15401575
extension StyleManagerProtocol {

Sources/MapboxMaps/Style/StyleManagerProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ internal protocol StyleManagerProtocol {
214214

215215
func setStyleColorThemeFor(_ colorTheme: CoreColorTheme?) -> Expected<NSNull, NSString>
216216
func setInitialStyleColorTheme()
217+
func setImportColorThemeForImportId(_ importId: String, colorTheme: CoreColorTheme?) -> Expected<NSNull, NSString>
217218
}
218219

219220
// MARK: Conformance

Tests/MapboxMapsTests/Foundation/Mocks/MockStyleManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import Foundation
33
@_implementationOnly import MapboxCommon_Private
44

55
class MockStyleManager: StyleManagerProtocol {
6+
struct ImportColorThemeParameters {
7+
var importId: String
8+
var colorTheme: CoreColorTheme?
9+
}
10+
let setImportColorThemeStub = Stub<ImportColorThemeParameters, Expected<NSNull, NSString>>( defaultReturnValue: .init(value: NSNull()))
11+
func setImportColorThemeForImportId(_ importId: String, colorTheme: CoreColorTheme?) -> Expected<NSNull, NSString> {
12+
setImportColorThemeStub(with: ImportColorThemeParameters(importId: importId, colorTheme: colorTheme))
13+
}
14+
615
let setStyleColorThemeForStub = Stub<CoreColorTheme?, Expected<NSNull, NSString>>(
716
defaultReturnValue: .init(value: NSNull())
817
)

0 commit comments

Comments
 (0)