-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add initial solution to moving target problem #1245
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions
21
Sources/MapboxMaps/Viewport/Transitions/Default/CameraOptionsComponent.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,21 @@ | ||
internal protocol CameraOptionsComponentProtocol { | ||
var cameraOptions: CameraOptions { get } | ||
func updated(with cameraOptions: CameraOptions) -> CameraOptionsComponentProtocol? | ||
} | ||
|
||
internal struct CameraOptionsComponent<T>: CameraOptionsComponentProtocol { | ||
internal let keyPath: WritableKeyPath<CameraOptions, T?> | ||
internal let value: T | ||
|
||
internal var cameraOptions: CameraOptions { | ||
var cameraOptions = CameraOptions() | ||
cameraOptions[keyPath: keyPath] = value | ||
return cameraOptions | ||
} | ||
|
||
internal func updated(with cameraOptions: CameraOptions) -> CameraOptionsComponentProtocol? { | ||
cameraOptions[keyPath: keyPath].map { | ||
CameraOptionsComponent(keyPath: keyPath, value: $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
81 changes: 81 additions & 0 deletions
81
Sources/MapboxMaps/Viewport/Transitions/Default/DefaultViewportTransitionAnimation.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,81 @@ | ||
import Foundation | ||
|
||
internal protocol DefaultViewportTransitionAnimationProtocol: Cancelable { | ||
func updateTargetCamera(with cameraOptions: CameraOptions) | ||
func start(with completion: @escaping (Bool) -> Void) | ||
} | ||
|
||
internal final class DefaultViewportTransitionAnimation: DefaultViewportTransitionAnimationProtocol { | ||
private let components: [DefaultViewportTransitionAnimationProtocol] | ||
|
||
internal init(components: [DefaultViewportTransitionAnimationProtocol]) { | ||
self.components = components | ||
} | ||
|
||
func start(with completion: @escaping (Bool) -> Void) { | ||
let group = DispatchGroup() | ||
var allFinished = true | ||
for component in components { | ||
group.enter() | ||
component.start { isFinished in | ||
allFinished = allFinished && isFinished | ||
group.leave() | ||
} | ||
} | ||
group.notify(queue: .main) { | ||
completion(allFinished) | ||
} | ||
} | ||
|
||
internal func updateTargetCamera(with cameraOptions: CameraOptions) { | ||
for component in components { | ||
component.updateTargetCamera(with: cameraOptions) | ||
} | ||
} | ||
|
||
internal func cancel() { | ||
for component in components { | ||
component.cancel() | ||
} | ||
} | ||
} | ||
|
||
internal final class DefaultViewportTransitionAnimationComponent: DefaultViewportTransitionAnimationProtocol { | ||
private let animator: SimpleCameraAnimatorProtocol | ||
private let delay: TimeInterval | ||
private let cameraOptionsComponent: CameraOptionsComponentProtocol | ||
private let mapboxMap: MapboxMapProtocol | ||
|
||
internal init(animator: SimpleCameraAnimatorProtocol, | ||
delay: TimeInterval, | ||
cameraOptionsComponent: CameraOptionsComponentProtocol, | ||
mapboxMap: MapboxMapProtocol) { | ||
self.animator = animator | ||
self.delay = delay | ||
self.cameraOptionsComponent = cameraOptionsComponent | ||
self.mapboxMap = mapboxMap | ||
} | ||
|
||
func start(with completion: @escaping (Bool) -> Void) { | ||
animator.addCompletion { position in | ||
completion(position != .current) | ||
} | ||
animator.startAnimation(afterDelay: delay) | ||
} | ||
|
||
internal func updateTargetCamera(with cameraOptions: CameraOptions) { | ||
guard let updatedComponent = cameraOptionsComponent.updated(with: cameraOptions) else { | ||
return | ||
} | ||
let isComplete = animator.state == .inactive | ||
if isComplete { | ||
mapboxMap.setCamera(to: updatedComponent.cameraOptions) | ||
} else { | ||
animator.to = updatedComponent.cameraOptions | ||
} | ||
} | ||
|
||
internal func cancel() { | ||
animator.cancel() | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...s/MapboxMaps/Viewport/Transitions/Default/DefaultViewportTransitionAnimationFactory.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,31 @@ | ||
import Foundation | ||
|
||
internal protocol DefaultViewportTransitionAnimationFactoryProtocol: AnyObject { | ||
func makeAnimationComponent(animator: SimpleCameraAnimatorProtocol, | ||
delay: TimeInterval, | ||
cameraOptionsComponent: CameraOptionsComponentProtocol) -> DefaultViewportTransitionAnimationProtocol | ||
|
||
func makeAnimation(components: [DefaultViewportTransitionAnimationProtocol]) -> DefaultViewportTransitionAnimationProtocol | ||
} | ||
|
||
internal final class DefaultViewportTransitionAnimationFactory: DefaultViewportTransitionAnimationFactoryProtocol { | ||
private let mapboxMap: MapboxMapProtocol | ||
|
||
internal init(mapboxMap: MapboxMapProtocol) { | ||
self.mapboxMap = mapboxMap | ||
} | ||
|
||
internal func makeAnimation(components: [DefaultViewportTransitionAnimationProtocol]) -> DefaultViewportTransitionAnimationProtocol { | ||
return DefaultViewportTransitionAnimation(components: components) | ||
} | ||
|
||
internal func makeAnimationComponent(animator: SimpleCameraAnimatorProtocol, | ||
delay: TimeInterval, | ||
cameraOptionsComponent: CameraOptionsComponentProtocol) -> DefaultViewportTransitionAnimationProtocol { | ||
return DefaultViewportTransitionAnimationComponent( | ||
animator: animator, | ||
delay: delay, | ||
cameraOptionsComponent: cameraOptionsComponent, | ||
mapboxMap: mapboxMap) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
does it have to be a key path to an optional property of
CameraOptions
? I see that CameraOptions has everything as Optional now, but if in the future we decide to add new property that is non-optional then this will not work for those properties.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.
It wouldn't have to be. Doing it this way allows
value
(on the next line) to be non-optional.The
*Options
naming pattern in GL Native typically indicates a type where all of its fields are optional. You pass instances to a setter and non-nil fields are updated and any nil fields are not modified. Given that, it's probably unlikely that we'll add non-optional fields toCameraOptions
in the future.