Skip to content

Commit 3a75f43

Browse files
committed
Fix LaneView styling on CarPlay.
1 parent 8e7485d commit 3a75f43

File tree

7 files changed

+229
-143
lines changed

7 files changed

+229
-143
lines changed

Sources/MapboxNavigation/DayStyle.swift

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ open class DayStyle: Style {
258258
NextInstructionLabel.appearance(for: phoneTraitCollection, whenContainedInInstancesOf: [NextBannerView.self]).textColorHighlighted = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
259259
NextInstructionLabel.appearance(for: phoneTraitCollection, whenContainedInInstancesOf: [NextBannerView.self]).normalFont = UIFont.systemFont(ofSize: 14.0).adjustedFont
260260

261-
LaneView.appearance(for: phoneTraitCollection).primaryColor = .defaultLaneArrowPrimaryCarPlay
262-
LaneView.appearance(for: phoneTraitCollection).secondaryColor = .defaultLaneArrowSecondaryCarPlay
261+
LaneView.appearance(for: phoneTraitCollection).primaryColor = .defaultLaneArrowPrimary
262+
LaneView.appearance(for: phoneTraitCollection).secondaryColor = .defaultLaneArrowSecondary
263263
LaneView.appearance(for: phoneTraitCollection).primaryColorHighlighted = .defaultLaneArrowPrimaryHighlighted
264264
LaneView.appearance(for: phoneTraitCollection).secondaryColorHighlighted = .defaultLaneArrowSecondaryHighlighted
265265
LaneView.appearance(for: phoneTraitCollection, whenContainedInInstancesOf: [LanesView.self]).primaryColor = .defaultLaneArrowPrimary
@@ -355,12 +355,6 @@ open class DayStyle: Style {
355355
SpeedLimitView.appearance(for: carPlayTraitCollection).textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
356356
SpeedLimitView.appearance(for: carPlayTraitCollection).regulatoryBorderColor = #colorLiteral(red: 0.800, green: 0, blue: 0, alpha: 1)
357357

358-
LaneView.appearance(for: carPlayTraitCollection).primaryColor = .defaultLaneArrowPrimaryCarPlay
359-
LaneView.appearance(for: carPlayTraitCollection).secondaryColor = .defaultLaneArrowSecondaryCarPlay
360-
361-
LaneView.appearance(for: carPlayTraitCollection).primaryColorHighlighted = .defaultLaneArrowPrimaryHighlighted
362-
LaneView.appearance(for: carPlayTraitCollection).secondaryColorHighlighted = .defaultLaneArrowSecondaryHighlighted
363-
364358
ManeuverView.appearance(for: carPlayTraitCollection).backgroundColor = .clear
365359
ManeuverView.appearance(for: carPlayTraitCollection).primaryColorHighlighted = .defaultTurnArrowPrimaryHighlighted
366360
ManeuverView.appearance(for: carPlayTraitCollection).secondaryColorHighlighted = .defaultTurnArrowSecondaryHighlighted
@@ -369,34 +363,27 @@ open class DayStyle: Style {
369363
// `UITraitCollection`, which contains both `UIUserInterfaceIdiom` and `UIUserInterfaceStyle`.
370364
// If not, `UITraitCollection` will only contain `UIUserInterfaceIdiom`.
371365
if #available(iOS 12.0, *) {
372-
let carPlayTraitCollection = UITraitCollection(userInterfaceIdiom: .carPlay)
373-
374366
let carPlayLightTraitCollection = UITraitCollection(traitsFrom: [
375367
carPlayTraitCollection,
376368
UITraitCollection(userInterfaceStyle: .light)
377369
])
378-
setCarPlayInstructionsStyling(for: carPlayLightTraitCollection)
370+
applyCarPlayStyling(for: carPlayLightTraitCollection)
379371

380372
let carPlayDarkTraitCollection = UITraitCollection(traitsFrom: [
381373
carPlayTraitCollection,
382374
UITraitCollection(userInterfaceStyle: .dark)
383375
])
384-
setCarPlayInstructionsStyling(for: carPlayDarkTraitCollection)
376+
applyCarPlayStyling(for: carPlayDarkTraitCollection)
385377
} else {
386-
setDefaultCarPlayInstructionsStyling()
378+
applyDefaultCarPlayStyling()
387379
}
388380
default:
389381
break
390382
}
391383
}
392384

