Skip to content

Commit

Permalink
move ui updates methods to class
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Sudekum authored and frederoni committed Feb 14, 2018
1 parent 518a3d1 commit 4a47ef0
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" ~> 3.7
github "mapbox/MapboxDirections.swift" ~> 0.16.1
github "mapbox/MapboxDirections.swift" "banner-types"
github "mapbox/turf-swift" ~> 0.0.3
github "rs/SDWebImage" ~> 4.3
github "mapbox/mapbox-events-ios" ~> 0.3
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" "3.7.4"
github "ceeK/Solar" "2.1.0"
github "facebook/ios-snapshot-test-case" "2.1.4"
github "mapbox/MapboxDirections.swift" "v0.16.1"
github "mapbox/MapboxDirections.swift" "021b0c665f6b1ddabbb32d2fc0c1d119911c9c33"
github "mapbox/mapbox-events-ios" "v0.3.0"
github "mapbox/mapbox-voice-swift" "v0.0.1"
github "mapbox/turf-swift" "v0.0.4"
Expand Down
19 changes: 17 additions & 2 deletions MapboxNavigation/InstructionsBannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ open class BaseInstructionsBannerView: UIControl {
delegate?.didTapInstructionsBanner(self)
}

func set(_ primaryInstruction: [VisualInstructionComponent]?, secondaryInstruction: [VisualInstructionComponent]?) {
func set(_ instruction: VisualInstruction) {
let secondaryInstruction = instruction.secondaryTextComponents
primaryLabel.numberOfLines = secondaryInstruction == nil ? 2 : 1

if secondaryInstruction == nil {
Expand All @@ -80,8 +81,9 @@ open class BaseInstructionsBannerView: UIControl {
baselineAlignInstructions()
}

primaryLabel.instruction = primaryInstruction
primaryLabel.instruction = instruction.primaryTextComponents
secondaryLabel.instruction = secondaryInstruction
maneuverView.visualInstruction = instruction
}

override open func prepareForInterfaceBuilder() {
Expand All @@ -92,4 +94,17 @@ open class BaseInstructionsBannerView: UIControl {

distance = 100
}

/**
Updates the instructions banner for a given `RouteProgress`.
*/
public func update(for routeProgress: RouteProgress) {
let stepProgress = routeProgress.currentLegProgress.currentStepProgress
let distanceRemaining = stepProgress.distanceRemaining

guard let visualInstruction = routeProgress.currentLegProgress.currentStep.instructionsDisplayedAlongStep?.last else { return }

set(visualInstruction)
distance = distanceRemaining > 5 ? distanceRemaining : 0
}
}
31 changes: 30 additions & 1 deletion MapboxNavigation/LanesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public class LanesView: UIView {
separatorView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
}

func updateLaneViews(step: RouteStep, durationRemaining: TimeInterval) {
func update(for routeProgress: RouteProgress) {
guard let step = routeProgress.currentLegProgress.upComingStep else { return }
guard !routeProgress.currentLegProgress.userHasArrivedAtWaypoint else { return }
let durationRemaining = routeProgress.currentLegProgress.currentStepProgress.durationRemaining

clearLaneViews()

if let allLanes = step.intersections?.first?.approachLanes,
Expand All @@ -80,6 +84,30 @@ public class LanesView: UIView {
stackView.addArrangedSubview(laneView)
}
}

if stackView.arrangedSubviews.count > 0 {
show()
} else {
hide()
}
}

public func show(animated: Bool = true) {
guard isHidden == true else { return }
if animated {
UIView.defaultAnimation(0.3, animations: {
self.isHidden = false
}, completion: nil)
} else {
self.isHidden = false
}
}

public func hide() {
guard isHidden == false else { return }
UIView.defaultAnimation(0.3, animations: {
self.isHidden = true
}, completion: nil)
}

fileprivate func clearLaneViews() {
Expand All @@ -88,4 +116,5 @@ public class LanesView: UIView {
$0.removeFromSuperview()
}
}

}
25 changes: 12 additions & 13 deletions MapboxNavigation/ManeuverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ public class ManeuverView: UIView {
}
}

