Skip to content

Commit

Permalink
Merge pull request #50 from OneBusAway/integration
Browse files Browse the repository at this point in the history
Integration Groundwork
  • Loading branch information
aaronbrethorst authored Aug 11, 2024
2 parents c952a9c + 63f3e46 commit 4b145d9
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 88 deletions.
15 changes: 8 additions & 7 deletions OTPKit/Features/MapExtension/MapMarkingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

public struct MapMarkingView: View {
@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService

public init() {}
public var body: some View {
Expand All @@ -22,9 +22,9 @@ public struct MapMarkingView: View {

HStack(spacing: 16) {
Button {
locationManagerService.toggleMapMarkingMode(false)
locationManagerService.selectAndRefreshCoordinate()
locationManagerService.removeOriginDestinationData()
tripPlanner.toggleMapMarkingMode(false)
tripPlanner.selectAndRefreshCoordinate()
tripPlanner.removeOriginDestinationData()
} label: {
Text("Cancel")
.padding(8)
Expand All @@ -33,9 +33,9 @@ public struct MapMarkingView: View {
.buttonStyle(.bordered)

Button {
locationManagerService.toggleMapMarkingMode(false)
locationManagerService.addOriginDestinationData()
locationManagerService.selectAndRefreshCoordinate()
tripPlanner.toggleMapMarkingMode(false)
tripPlanner.addOriginDestinationData()
tripPlanner.selectAndRefreshCoordinate()
} label: {
Text("Add Pin")
.padding(8)
Expand All @@ -52,4 +52,5 @@ public struct MapMarkingView: View {

#Preview {
MapMarkingView()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
11 changes: 6 additions & 5 deletions OTPKit/Features/OriginDestination/OriginDestinationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftUI
/// It consists a list of Origin and Destination along with the `MapKit`
public struct OriginDestinationView: View {
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService
@State private var isSheetOpened = false

// Public Initializer
Expand All @@ -23,7 +23,7 @@ public struct OriginDestinationView: View {
List {
Button(action: {
sheetEnvironment.isSheetOpened.toggle()
locationManagerService.originDestinationState = .origin
tripPlanner.originDestinationState = .origin
}, label: {
HStack(spacing: 16) {
Image(systemName: "paperplane.fill")
Expand All @@ -32,14 +32,14 @@ public struct OriginDestinationView: View {
.fill(Color.green)
.frame(width: 30, height: 30)
)
Text(locationManagerService.originName)
Text(tripPlanner.originName)
}
})
.foregroundStyle(.foreground)

Button(action: {
sheetEnvironment.isSheetOpened.toggle()
locationManagerService.originDestinationState = .destination
tripPlanner.originDestinationState = .destination
}, label: {
HStack(spacing: 16) {
Image(systemName: "mappin")
Expand All @@ -48,7 +48,7 @@ public struct OriginDestinationView: View {
.fill(Color.green)
.frame(width: 30, height: 30)
)
Text(locationManagerService.destinationName)
Text(tripPlanner.destinationName)
}
})
.foregroundStyle(.foreground)
Expand All @@ -63,4 +63,5 @@ public struct OriginDestinationView: View {

#Preview {
OriginDestinationView()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import SwiftUI
public struct AddFavoriteLocationsSheet: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment

@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService

@State private var search = ""

@FocusState private var isSearchFocused: Bool

private var filteredCompletions: [Location] {
let favorites = sheetEnvironment.favoriteLocations
return locationManagerService.completions.filter { completion in
return tripPlanner.completions.filter { completion in
!favorites.contains { favorite in
favorite.title == completion.title &&
favorite.subTitle == completion.subTitle
Expand All @@ -30,7 +29,7 @@ public struct AddFavoriteLocationsSheet: View {
}

private func currentUserSection() -> some View {
if search.isEmpty, let userLocation = locationManagerService.currentLocation {
if search.isEmpty, let userLocation = tripPlanner.currentLocation {
AnyView(
Button(action: {
switch UserDefaultsServices.shared.saveFavoriteLocationData(data: userLocation) {
Expand Down Expand Up @@ -101,12 +100,13 @@ public struct AddFavoriteLocationsSheet: View {
searchedResultsSection()
}
.onChange(of: search) { _, searchValue in
locationManagerService.updateQuery(queryFragment: searchValue)
tripPlanner.updateQuery(queryFragment: searchValue)
}
}
}
}

#Preview {
AddFavoriteLocationsSheet()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import SwiftUI
/// Show all the lists of favorite locations
public struct MoreFavoriteLocationsSheet: View {
@Environment(\.dismiss) private var dismiss

@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService

@State private var isDetailSheetOpened = false

Expand Down Expand Up @@ -45,4 +44,5 @@ public struct MoreFavoriteLocationsSheet: View {

#Preview {
MoreFavoriteLocationsSheet()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import SwiftUI
public struct OriginDestinationSheetView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var sheetEnvironment: OriginDestinationSheetEnvironment

@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService

@State private var search: String = ""

Expand Down Expand Up @@ -72,8 +71,8 @@ public struct OriginDestinationSheetView: View {
HStack {
ForEach(sheetEnvironment.favoriteLocations, content: { location in
FavoriteView(title: location.title, imageName: "mappin", tapAction: {
locationManagerService.appendMarker(location: location)
locationManagerService.addOriginDestinationData()
tripPlanner.appendMarker(location: location)
tripPlanner.addOriginDestinationData()
dismiss()
}, longTapAction: {
isShowFavoriteConfirmationDialog = true
Expand Down Expand Up @@ -102,8 +101,8 @@ public struct OriginDestinationSheetView: View {
Section(content: {
ForEach(Array(sheetEnvironment.recentLocations.prefix(5)), content: { location in
Button {
locationManagerService.appendMarker(location: location)
locationManagerService.addOriginDestinationData()
tripPlanner.appendMarker(location: location)
tripPlanner.addOriginDestinationData()
dismiss()
} label: {
VStack(alignment: .leading) {
Expand All @@ -124,10 +123,10 @@ public struct OriginDestinationSheetView: View {

private func searchResultsSection() -> some View {
Group {
ForEach(locationManagerService.completions) { location in
ForEach(tripPlanner.completions) { location in
Button(action: {
locationManagerService.appendMarker(location: location)
locationManagerService.addOriginDestinationData()
tripPlanner.appendMarker(location: location)
tripPlanner.addOriginDestinationData()
switch UserDefaultsServices.shared.saveRecentLocations(data: location) {
case .success:
dismiss()
Expand All @@ -149,10 +148,10 @@ public struct OriginDestinationSheetView: View {

private func currentUserSection() -> some View {
Group {
if let userLocation = locationManagerService.currentLocation {
if let userLocation = tripPlanner.currentLocation {
Button(action: {
locationManagerService.appendMarker(location: userLocation)
locationManagerService.addOriginDestinationData()
tripPlanner.appendMarker(location: userLocation)
tripPlanner.addOriginDestinationData()
switch UserDefaultsServices.shared.saveRecentLocations(data: userLocation) {
case .success:
dismiss()
Expand All @@ -176,7 +175,7 @@ public struct OriginDestinationSheetView: View {

private func selectLocationBasedOnMap() -> some View {
Button(action: {
locationManagerService.toggleMapMarkingMode(true)
tripPlanner.toggleMapMarkingMode(true)
dismiss()
}, label: {
HStack {
Expand Down Expand Up @@ -209,7 +208,7 @@ public struct OriginDestinationSheetView: View {
}
}
.onChange(of: search) { _, searchValue in
locationManagerService.updateQuery(queryFragment: searchValue)
tripPlanner.updateQuery(queryFragment: searchValue)
}

Spacer()
Expand All @@ -223,9 +222,11 @@ public struct OriginDestinationSheetView: View {
case .addFavoriteSheet:
AddFavoriteLocationsSheet()
.environmentObject(sheetEnvironment)
.environmentObject(tripPlanner)
case .moreFavoritesSheet:
MoreFavoriteLocationsSheet()
.environmentObject(sheetEnvironment)
.environmentObject(tripPlanner)
case .favoriteDetailsSheet:
FavoriteLocationDetailSheet()
.environmentObject(sheetEnvironment)
Expand All @@ -248,4 +249,5 @@ public struct OriginDestinationSheetView: View {
#Preview {
OriginDestinationSheetView()
.environmentObject(OriginDestinationSheetEnvironment())
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
19 changes: 10 additions & 9 deletions OTPKit/Features/TripPlanner/Direction/DirectionSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MapKit
import SwiftUI

public struct DirectionSheetView: View {
@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(\.dismiss) private var dismiss
@Binding var sheetDetent: PresentationDetent
@State private var scrollToItem: String?
Expand All @@ -27,7 +27,7 @@ public struct DirectionSheetView: View {
private func handleTap(coordinate: CLLocationCoordinate2D, itemId: String) {
let placemark = MKPlacemark(coordinate: coordinate)
let item = MKMapItem(placemark: placemark)
locationManagerService.changeMapCamera(item)
tripPlanner.changeMapCamera(item)
scrollToItem = itemId
sheetDetent = .fraction(0.2)
}
Expand All @@ -36,15 +36,15 @@ public struct DirectionSheetView: View {
ScrollViewReader { proxy in
List {
Section {
PageHeaderView(text: "\(locationManagerService.destinationName)") {
locationManagerService.resetTripPlanner()
PageHeaderView(text: "\(tripPlanner.destinationName)") {
tripPlanner.resetTripPlanner()
dismiss()
}
.frame(height: 50)
.listRowInsets(EdgeInsets())
}

if let itinerary = locationManagerService.selectedItinerary {
if let itinerary = tripPlanner.selectedItinerary {
Section {
createOriginView(itinerary: itinerary)
createLegsView(itinerary: itinerary)
Expand All @@ -71,11 +71,11 @@ public struct DirectionSheetView: View {
private func createOriginView(itinerary _: Itinerary) -> some View {
DirectionLegOriginDestinationView(
title: "Origin",
description: locationManagerService.originName
description: tripPlanner.originName
)
.id("item-0")
.onTapGesture {
if let originCoordinate = locationManagerService.originCoordinate {
if let originCoordinate = tripPlanner.originCoordinate {
handleTap(coordinate: originCoordinate, itemId: "item-0")
}
}
Expand All @@ -95,11 +95,11 @@ public struct DirectionSheetView: View {
private func createDestinationView(itinerary: Itinerary) -> some View {
DirectionLegOriginDestinationView(
title: "Destination",
description: locationManagerService.destinationName
description: tripPlanner.destinationName
)
.id("item-\(itinerary.legs.count + 1)")
.onTapGesture {
if let destinationCoordinate = locationManagerService.destinationCoordinate {
if let destinationCoordinate = tripPlanner.destinationCoordinate {
handleTap(coordinate: destinationCoordinate, itemId: "item-\(itinerary.legs.count + 1)")
}
}
Expand All @@ -108,4 +108,5 @@ public struct DirectionSheetView: View {

#Preview {
DirectionSheetView(sheetDetent: .constant(.fraction(0.2)))
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

public struct TripPlannerSheetView: View {
@ObservedObject private var locationManagerService = LocationManagerService.shared
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(\.dismiss) var dismiss

public init() {}
Expand All @@ -28,11 +28,11 @@ public struct TripPlannerSheetView: View {

public var body: some View {
VStack {
if let itineraries = locationManagerService.planResponse?.plan?.itineraries {
if let itineraries = tripPlanner.planResponse?.plan?.itineraries {
List(itineraries, id: \.self) { itinerary in
Button(action: {
locationManagerService.selectedItinerary = itinerary
locationManagerService.planResponse = nil
tripPlanner.selectedItinerary = itinerary
tripPlanner.planResponse = nil
dismiss()
}, label: {
HStack(spacing: 20) {
Expand Down Expand Up @@ -60,9 +60,9 @@ public struct TripPlannerSheetView: View {
}

Button(action: {
locationManagerService.selectedItinerary = itinerary
locationManagerService.planResponse = nil
locationManagerService.adjustOriginDestinationCamera()
tripPlanner.selectedItinerary = itinerary
tripPlanner.planResponse = nil
tripPlanner.adjustOriginDestinationCamera()
dismiss()
}, label: {
Text("Preview")
Expand All @@ -82,7 +82,7 @@ public struct TripPlannerSheetView: View {
}

Button(action: {
locationManagerService.resetTripPlanner()
tripPlanner.resetTripPlanner()
dismiss()
}, label: {
Text("Cancel")
Expand All @@ -99,4 +99,5 @@ public struct TripPlannerSheetView: View {

#Preview {
TripPlannerSheetView()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Loading

0 comments on commit 4b145d9

Please sign in to comment.