-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce ResourceOptionsManager #396
Conversation
|
||
/// The access token to access the resource. This should be a valid, non-empty, | ||
/// Mapbox access token | ||
public var accessToken: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like us to consider making access token a custom type, for example an enum. Not unlike an optional, but instead of a .none
case, we could have a .search
or .default
case that would look for the token in the Info.plist etc. This way it's more explicit and we could deprecate it later if we ever choose to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea.. may be we could even make it expressibleByString
to get the best of both worlds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @Chaoba
on Android we can not overwrite the type in the ResourceOptions..
} | ||
|
||
extension ResourceOptions: CustomStringConvertible, CustomDebugStringConvertible { | ||
private func redactedAccessToken() -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do add a custom AccessToken type, then this description logic would move there too.
internal static func destroyDefault() { | ||
defaultInstance = nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in preparation of being able to clean up TileStores completely - useful for tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are considering to expose the same cc @Chaoba
/// ``` | ||
/// | ||
/// - Parameter accessToken: Valid access token or `nil` | ||
public convenience init(accessToken: String? = nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also added a convenience that just takes an access token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are aligned here.
/// | ||
/// - Parameter accessToken: Valid access token or `nil` | ||
internal func reset(resourceOptions: ResourceOptions? = nil) { | ||
self.resourceOptions = resourceOptions ?? ResourceOptions(accessToken: defaultAccessToken()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default access token logic should probably move to where the core ResourceOptions object is initialized from the struct.
// Check clamping | ||
let northMax = CLLocationCoordinate2D(latitude: Projection.latitudeMax, longitude: 0) | ||
let northMaxMercator = projectorType.project(northMax, zoomScale: zoomScale) | ||
XCTAssertEqual(northMercator.y, northMaxMercator.y, accuracy: 0.000001) | ||
|
||
let southMin = CLLocationCoordinate2D(latitude: Projection.latitudeMin, longitude: 0) | ||
let southMinMercator = projectorType.project(southMin, zoomScale: zoomScale) | ||
XCTAssertEqual(southMercator.y, southMinMercator.y, accuracy: 0.000001) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated test - will likely leave in this PR for simplicity
@@ -33,7 +34,7 @@ internal struct ResourceEventResponse: Decodable { | |||
var noContent: Bool | |||
var notModified: Bool | |||
var mustRevalidate: Bool | |||
var offlineData: Bool | |||
var source: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated test fix.
Sources/MapboxMaps/Foundation/Extensions/Core/CacheManager.swift
Outdated
Show resolved
Hide resolved
glyphsRasterizationOptions: GlyphsRasterizationOptions? = GlyphsRasterizationOptions(fontFamilies: []), | ||
resourceOptions: ResourceOptions = ResourceOptions()) { | ||
glyphsRasterizationOptions: GlyphsRasterizationOptions? = GlyphsRasterizationOptions(), | ||
resourceOptions: ResourceOptions = ResourceOptionsManager.default.resourceOptions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MapSnapshotOptions is a gl-native generated class in Android, and we couldn't override the default value here...
/// Convenience class that manages a global `ResourceOptions` | ||
/// | ||
/// It's possible to create `ResourceOptionsManager` instances as you need them, | ||
/// however it's convenient to use the default object (`default`). | ||
/// | ||
/// For example, we recommend that the Mapbox access token be set in | ||
/// `application(_:didFinishLaunchingWithOptions:)` rather than relying on the | ||
/// value in your application's Info.plist: | ||
/// | ||
/// ``` | ||
/// func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | ||
/// // Override point for customization after application launch. | ||
/// ResourceOptionsManager.default.resourceOptions.accessToken = "overridden-access-token" | ||
/// return true | ||
/// } | ||
/// ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Chaoba let's also align on the documentation
/// A valid access token must be provided or found. | ||
public static var `default`: ResourceOptionsManager { | ||
if defaultInstance == nil { | ||
defaultInstance = ResourceOptionsManager(accessToken: nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on iOS, do you throw exceptions when the accessToken is null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not at the moment, does Android? Also, nil
in this context means "search for the token".
if token.isEmpty { | ||
Log.warning(forMessage: "Empty access token.", category: "ResourceOptions") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you don't throw exceptions when the token is not found, and instead just show a warning message? What does it look like to users? will the MapView will show a black screen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, if the access token is not found anywhere, we will throw a runtime exception that crashes the app..
cc @Chaoba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do the same - though there's some trickiness around testing.
[run device tests]
[run device tests]
Co-authored-by: Peng Liu <peng.liu@mapbox.com>
ce2d94f
to
4d54035
Compare
PRs must be submitted under the terms of our Contributor License Agreement CLA.
Fixes: < Link to related issues that will be fixed by this pull request, if they exist >
Pull request checklist:
Write tests for all new functionality. If tests were not written, please explain why.UpdatedAdd an entry inside this element for inclusion in theManual edit.mapbox-maps-ios
changelog:<changelog></changelog>
.Summary of changes
This PR replaces
CredentialsManager
withResourceOptionsManager
creating a struct basedResourceOptions
that shadows the type from MapboxCoreMaps. The bulk of the other changes are related to this except for the following:.mapbox
. This aligns with other frameworks.MapboxAccessToken
file - since it appears that the build pre-action script is not being runtestCacheManagerIsReleasedAndHandlerRetainingManagerIsCalled
ResourceEvent
and associated test