Skip to content

Commit

Permalink
Merge pull request #53 from OneBusAway/fnf
Browse files Browse the repository at this point in the history
Fit and finish pass on TripPlannerView
  • Loading branch information
aaronbrethorst authored Aug 12, 2024
2 parents 4b145d9 + e2ea9e1 commit 2a7c84c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
51 changes: 27 additions & 24 deletions OTPKit/Features/TripPlanner/TripPlannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,41 @@ import SwiftUI
public struct TripPlannerView: View {
@EnvironmentObject private var tripPlanner: TripPlannerService

public init() {}
public init(text: String) {
self.text = text
}

private let text: String

public var body: some View {
VStack {
Button(action: {
tripPlanner.isStepsViewPresented = true
}, label: {
Text("Start")
})
.frame(maxWidth: .infinity)
.padding()
.background(Color.gray)
.foregroundStyle(.foreground)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(.horizontal, 16)
VStack(alignment: .leading, spacing: 0) {
Text(text)
.fontWeight(.semibold)
.padding(16)
HStack {
Button(action: {
tripPlanner.resetTripPlanner()
}, label: {
Text("Cancel")
.frame(maxWidth: .infinity)
})
.buttonStyle(BorderedButtonStyle())

Button(action: {
tripPlanner.resetTripPlanner()
}, label: {
Text("Cancel")
})
.frame(maxWidth: .infinity)
Button(action: {
tripPlanner.isStepsViewPresented = true
}, label: {
Text("Start")
.frame(maxWidth: .infinity)
})
.buttonStyle(BorderedProminentButtonStyle())
}
.padding()
.background(Color.gray)
.foregroundStyle(.foreground)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(.horizontal, 16)
}
.background(.thickMaterial)
}
}

#Preview {
TripPlannerView()
TripPlannerView(text: "43 minutes, departs at 4:15 PM")
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
32 changes: 20 additions & 12 deletions OTPKit/Miscellaneous/Formatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ class Formatters {
static func formatTimeDuration(_ duration: Int) -> String {
if duration < 60 {
return "\(duration) second\(duration > 1 ? "s" : "")"
} else if duration < 3600 {
let minutes = duration / 60
}

let (hours, minutes) = hoursAndMinutesFrom(seconds: duration)

if hours == 0 {
return String(format: "%d min", minutes)
} else {
let hours = Double(duration) / 3600
return String(format: "%.1f hours", hours)
}

return String(format: "%d hr %d min", hours, minutes)
}

static func hoursAndMinutesFrom(seconds: Int) -> (hours: Int, minutes: Int) {
let hours = seconds / 3600
let remainingSeconds = seconds % 3600
let minutes = remainingSeconds / 60
return (hours, minutes)
}

static func formatDistance(_ distance: Int) -> String {
Expand All @@ -30,12 +39,11 @@ class Formatters {
}
}

static func formatDateToTime(_ date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"

return dateFormatter.string(from: date)
static func formatDateToTime(_ date: Date, locale: Locale = .current) -> String {
let formatter = DateFormatter()
formatter.locale = locale
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter.string(from: date)
}
}
8 changes: 8 additions & 0 deletions OTPKit/Models/TripPlanner/Itinerary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public struct Itinerary: Codable, Hashable {

/// Array of `Leg` objects representing individual segments of the itinerary.
public let legs: [Leg]

public var summary: String {
// TODO: localize this!
let time = Formatters.formatDateToTime(startTime)
let formattedDuration = Formatters.formatTimeDuration(duration)
// return something like "43 minutes, departs at X:YY PM"
return "Departs at \(time); duration: \(formattedDuration)"
}
}
5 changes: 2 additions & 3 deletions OTPKitDemo/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ public struct MapView: View {
} else if tripPlanner.isMapMarkingMode {
MapMarkingView()
.environmentObject(tripPlanner)
} else if tripPlanner.selectedItinerary != nil,
tripPlanner.isStepsViewPresented == false {
} else if let selectedItinerary = tripPlanner.selectedItinerary, !tripPlanner.isStepsViewPresented {
VStack {
Spacer()
TripPlannerView()
TripPlannerView(text: selectedItinerary.summary)
.environmentObject(tripPlanner)
}
} else if tripPlanner.planResponse == nil, tripPlanner.isStepsViewPresented == false {
Expand Down

0 comments on commit 2a7c84c

Please sign in to comment.