@objc public var step: RouteStep? {
@objc public var isStart = false {
didSet {
setNeedsDisplay()
}
}

@objc public var isStart = false {
@objc public var isEnd = false {
didSet {
setNeedsDisplay()
}
}

@objc public var isEnd = false {
@IBInspectable
var scale: CGFloat = 1 {
didSet {
setNeedsDisplay()
}
}

@IBInspectable
var scale: CGFloat = 1 {
@objc public var visualInstruction: VisualInstruction? {
didSet {
setNeedsDisplay()
}
Expand All @@ -57,7 +57,7 @@ public class ManeuverView: UIView {
return
#endif

guard let step = step else {
guard let visualInstruction = visualInstruction else {
if isStart {
ManeuversStyleKit.drawStarting(frame: bounds, resizing: resizing, primaryColor: primaryColor)
} else if isEnd {
Expand All @@ -67,9 +67,8 @@ public class ManeuverView: UIView {
}

var flip: Bool = false
let type: ManeuverType = step.maneuverType != .none ? step.maneuverType : .turn
let angle = ((step.finalHeading ?? 0) - (step.initialHeading ?? 0)).wrap(min: -180, max: 180)
let direction: ManeuverDirection = step.maneuverDirection != .none ? step.maneuverDirection : ManeuverDirection(angle: Int(angle))
let type = visualInstruction.maneuverType != .none ? visualInstruction.maneuverType : .turn
let direction = visualInstruction.maneuverDirection != .none ? visualInstruction.maneuverDirection : .straightAhead

switch type {
case .merge:
Expand All @@ -85,13 +84,13 @@ public class ManeuverView: UIView {
switch direction {
case .straightAhead:
ManeuversStyleKit.drawRoundabout(frame: bounds, resizing: resizing, primaryColor: primaryColor, secondaryColor: secondaryColor, roundabout_angle: 180)
flip = step.drivingSide == .left
flip = visualInstruction.drivingSide == .left
case .left, .slightLeft, .sharpLeft:
ManeuversStyleKit.drawRoundabout(frame: bounds, resizing: resizing, primaryColor: primaryColor, secondaryColor: secondaryColor, roundabout_angle: 275)
flip = step.drivingSide == .left
flip = visualInstruction.drivingSide == .left
default:
ManeuversStyleKit.drawRoundabout(frame: bounds, resizing: resizing, primaryColor: primaryColor, secondaryColor: secondaryColor, roundabout_angle: 90)
flip = step.drivingSide == .left
flip = visualInstruction.drivingSide == .left
}
case .arrive:
switch direction {
Expand Down Expand Up @@ -125,7 +124,7 @@ public class ManeuverView: UIView {
flip = true
case .uTurn:
ManeuversStyleKit.drawArrow180right(frame: bounds, resizing: resizing, primaryColor: primaryColor)
flip = step.drivingSide == .right
flip = visualInstruction.drivingSide == .right
default:
ManeuversStyleKit.drawArrowstraight(frame: bounds, resizing: resizing, primaryColor: primaryColor)
}
Expand Down
44 changes: 44 additions & 0 deletions MapboxNavigation/NextBannerView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UIKit
import MapboxDirections
import MapboxCoreNavigation

/// :nodoc:
@objc(MBNextInstructionLabel)
Expand Down Expand Up @@ -71,4 +72,47 @@ open class NextBannerView: UIView {
instructionLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true
}

func shouldShowNextBanner(for routeProgress: RouteProgress) -> Bool {
guard let upcomingStep = routeProgress.currentLegProgress.upComingStep, let nextStep = routeProgress.currentLegProgress.stepAfter(upcomingStep) else {
return false
}

// If the followon step is short and the user is near the end of the current step, show the nextBanner.
guard nextStep.expectedTravelTime <= RouteControllerHighAlertInterval * RouteControllerLinkedInstructionBufferMultiplier,
upcomingStep.expectedTravelTime <= RouteControllerHighAlertInterval * RouteControllerLinkedInstructionBufferMultiplier else {
return false
}

guard let _ = upcomingStep.instructionsDisplayedAlongStep?.last else { return false }

return true
}

public func update(for routeProgress: RouteProgress) {
guard shouldShowNextBanner(for: routeProgress) else {
hide()
return
}

guard let instruction = routeProgress.currentLegProgress.upComingStep?.instructionsDisplayedAlongStep?.last else { return }

maneuverView.visualInstruction = instruction
instructionLabel.instruction = instruction.primaryTextComponents
show()
}

public func show() {
guard isHidden else { return }
UIView.defaultAnimation(0.3, animations: {
self.isHidden = false
}, completion: nil)
}

public func hide() {
guard !isHidden else { return }
UIView.defaultAnimation(0.3, animations: {
self.isHidden = true
}, completion: nil)
}

}
104 changes: 10 additions & 94 deletions MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class RouteMapViewController: UIViewController {
@IBOutlet weak var bannerContainerShowConstraint: NSLayoutConstraint!

var route: Route { return routeController.routeProgress.route }
var previousStep: RouteStep?
var updateETATimer: Timer?
var previewInstructionsView: StepInstructionsView?
var lastTimeUserRerouted: Date?
Expand Down Expand Up @@ -286,8 +285,10 @@ class RouteMapViewController: UIViewController {
func notifyDidReroute(route: Route) {
updateETA()

if let location = routeController.location {
updateInstructions(routeProgress: routeController.routeProgress, location: location, secondsRemaining: 0)
instructionsBannerView.update(for: routeController.routeProgress)
lanesView.update(for: routeController.routeProgress)
if lanesView.isHidden {
nextBannerView.update(for: routeController.routeProgress)
}

mapView.addArrow(route: routeController.routeProgress.route, legIndex: routeController.routeProgress.legIndex, stepIndex: routeController.routeProgress.currentLegProgress.stepIndex + 1)
Expand Down Expand Up @@ -319,7 +320,7 @@ class RouteMapViewController: UIViewController {

@objc func willReroute(notification: NSNotification) {
let title = NSLocalizedString("REROUTING", bundle: .mapboxNavigation, value: "Rerouting…", comment: "Indicates that rerouting is in progress")
hideLaneViews()
lanesView.hide()
statusView.show(title, showSpinner: true)
statusView.hide(delay: 3, animated: true)
}
Expand Down Expand Up @@ -393,16 +394,12 @@ class RouteMapViewController: UIViewController {

updateETA()

let step = routeProgress.currentLegProgress.upComingStep ?? routeProgress.currentLegProgress.currentStep

if let upComingStep = routeProgress.currentLegProgress?.upComingStep, !routeProgress.currentLegProgress.userHasArrivedAtWaypoint {
updateLaneViews(step: upComingStep, durationRemaining: routeProgress.currentLegProgress.currentStepProgress.durationRemaining)
lanesView.update(for: routeProgress)
instructionsBannerView.update(for: routeProgress)
if lanesView.isHidden {
nextBannerView.update(for: routeProgress)
}

previousStep = step
updateInstructions(routeProgress: routeProgress, location: location, secondsRemaining: secondsRemaining)
updateNextBanner(routeProgress: routeProgress)

if currentLegIndexMapped != routeProgress.legIndex {
mapView.showWaypoints(routeProgress.route, legIndex: routeProgress.legIndex)
mapView.showRoutes([routeProgress.route], legIndex: routeProgress.legIndex)
Expand All @@ -423,58 +420,6 @@ class RouteMapViewController: UIViewController {
}
}

func updateInstructions(routeProgress: RouteProgress, location: CLLocation, secondsRemaining: TimeInterval) {
let stepProgress = routeProgress.currentLegProgress.currentStepProgress
let distanceRemaining = stepProgress.distanceRemaining

guard let visualInstruction = routeProgress.currentLegProgress.currentStep.instructionsDisplayedAlongStep?.last else { return }

instructionsBannerView.set(visualInstruction.primaryTextComponents, secondaryInstruction: visualInstruction.secondaryTextComponents)
instructionsBannerView.distance = distanceRemaining > 5 ? distanceRemaining : 0
instructionsBannerView.maneuverView.step = routeProgress.currentLegProgress.upComingStep
}

func updateNextBanner(routeProgress: RouteProgress) {

guard let upcomingStep = routeProgress.currentLegProgress.upComingStep,
let nextStep = routeProgress.currentLegProgress.stepAfter(upcomingStep),
lanesView.isHidden
else {
hideNextBanner()
return
}

// If the followon step is short and the user is near the end of the current step, show the nextBanner.
guard nextStep.expectedTravelTime <= RouteControllerHighAlertInterval * RouteControllerLinkedInstructionBufferMultiplier,
routeProgress.currentLegProgress.currentStepProgress.durationRemaining <= RouteControllerHighAlertInterval * RouteControllerLinkedInstructionBufferMultiplier else {
hideNextBanner()
return
}

guard let instructions = upcomingStep.instructionsDisplayedAlongStep?.last else {
hideNextBanner()
return
}

nextBannerView.maneuverView.step = nextStep
nextBannerView.instructionLabel.instruction = instructions.primaryTextComponents
showNextBanner()
}

func showNextBanner() {
guard nextBannerView.isHidden else { return }
UIView.defaultAnimation(0.3, animations: {
self.nextBannerView.isHidden = false
}, completion: nil)
}

func hideNextBanner() {
guard !nextBannerView.isHidden else { return }
UIView.defaultAnimation(0.3, animations: {
self.nextBannerView.isHidden = true
}, completion: nil)
}

var contentInsets: UIEdgeInsets {
var margin: CGFloat = 0.0
if #available(iOS 11.0, *) { margin = view.safeAreaInsets.bottom }
Expand All @@ -483,34 +428,6 @@ class RouteMapViewController: UIViewController {
return UIEdgeInsets(top: instructionsBannerContainerView.bounds.height, left: 0, bottom: bottom, right: 0)
}

func updateLaneViews(step: RouteStep, durationRemaining: TimeInterval) {
lanesView.updateLaneViews(step: step, durationRemaining: durationRemaining)

if lanesView.stackView.arrangedSubviews.count > 0 {
showLaneViews()
} else {
hideLaneViews()
}
}

func showLaneViews(animated: Bool = true) {
hideNextBanner()
guard lanesView.isHidden == true else { return }
if animated {
UIView.defaultAnimation(0.3, animations: {
self.lanesView.isHidden = false
}, completion: nil)
} else {
self.lanesView.isHidden = false
}
}

func hideLaneViews() {
guard lanesView.isHidden == false else { return }
UIView.defaultAnimation(0.3, animations: {
self.lanesView.isHidden = true
}, completion: nil)
}
// MARK: End Of Route

func showEndOfRoute(duration: TimeInterval = 0.3, completion: ((Bool) -> Void)? = nil) {
Expand Down Expand Up @@ -858,8 +775,7 @@ extension RouteMapViewController: StepsViewControllerDelegate {
let instructionsView = StepInstructionsView(frame: instructionsBannerView.frame)
instructionsView.backgroundColor = StepInstructionsView.appearance().backgroundColor
instructionsView.delegate = self
instructionsView.set(instructions.primaryTextComponents, secondaryInstruction: instructions.secondaryTextComponents)
instructionsView.maneuverView.step = maneuverStep
instructionsView.set(instructions)
instructionsView.distance = distance

instructionsBannerContainerView.backgroundColor = instructionsView.backgroundColor
Expand Down
Loading

0 comments on commit 4a47ef0

Please sign in to comment.