-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose CustomGeometrySource and CustomRasterSource (#1992)
* Initial implementation * Clean up implementation, add tests * Update tests * Update tests and docs * Address review comments * Update options to an optional * Remove whitespace * Add trailing newline
- Loading branch information
1 parent
e25eaa4
commit 711ac7f
Showing
11 changed files
with
285 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Sources/MapboxMaps/Style/CustomSources/CustomGeometrySource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Foundation | ||
|
||
/// Describes a Custom Geometry Source to be used in the style | ||
/// | ||
/// A CustomGeometrySource uses a coalescing model for frequent data updates targeting the same tile id. | ||
/// This means that the in-progress request as well as the last scheduled request are guaranteed to finish. | ||
public struct CustomGeometrySource: Source { | ||
|
||
/// The Source type | ||
public let type: SourceType | ||
|
||
/// Style source identifier. | ||
public let id: String | ||
|
||
/// Settings for the custom geometry, including a fetchTileFunction callback | ||
public let options: CustomGeometrySourceOptions? | ||
|
||
public init(id: String, options: CustomGeometrySourceOptions) { | ||
self.type = .customGeometry | ||
self.id = id | ||
self.options = options | ||
} | ||
} | ||
|
||
extension CustomGeometrySource { | ||
enum CodingKeys: String, CodingKey { | ||
case id | ||
case type | ||
} | ||
|
||
/// Init from a decoder, note that the CustomGeometrySourceOptions are not decodable and need to be set separately | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
id = try container.decode(String.self, forKey: .id) | ||
type = try container.decode(SourceType.self, forKey: .type) | ||
options = nil | ||
} | ||
|
||
/// Encode, note that options will not be included | ||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(id, forKey: .id) | ||
try container.encode(type, forKey: .type) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Sources/MapboxMaps/Style/CustomSources/CustomRasterSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Foundation | ||
|
||
/// Describes a Custom Raster Source to be used in the style. | ||
/// | ||
/// To add the data, set options with a ``CustomRasterSourceOptions`` with a fetchTileFunction callback | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
@_spi(Experimental) | ||
public struct CustomRasterSource: Source { | ||
|
||
/// The Source type | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
public let type: SourceType = .customRaster | ||
|
||
/// Style source identifier. | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
public let id: String | ||
|
||
/// Settings for the custom raster source, including a fetchTileFunction callback | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
public let options: CustomRasterSourceOptions? | ||
|
||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
public init(id: String, options: CustomRasterSourceOptions) { | ||
self.id = id | ||
self.options = options | ||
} | ||
} | ||
|
||
extension CustomRasterSource { | ||
enum CodingKeys: String, CodingKey { | ||
case id | ||
case type | ||
} | ||
|
||
/// Init from a decoder, note that the CustomRasterSourceOptions are not decodable and need to be set separately | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
id = try container.decode(String.self, forKey: .id) | ||
options = nil | ||
} | ||
|
||
/// Encode, note that options will not be included | ||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(id, forKey: .id) | ||
try container.encode(type, forKey: .type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Tests/MapboxMapsTests/Style/CustomSourcesIntegrationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import XCTest | ||
@_spi(Experimental) @testable import MapboxMaps | ||
|
||
final class CustomSourcesIntegrationTests: MapViewIntegrationTestCase { | ||
|
||
func testCustomRasterSourceAdditionAndRemoval() { | ||
let successfullyAddedSourceExpectation = XCTestExpectation(description: "Successfully added CustomRasterSource to Map") | ||
successfullyAddedSourceExpectation.expectedFulfillmentCount = 1 | ||
|
||
let successfullyRetrievedSourceExpectation = XCTestExpectation(description: "Successfully retrieved CustomRasterSource from Map") | ||
successfullyRetrievedSourceExpectation.expectedFulfillmentCount = 1 | ||
|
||
mapView.mapboxMap.styleURI = .standard | ||
|
||
didFinishLoadingStyle = { mapView in | ||
let source = CustomRasterSource(id: "test-source", options: CustomRasterSourceOptions(fetchTileFunction: { _ in }, cancelTileFunction: { _ in })) | ||
|
||
// Add source | ||
do { | ||
try mapView.mapboxMap.addSource(source) | ||
successfullyAddedSourceExpectation.fulfill() | ||
} catch { | ||
XCTFail("Failed to add CustomRasterSource because of error: \(error)") | ||
} | ||
|
||
// Retrieve the source | ||
do { | ||
_ = try mapView.mapboxMap.source(withId: "test-source", type: CustomRasterSource.self) | ||
successfullyRetrievedSourceExpectation.fulfill() | ||
} catch { | ||
XCTFail("Failed to retrieve CustomRasterSource because of error: \(error)") | ||
} | ||
} | ||
wait(for: [successfullyAddedSourceExpectation, successfullyRetrievedSourceExpectation], timeout: 5.0) | ||
} | ||
|
||
func testCustomGeometrySourceAdditionAndRemoval() { | ||
let successfullyAddedSourceExpectation = XCTestExpectation(description: "Successfully added CustomGeometrySource to Map") | ||
successfullyAddedSourceExpectation.expectedFulfillmentCount = 1 | ||
|
||
let successfullyRetrievedSourceExpectation = XCTestExpectation(description: "Successfully retrieved CustomGeometrySource from Map") | ||
successfullyRetrievedSourceExpectation.expectedFulfillmentCount = 1 | ||
|
||
mapView.mapboxMap.styleURI = .standard | ||
|
||
didFinishLoadingStyle = { mapView in | ||
let source = CustomGeometrySource(id: "test-source", options: CustomGeometrySourceOptions(fetchTileFunction: { _ in }, cancelTileFunction: { _ in }, tileOptions: TileOptions())) | ||
|
||
// Add source | ||
do { | ||
try mapView.mapboxMap.addSource(source) | ||
successfullyAddedSourceExpectation.fulfill() | ||
} catch { | ||
XCTFail("Failed to add CustomGeometrySource because of error: \(error)") | ||
} | ||
|
||
// Retrieve the source | ||
do { | ||
_ = try mapView.mapboxMap.source(withId: "test-source", type: CustomGeometrySource.self) | ||
successfullyRetrievedSourceExpectation.fulfill() | ||
} catch { | ||
XCTFail("Failed to retrieve CustomGeometrySource because of error: \(error)") | ||
} | ||
} | ||
wait(for: [successfullyAddedSourceExpectation, successfullyRetrievedSourceExpectation], timeout: 5.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// This file is generated. | ||
import XCTest | ||
@_spi(Experimental) @testable import MapboxMaps | ||
|
||
final class CustomSourcesSourceTests: XCTestCase { | ||
|
||
func testRasterEncodingAndDecoding() { | ||
let testCustomRasterSourceOptions = CustomRasterSourceOptions(fetchTileFunction: { _ in }, cancelTileFunction: { _ in }) | ||
|
||
var source = CustomRasterSource(id: "test-source", options: testCustomRasterSourceOptions) | ||
|
||
var data: Data? | ||
do { | ||
data = try JSONEncoder().encode(source) | ||
} catch { | ||
XCTFail("Failed to encode CustomRasterSource.") | ||
} | ||
|
||
guard let validData = data else { | ||
XCTFail("Failed to encode CustomRasterSource.") | ||
return | ||
} | ||
|
||
do { | ||
let decodedSource = try JSONDecoder().decode(CustomRasterSource.self, from: validData) | ||
XCTAssert(decodedSource.type == SourceType.customRaster) | ||
XCTAssert(decodedSource.id == "test-source") | ||
XCTAssertNil(decodedSource.options) | ||
} catch { | ||
XCTFail("Failed to decode CustomRasterSource.") | ||
} | ||
} | ||
|
||
func testGeometryEncodingAndDecoding() { | ||
let testCustomGeometrySourceOptions = CustomGeometrySourceOptions(fetchTileFunction: { _ in }, cancelTileFunction: { _ in }, tileOptions: TileOptions()) | ||
|
||
var source = CustomGeometrySource(id: "test-source", options: testCustomGeometrySourceOptions) | ||
|
||
var data: Data? | ||
do { | ||
data = try JSONEncoder().encode(source) | ||
} catch { | ||
XCTFail("Failed to encode CustomRasterSource.") | ||
} | ||
|
||
guard let validData = data else { | ||
XCTFail("Failed to encode CustomRasterSource.") | ||
return | ||
} | ||
|
||
do { | ||
let decodedSource = try JSONDecoder().decode(CustomGeometrySource.self, from: validData) | ||
XCTAssert(decodedSource.type == SourceType.customGeometry) | ||
XCTAssert(decodedSource.id == "test-source") | ||
XCTAssertNil(decodedSource.options) | ||
} catch { | ||
XCTFail("Failed to decode CustomRasterSource.") | ||
} | ||
} | ||
} | ||
|
||
// End of generated file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters