-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial solution to moving target problem (#1245)
- Loading branch information
Showing
25 changed files
with
1,182 additions
and
216 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
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.