393385
@available(iOS 12.0, *)
394-
func setCarPlayInstructionsStyling(for traitCollection: UITraitCollection?) {
395-
guard let traitCollection = traitCollection,
396-
traitCollection.userInterfaceIdiom == .carPlay else { return }
397-
398-
let carPlayTraitCollection = UITraitCollection(userInterfaceIdiom: .carPlay)
399-
386+
func applyCarPlayStyling(for traitCollection: UITraitCollection) {
400387
// On CarPlay, `ExitView` and `GenericRouteShield` styling depends on `UIUserInterfaceStyle`,
401388
// which was set on CarPlay external screen.
402389
// In case if it was set to `UIUserInterfaceStyle.light` white color will be used, otherwise
@@ -405,51 +392,55 @@ open class DayStyle: Style {
405392
// property of which returns incorrect value), this property has to be taken from callbacks
406393
// similar to: `UITraitEnvironment.traitCollectionDidChange(_:)`, or by creating `UITraitCollection`
407394
// directly.
395+
let defaultInstructionColor: UIColor
396+
397+
let defaultLaneViewPrimaryColor: UIColor
398+
let defaultLaneViewSecondaryColor: UIColor
399+
400+
let defaultLaneArrowPrimaryHighlightedColor: UIColor
401+
let defaultLaneArrowSecondaryHighlightedColor: UIColor
402+
408403
switch traitCollection.userInterfaceStyle {
409-
case .dark:
410-
let defaultColor = UIColor.white
404+
case .light, .unspecified:
405+
defaultInstructionColor = UIColor.black
411406

412-
let carPlayDarkTraitCollection = UITraitCollection(traitsFrom: [
413-
carPlayTraitCollection,
414-
UITraitCollection(userInterfaceStyle: .dark)
415-
])
407+
defaultLaneViewPrimaryColor = .defaultLaneArrowPrimary
408+
defaultLaneViewSecondaryColor = .defaultLaneArrowSecondary
416409

417-
ExitView.appearance(for: carPlayDarkTraitCollection).backgroundColor = .clear
418-
ExitView.appearance(for: carPlayDarkTraitCollection).borderWidth = 1.0
419-
ExitView.appearance(for: carPlayDarkTraitCollection).cornerRadius = 5.0
420-
ExitView.appearance(for: carPlayDarkTraitCollection).foregroundColor = defaultColor
421-
ExitView.appearance(for: carPlayDarkTraitCollection).borderColor = defaultColor
422-
423-
GenericRouteShield.appearance(for: carPlayDarkTraitCollection).backgroundColor = .clear
424-
GenericRouteShield.appearance(for: carPlayDarkTraitCollection).borderWidth = 1.0
425-
GenericRouteShield.appearance(for: carPlayDarkTraitCollection).cornerRadius = 5.0
426-
GenericRouteShield.appearance(for: carPlayDarkTraitCollection).foregroundColor = defaultColor
427-
GenericRouteShield.appearance(for: carPlayDarkTraitCollection).borderColor = defaultColor
428-
case .light, .unspecified:
429-
let defaultColor = UIColor.black
410+
defaultLaneArrowPrimaryHighlightedColor = .defaultLaneArrowPrimaryHighlighted
411+
defaultLaneArrowSecondaryHighlightedColor = .defaultLaneArrowSecondaryHighlighted
412+
case .dark:
413+
defaultInstructionColor = UIColor.white
430414

431-
let carPlayLightTraitCollection = UITraitCollection(traitsFrom: [
432-
carPlayTraitCollection,
433-
UITraitCollection(userInterfaceStyle: .light)
434-
])
415+
defaultLaneViewPrimaryColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
416+
defaultLaneViewSecondaryColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.3)
435417

436-
ExitView.appearance(for: carPlayLightTraitCollection).backgroundColor = .clear
437-
ExitView.appearance(for: carPlayLightTraitCollection).borderWidth = 1.0
438-
ExitView.appearance(for: carPlayLightTraitCollection).cornerRadius = 5.0
439-
ExitView.appearance(for: carPlayLightTraitCollection).foregroundColor = defaultColor
440-
ExitView.appearance(for: carPlayLightTraitCollection).borderColor = defaultColor
441-
442-
GenericRouteShield.appearance(for: carPlayLightTraitCollection).backgroundColor = .clear
443-
GenericRouteShield.appearance(for: carPlayLightTraitCollection).borderWidth = 1.0
444-
GenericRouteShield.appearance(for: carPlayLightTraitCollection).cornerRadius = 5.0
445-
GenericRouteShield.appearance(for: carPlayLightTraitCollection).foregroundColor = defaultColor
446-
GenericRouteShield.appearance(for: carPlayLightTraitCollection).borderColor = defaultColor
418+
defaultLaneArrowPrimaryHighlightedColor = .defaultLaneArrowPrimaryHighlighted
419+
defaultLaneArrowSecondaryHighlightedColor = .defaultLaneArrowSecondaryHighlighted
447420
@unknown default:
448421
fatalError("Unknown userInterfaceStyle.")
449422
}
423+
424+
ExitView.appearance(for: traitCollection).backgroundColor = .clear
425+
ExitView.appearance(for: traitCollection).borderWidth = 1.0
426+
ExitView.appearance(for: traitCollection).cornerRadius = 5.0
427+
ExitView.appearance(for: traitCollection).foregroundColor = defaultInstructionColor
428+
ExitView.appearance(for: traitCollection).borderColor = defaultInstructionColor
429+
430+
GenericRouteShield.appearance(for: traitCollection).backgroundColor = .clear
431+
GenericRouteShield.appearance(for: traitCollection).borderWidth = 1.0
432+
GenericRouteShield.appearance(for: traitCollection).cornerRadius = 5.0
433+
GenericRouteShield.appearance(for: traitCollection).foregroundColor = defaultInstructionColor
434+
GenericRouteShield.appearance(for: traitCollection).borderColor = defaultInstructionColor
435+
436+
LaneView.appearance(for: traitCollection).primaryColor = defaultLaneViewPrimaryColor
437+
LaneView.appearance(for: traitCollection).secondaryColor = defaultLaneViewSecondaryColor
438+
439+
LaneView.appearance(for: traitCollection).primaryColorHighlighted = defaultLaneArrowPrimaryHighlightedColor
440+
LaneView.appearance(for: traitCollection).secondaryColorHighlighted = defaultLaneArrowSecondaryHighlightedColor
450441
}
451442

452-
func setDefaultCarPlayInstructionsStyling() {
443+
func applyDefaultCarPlayStyling() {
453444
let defaultColor = UIColor.black
454445
let carPlayTraitCollection = UITraitCollection(userInterfaceIdiom: .carPlay)
455446

@@ -458,5 +449,11 @@ open class DayStyle: Style {
458449

459450
GenericRouteShield.appearance(for: carPlayTraitCollection).foregroundColor = defaultColor
460451
GenericRouteShield.appearance(for: carPlayTraitCollection).borderColor = defaultColor
452+
453+
LaneView.appearance(for: carPlayTraitCollection).primaryColor = .defaultLaneArrowPrimary
454+
LaneView.appearance(for: carPlayTraitCollection).secondaryColor = .defaultLaneArrowSecondary
455+
456+
LaneView.appearance(for: carPlayTraitCollection).primaryColorHighlighted = .defaultLaneArrowPrimaryHighlighted
457+
LaneView.appearance(for: carPlayTraitCollection).secondaryColorHighlighted = .defaultLaneArrowSecondaryHighlighted
461458
}
462459
}

Sources/MapboxNavigation/LaneView.swift

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,24 +336,37 @@ extension ManeuverDirection {
336336

337337
/// :nodoc:
338338
open class LaneView: UIView {
339+
340+
/**
341+
The direction or directions of travel that the lane is reserved for.
342+
*/
339343
var indications: LaneIndication {
340344
didSet {
341345
setNeedsDisplay()
342346
}
343347
}
344348

349+
/**
350+
Denotes which of the `indications` is applicable to the current route when there is more than one.
351+
*/
345352
var maneuverDirection: ManeuverDirection? {
346353
didSet {
347354
setNeedsDisplay()
348355
}
349356
}
350357

351-
var isValid: Bool = false {
358+
/**
359+
Denotes whether or not the user can use this lane to continue along the current route.
360+
*/
361+
var isUsable: Bool = false {
352362
didSet {
353363
setNeedsDisplay()
354364
}
355365
}
356366

367+
/**
368+
Indicates which side of the road cars and traffic flow.
369+
*/
357370
var drivingSide: DrivingSide = .right {
358371
didSet {
359372
setNeedsDisplay()
@@ -364,38 +377,60 @@ open class LaneView: UIView {
364377
return bounds.size
365378
}
366379

380+
/**
381+
Color of the maneuver direction (applied only when `LaneView.isUsable` is set to `true`). In case if
382+
`LaneView.showHighlightedColors` is set to `true` this value is not used, `LaneView.primaryColorHighlighted`
383+
is used instead.
384+
*/
367385
@objc public dynamic var primaryColor: UIColor = .defaultLaneArrowPrimary {
368386
didSet {
369387
setNeedsDisplay()
370388
}
371389
}
372390

391+
/**
392+
Color of the directions that the lane is reserved for (except the one that is applicable to the
393+
current route). In case if `LaneView.showHighlightedColors` is set to `true` this value is not used,
394+
`LaneView.secondaryColorHighlighted` is used instead.
395+
*/
373396
@objc public dynamic var secondaryColor: UIColor = .defaultLaneArrowSecondary {
374397
didSet {
375398
setNeedsDisplay()
376399
}
377400
}
378-
401+
402+
/**
403+
Highlighted color of the directions that the lane is reserved for (except the one that is
404+
applicable to the current route).
405+
*/
379406
@objc public dynamic var primaryColorHighlighted: UIColor = .defaultLaneArrowPrimaryHighlighted {
380407
didSet {
381408
setNeedsDisplay()
382409
}
383410
}
384-
411+
412+
/**
413+
Highlighted color of the directions that the lane is reserved for (except the one that is applicable
414+
to the current route).
415+
*/
385416
@objc public dynamic var secondaryColorHighlighted: UIColor = .defaultLaneArrowSecondaryHighlighted {
386417
didSet {
387418
setNeedsDisplay()
388419
}
389420
}
390-
421+
422+
/**
423+
Controls whether highighted colors (either `LaneView.primaryColorHighlighted` or
424+
`LaneView.secondaryColorHighlighted`) should be used.
425+
*/
391426
public var showHighlightedColors: Bool = false {
392427
didSet {
393428
setNeedsDisplay()
394429
}
395430
}
396431

397432
var appropriatePrimaryColor: UIColor {
398-
if isValid {
433+
if isUsable {
399434
return showHighlightedColors ? primaryColorHighlighted : primaryColor
400435
} else {
401436
return showHighlightedColors ? secondaryColorHighlighted : secondaryColor
@@ -408,12 +443,16 @@ open class LaneView: UIView {
408443

409444
static let defaultFrame: CGRect = CGRect(origin: .zero, size: 30.0)
410445

411-
convenience init(indications: LaneIndication, isUsable: Bool, direction: ManeuverDirection?) {
446+
convenience init(indications: LaneIndication,
447+
isUsable: Bool,
448+
direction: ManeuverDirection?,
449+
showHighlightedColors: Bool = false) {
412450
self.init(frame: LaneView.defaultFrame)
413451
backgroundColor = .clear
414452
self.indications = indications
415453
maneuverDirection = direction ?? ManeuverDirection(rawValue: indications.description)
416-
isValid = isUsable
454+
self.isUsable = isUsable
455+
self.showHighlightedColors = showHighlightedColors
417456
}
418457

419458
override init(frame: CGRect) {
@@ -422,9 +461,9 @@ open class LaneView: UIView {
422461
commonInit()
423462
}
424463

425-
@objc public required init?(coder aDecoder: NSCoder) {
464+
@objc public required init?(coder decoder: NSCoder) {
426465
indications = []
427-
super.init(coder: aDecoder)
466+
super.init(coder: decoder)
428467
commonInit()
429468
}
430469

@@ -444,7 +483,7 @@ open class LaneView: UIView {
444483
#endif
445484

446485
let resizing = LanesStyleKit.ResizingBehavior.aspectFit
447-
let appropriateColor = isValid ? appropriatePrimaryColor : appropriateSecondaryColor
486+
let appropriateColor = isUsable ? appropriatePrimaryColor : appropriateSecondaryColor
448487
let size = CGSize(width: 32, height: 32)
449488

450489
let isFlipped = indications.dominantSide(maneuverDirection: maneuverDirection, drivingSide: drivingSide) == .left

Sources/MapboxNavigation/LanesView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ open class LanesView: UIView, NavigationComponent {
5959
}
6060
}
6161

62-
guard !subviews.isEmpty && subviews.contains(where: { $0.isValid }) else {
62+
guard !subviews.isEmpty && subviews.contains(where: { $0.isUsable }) else {
6363
hide(animated: animated,
6464
duration: duration) { completed in
6565
completion?(completed)

Sources/MapboxNavigation/NavigationViewController.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,15 +880,19 @@ extension NavigationViewController: NavigationServiceDelegate {
880880
imageColor = .black
881881
}
882882

883-
if let image = instruction.primaryInstruction.maneuverImage(side: instruction.drivingSide, color: imageColor, size: CGSize(width: 72, height: 72)) {
883+
if let image = instruction.primaryInstruction.maneuverImage(drivingSide: instruction.drivingSide,
884+
color: imageColor,
885+
size: CGSize(width: 72, height: 72)) {
884886
// Bake in any transform required for left turn arrows etc.
885887
let imageData = UIGraphicsImageRenderer(size: image.size).pngData { (context) in
886888
image.draw(at: .zero)
887889
}
888890
let temporaryURL = FileManager.default.temporaryDirectory.appendingPathComponent("com.mapbox.navigation.notification-icon.png")
889891
do {
890892
try imageData.write(to: temporaryURL)
891-
let iconAttachment = try UNNotificationAttachment(identifier: "maneuver", url: temporaryURL, options: [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG])
893+
let iconAttachment = try UNNotificationAttachment(identifier: "maneuver",
894+
url: temporaryURL,
895+
options: [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG])
892896
content.attachments = [iconAttachment]
893897
} catch {
894898
Log.error("Failed to create UNNotificationAttachment with error: \(error.localizedDescription).",

Sources/MapboxNavigation/NightStyle.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ open class NightStyle: DayStyle {
169169
// `StylableLabel` is used in `CarPlayCompassView` to show compass direction.
170170
StylableLabel.appearance(for: carPlayTraitCollection, whenContainedInInstancesOf: [CarPlayCompassView.self]).normalTextColor = #colorLiteral(red: 0.9842069745, green: 0.9843751788, blue: 0.9841964841, alpha: 1)
171171

172-
LaneView.appearance(for: carPlayTraitCollection).primaryColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
173-
LaneView.appearance(for: carPlayTraitCollection).secondaryColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.3)
174-
175172
WayNameView.appearance(for: carPlayTraitCollection).borderColor = #colorLiteral(red: 0.2802129388, green: 0.3988235593, blue: 0.5260632038, alpha: 1)
176173
WayNameLabel.appearance(for: carPlayTraitCollection).roadShieldBlackColor = #colorLiteral(red: 0.08, green: 0.09, blue: 0.12, alpha: 1)
177174
WayNameLabel.appearance(for: carPlayTraitCollection).roadShieldBlueColor = #colorLiteral(red: 0.18, green: 0.26, blue: 0.66, alpha: 1)

0 commit comments

Comments
 (0)