Skip to content
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

Merged
merged 15 commits into from
May 28, 2021
Merged

Introduce ResourceOptionsManager #396

merged 15 commits into from
May 28, 2021

Conversation

julianrex
Copy link
Contributor

@julianrex julianrex commented May 26, 2021

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:

  • Briefly describe the changes in this PR.
  • Write tests for all new functionality. If tests were not written, please explain why. Updated
  • Document any changes to public APIs.
  • Apply changelog label ('breaking change', 'bug 🪲', 'build', 'docs', 'feature 🍏', 'performance ⚡', 'testing 💯') or use the label 'skip changelog'
  • Add an entry inside this element for inclusion in the mapbox-maps-ios changelog: <changelog></changelog>. Manual edit.
  • Update the migration guide, API Docs, Markdown files - Readme, Developing, etc

Summary of changes

This PR replaces CredentialsManager with ResourceOptionsManager creating a struct based ResourceOptions that shadows the type from MapboxCoreMaps. The bulk of the other changes are related to this except for the following:

  • Update the cache path to live in .mapbox. This aligns with other frameworks.
  • CI creates the MapboxAccessToken file - since it appears that the build pre-action script is not being run
  • Updated min/max latitude projection test
  • Re-enabled testCacheManagerIsReleasedAndHandlerRetainingManagerIsCalled
  • Fix MapView retain issue with integration tests
  • Fix ResourceEvent and associated test

@julianrex julianrex added the breaking change ⚠️ If your pull request introduces a breaking change and updates are required when version is published label May 27, 2021
@julianrex julianrex changed the title ResourceOptionsManager Introduce ResourceOptionsManager May 27, 2021

/// The access token to access the resource. This should be a valid, non-empty,
/// Mapbox access token
public var accessToken: String
Copy link
Contributor Author

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.

Copy link
Contributor

@nishant-karajgikar nishant-karajgikar May 27, 2021

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?

Copy link
Member

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 {
Copy link
Contributor Author

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
}
Copy link
Contributor Author

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.

Copy link
Member

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

Sources/MapboxMaps/Foundation/ResourceOptionsManager.swift Outdated Show resolved Hide resolved
/// ```
///
/// - Parameter accessToken: Valid access token or `nil`
public convenience init(accessToken: String? = nil) {
Copy link
Contributor Author

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.

Copy link
Member

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())
Copy link
Contributor Author

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.

Comment on lines +91 to +98
// 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)
Copy link
Contributor Author

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated test fix.

glyphsRasterizationOptions: GlyphsRasterizationOptions? = GlyphsRasterizationOptions(fontFamilies: []),
resourceOptions: ResourceOptions = ResourceOptions()) {
glyphsRasterizationOptions: GlyphsRasterizationOptions? = GlyphsRasterizationOptions(),
resourceOptions: ResourceOptions = ResourceOptionsManager.default.resourceOptions) {
Copy link
Member

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...

Comment on lines +4 to +19
/// 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
/// }
/// ```
Copy link
Member

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)
Copy link
Member

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?

Copy link
Contributor Author

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".

Comment on lines +141 to +143
if token.isEmpty {
Log.warning(forMessage: "Empty access token.", category: "ResourceOptions")
}
Copy link
Member

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?

Copy link
Member

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

Copy link
Contributor Author

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.

@pengdev pengdev requested a review from Chaoba May 27, 2021 15:56
@julianrex julianrex marked this pull request as ready for review May 28, 2021 00:02
@julianrex julianrex merged commit 31241bf into main May 28, 2021
@julianrex julianrex deleted the jrex/resource-manager branch May 28, 2021 00:02
@julianrex julianrex mentioned this pull request May 28, 2021
3 tasks
@evil159 evil159 mentioned this pull request May 24, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change ⚠️ If your pull request introduces a breaking change and updates are required when version is published
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants