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

[ios] pull down InstructionView handle to show remaining steps #276

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apple/Sources/FerrostarCore/NavigationState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public struct NavigationState: Hashable {
return visualInstruction
}

public var remainingSteps: [RouteStep]? {
guard case let .navigating(_, _, remainingSteps: remainingSteps, _, _, _, _, _, _) = tripState else {
return nil
}

return remainingSteps
}

public var currentAnnotationJSON: String? {
guard case let .navigating(_, _, _, _, _, _, _, _, annotationJson: annotationJson) = tripState else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct LandscapeNavigationOverlayView: View, CustomizableNavigatingInnerGridView

private var navigationState: NavigationState?

@State private var isInstructionViewExpanded: Bool = false
// TODO: Same as portrait?
ianthetechie marked this conversation as resolved.
Show resolved Hide resolved

var topCenter: (() -> AnyView)?
var topTrailing: (() -> AnyView)?
var midLeading: (() -> AnyView)?
Expand Down Expand Up @@ -46,26 +49,29 @@ struct LandscapeNavigationOverlayView: View, CustomizableNavigatingInnerGridView

var body: some View {
HStack {
VStack {
ZStack(alignment: .top) {
VStack {
Spacer()
if case .navigating = navigationState?.tripState,
let progress = navigationState?.currentProgress
{
ArrivalView(
progress: progress,
onTapExit: onTapExit
)
}
}
if case .navigating = navigationState?.tripState,
let visualInstruction = navigationState?.currentVisualInstruction,
let progress = navigationState?.currentProgress
let progress = navigationState?.currentProgress,
let remainingSteps = navigationState?.remainingSteps
{
InstructionsView(
visualInstruction: visualInstruction,
distanceFormatter: formatterCollection.distanceFormatter,
distanceToNextManeuver: progress.distanceToNextManeuver
)
}

Spacer()

if case .navigating = navigationState?.tripState,
let progress = navigationState?.currentProgress
{
ArrivalView(
progress: progress,
onTapExit: onTapExit
distanceToNextManeuver: progress.distanceToNextManeuver,
remainingSteps: remainingSteps,
isExpanded: $isInstructionViewExpanded
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct PortraitNavigationOverlayView: View, CustomizableNavigatingInnerGridView

private var navigationState: NavigationState?

@State private var isInstructionViewExpanded: Bool = false
@State private var instructionsViewSizeWhenNotExpanded: CGSize = .zero

var topCenter: (() -> AnyView)?
var topTrailing: (() -> AnyView)?
var midLeading: (() -> AnyView)?
Expand Down Expand Up @@ -45,46 +48,54 @@ struct PortraitNavigationOverlayView: View, CustomizableNavigatingInnerGridView
}

var body: some View {
VStack {
ZStack(alignment: .top) {
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 layout changes here are non-trivial.

We want to be able to expand the instruction view over top of the other content when it's expanded. But when it's not expanded, we want it to behave as if it's in a vstack with the NavigationInnerGridView.

My solution was to put the InstructionView in a ZStack above the grid, and then plumb out the InstructionView's "unexpanded height" to push down the NavigatingInnerGridView.

I struggled a bit with implementing this layout in SwiftUI. It's something I think would be natural to express in old school iOS layout constraints. Maybe there's a better way?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'm not aware of a better way offhand either. It's one of the areas where SwiftUI and others struggle compared to springs-and-struts or auto layout.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are a few conflicts unfortunately with the main branch since @Archdoog just cleaned up a bunch of stuff to clean up the pattern matching mess 😅

VStack {
Spacer()

// The inner content is displayed vertically full screen
// when both the visualInstructions and progress are nil.
// It will automatically reduce height if and when either
// view appears
NavigatingInnerGridView(
speedLimit: speedLimit,
showZoom: showZoom,
onZoomIn: onZoomIn,
onZoomOut: onZoomOut,
showCentering: showCentering,
onCenter: onCenter
)
.innerGrid {
topCenter?()
} topTrailing: {
topTrailing?()
} midLeading: {
midLeading?()
} bottomTrailing: {
bottomTrailing?()
}

if case .navigating = navigationState?.tripState,
let progress = navigationState?.currentProgress
{
ArrivalView(
progress: progress,
onTapExit: onTapExit
)
}
}.padding(.top, instructionsViewSizeWhenNotExpanded.height)

if case .navigating = navigationState?.tripState,
let visualInstruction = navigationState?.currentVisualInstruction,
let progress = navigationState?.currentProgress
let progress = navigationState?.currentProgress,
let remainingSteps = navigationState?.remainingSteps
{
InstructionsView(
visualInstruction: visualInstruction,
distanceFormatter: formatterCollection.distanceFormatter,
distanceToNextManeuver: progress.distanceToNextManeuver
)
}

// The inner content is displayed vertically full screen
// when both the visualInstructions and progress are nil.
// It will automatically reduce height if and when either
// view appears
NavigatingInnerGridView(
speedLimit: speedLimit,
showZoom: showZoom,
onZoomIn: onZoomIn,
onZoomOut: onZoomOut,
showCentering: showCentering,
onCenter: onCenter
)
.innerGrid {
topCenter?()
} topTrailing: {
topTrailing?()
} midLeading: {
midLeading?()
} bottomTrailing: {
bottomTrailing?()
}

if case .navigating = navigationState?.tripState,
let progress = navigationState?.currentProgress
{
ArrivalView(
progress: progress,
onTapExit: onTapExit
distanceToNextManeuver: progress.distanceToNextManeuver,
remainingSteps: remainingSteps,
isExpanded: $isInstructionViewExpanded,
sizeWhenNotExpanded: $instructionsViewSizeWhenNotExpanded
)
}
}
Expand Down
95 changes: 95 additions & 0 deletions apple/Sources/FerrostarSwiftUI/TestFixtureFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/// Various helpers that generate views for previews.

import FerrostarCoreFFI
import Foundation

protocol TestFixtureFactory {
associatedtype Output
func build(_ n: Int) -> Output
}

extension TestFixtureFactory {
func buildMany(_ n: Int) -> [Output] {
(0 ... n).map { build($0) }
}
}

struct VisualInstructionContentFactory: TestFixtureFactory {
public init() {}

public var textBuilder: (Int) -> String = { n in RoadNameFactory().build(n) }
public func text(_ builder: @escaping (Int) -> String) -> Self {
var copy = self
copy.textBuilder = builder
return copy
}

public func build(_ n: Int = 0) -> VisualInstructionContent {
VisualInstructionContent(
text: textBuilder(n),
maneuverType: .turn,
maneuverModifier: .left,
roundaboutExitDegrees: nil
)
}
}

struct VisualInstructionFactory: TestFixtureFactory {
public init() {}

public var primaryContentBuilder: (Int) -> VisualInstructionContent = { n in
VisualInstructionContentFactory().build(n)
}

public var secondaryContentBuilder: (Int) -> VisualInstructionContent? = { _ in nil }

public func secondaryContent(_ builder: @escaping (Int) -> VisualInstructionContent) -> Self {
var copy = self
copy.secondaryContentBuilder = builder
return copy
}

public func build(_ n: Int = 0) -> VisualInstruction {
VisualInstruction(
primaryContent: primaryContentBuilder(n),
secondaryContent: secondaryContentBuilder(n),
triggerDistanceBeforeManeuver: 42.0
)
}
}

struct RouteStepFactory: TestFixtureFactory {
public init() {}
public var visualInstructionBuilder: (Int) -> VisualInstruction = { n in VisualInstructionFactory().build(n) }
public var roadNameBuilder: (Int) -> String = { n in RoadNameFactory().build(n) }

public func build(_ n: Int = 0) -> RouteStep {
RouteStep(
geometry: [],
distance: 100,
duration: 99,
roadName: roadNameBuilder(n),
instruction: "Walk west on \(roadNameBuilder(n))",
visualInstructions: [visualInstructionBuilder(n)],
spokenInstructions: [],
annotations: nil
)
}
}

struct RoadNameFactory: TestFixtureFactory {
public init() {}
public var baseNameBuilder: (Int) -> String = { _ in "Ave" }

public func baseName(_ builder: @escaping (Int) -> String) -> Self {
var copy = self
copy.baseNameBuilder = builder
return copy
}

public func build(_ n: Int = 0) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .ordinal
return "\(numberFormatter.string(from: NSNumber(value: n + 1))!) \(baseNameBuilder(n))"
}
}
Loading
Loading