Skip to content

Commit

Permalink
test: Add tests for nearby transit relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaSimon committed Mar 22, 2024
1 parent 5ec3aed commit 6b12ac1
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 113 deletions.
4 changes: 4 additions & 0 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
9AC4FDF12BACE216004479BF /* NearbyTransitPageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AC4FDF02BACE216004479BF /* NearbyTransitPageView.swift */; };
9ACA9DF32BA1EC8B003F0E9E /* HomeMapViewUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ACA9DF22BA1EC8B003F0E9E /* HomeMapViewUITests.swift */; };
9AD1D1FE2BA4D5C600182060 /* ViewportProviderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AD1D1FD2BA4D5C600182060 /* ViewportProviderTest.swift */; };
9ADB849D2BAD05BC006581CE /* Inspection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ADB849C2BAD05BC006581CE /* Inspection.swift */; };
9AF88E052B48913C00E08C7C /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 9AF88E042B48913C00E08C7C /* Localizable.xcstrings */; };
A430D45FE0676C73075AB85B /* Pods_iosAppTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ED1649D982654BA7A4D2F2DC /* Pods_iosAppTests.framework */; };
A55C5596CDC797ED68F79279 /* Pods_iosApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1C6BD892027AC258EE8F408D /* Pods_iosApp.framework */; };
Expand Down Expand Up @@ -171,6 +172,7 @@
9AC4FDF02BACE216004479BF /* NearbyTransitPageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NearbyTransitPageView.swift; sourceTree = "<group>"; };
9ACA9DF22BA1EC8B003F0E9E /* HomeMapViewUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeMapViewUITests.swift; sourceTree = "<group>"; };
9AD1D1FD2BA4D5C600182060 /* ViewportProviderTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewportProviderTest.swift; sourceTree = "<group>"; };
9ADB849C2BAD05BC006581CE /* Inspection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Inspection.swift; sourceTree = "<group>"; };
9AF88E042B48913C00E08C7C /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
ED1649D982654BA7A4D2F2DC /* Pods_iosAppTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iosAppTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F71252D2B68FF131F8E6BDE2 /* Pods-iosAppTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosAppTests.release.xcconfig"; path = "Target Support Files/Pods-iosAppTests/Pods-iosAppTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -432,6 +434,7 @@
9A03F3652BA9E68500DA40DC /* Debouncer.swift */,
9A37F3042BACCC40001714FE /* DoubleRoundedExtension.swift */,
9A37F3062BACCCA5001714FE /* CoordinateExtension.swift */,
9ADB849C2BAD05BC006581CE /* Inspection.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -745,6 +748,7 @@
8CEA10232BA0F3C6001C6EB9 /* ScheduleFetcher.swift in Sources */,
6E99CBB72B9892C80047E78D /* SocketProvider.swift in Sources */,
9A6DDF912B976FDF004D141A /* EmptyWhenModifier.swift in Sources */,
9ADB849D2BAD05BC006581CE /* Inspection.swift in Sources */,
6EE7457E2B965ADE0052227E /* Socket.swift in Sources */,
9A03F3662BA9E68500DA40DC /* Debouncer.swift in Sources */,
9A8B34AD2B88E5090018412C /* RailRouteShapeFetcher.swift in Sources */,
Expand Down
5 changes: 2 additions & 3 deletions iosApp/iosApp/Fetchers/NearbyFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ class NearbyFetcher: ObservableObject {
predictions: PredictionsStreamDataResponse?,
filterAtTime: Instant
) -> [StopAssociatedRoute]? {
let lat = loadedLocation?.latitude ?? 0.0
let lon = loadedLocation?.longitude ?? 0.0
guard let loadedLocation else { return nil }
return nearbyByRouteAndStop?.withRealtimeInfo(
sortByDistanceFrom: .init(longitude: lon, latitude: lat),
sortByDistanceFrom: .init(longitude: loadedLocation.longitude, latitude: loadedLocation.latitude),
schedules: schedules,
predictions: predictions,
filterAtTime: filterAtTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

import CoreLocation

struct NearbyTransitLocationProvider {
class NearbyTransitLocationProvider: ObservableObject {
let currentLocation: CLLocationCoordinate2D?
let cameraLocation: CLLocationCoordinate2D
let isFollowing: Bool

var location: CLLocationCoordinate2D {
if isFollowing, currentLocation != nil { currentLocation! } else { cameraLocation }
}
@Published var location: CLLocationCoordinate2D

init(currentLocation: CLLocationCoordinate2D? = nil, cameraLocation: CLLocationCoordinate2D, isFollowing: Bool) {
self.currentLocation = currentLocation
self.cameraLocation = cameraLocation
self.isFollowing = isFollowing

location = if isFollowing, currentLocation != nil { currentLocation! } else { cameraLocation }
}
}
5 changes: 3 additions & 2 deletions iosApp/iosApp/Pages/NearbyTransit/NearbyTransitPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct NearbyTransitPageView: View {

var body: some View {
NearbyTransitView(
location: locationProvider.location,
locationProvider: locationProvider,
nearbyFetcher: nearbyFetcher,
scheduleFetcher: scheduleFetcher,
predictionsFetcher: predictionsFetcher
Expand All @@ -69,7 +69,8 @@ struct NearbyTransitPageView: View {
)
}
.onChange(of: currentLocation) { newLocation in
let shouldUpdateLocation = viewportProvider.viewport.isFollowing && !locationProvider.location.isRoughlyEqualTo(newLocation)
let shouldUpdateLocation = viewportProvider.viewport.isFollowing
&& !locationProvider.location.isRoughlyEqualTo(newLocation)
if shouldUpdateLocation {
locationProvider = .init(
currentLocation: newLocation,
Expand Down
22 changes: 6 additions & 16 deletions iosApp/iosApp/Pages/NearbyTransit/NearbyTransitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,14 @@ import SwiftUI

struct NearbyTransitView: View {
@Environment(\.scenePhase) private var scenePhase
let location: CLLocationCoordinate2D
@ObservedObject var locationProvider: NearbyTransitLocationProvider
@ObservedObject var nearbyFetcher: NearbyFetcher
@ObservedObject var scheduleFetcher: ScheduleFetcher
@ObservedObject var predictionsFetcher: PredictionsFetcher
@State var now = Date.now

let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()

init(
location: CLLocationCoordinate2D,
nearbyFetcher: NearbyFetcher,
scheduleFetcher: ScheduleFetcher,
predictionsFetcher: PredictionsFetcher
) {
self.location = location
self.nearbyFetcher = nearbyFetcher
self.scheduleFetcher = scheduleFetcher
self.predictionsFetcher = predictionsFetcher
}
let inspection = Inspection<Self>()

var body: some View {
VStack {
Expand All @@ -52,11 +41,11 @@ struct NearbyTransitView: View {
}
}
.onAppear {
getNearby(location: location)
getNearby(location: locationProvider.location)
joinPredictions()
didAppear?(self)
}
.onChange(of: location) { newLocation in
.onChange(of: locationProvider.location) { newLocation in
getNearby(location: newLocation)
}
.onChange(of: nearbyFetcher.nearbyByRouteAndStop) { _ in
Expand All @@ -75,12 +64,13 @@ struct NearbyTransitView: View {
.onReceive(timer) { input in
now = input
}
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.onDisappear {
leavePredictions()
}
.replaceWhen(nearbyFetcher.errorText) { errorText in
IconCard(iconName: "network.slash", details: errorText)
.refreshable(nearbyFetcher.loading) { getNearby(location: location) }
.refreshable(nearbyFetcher.loading) { getNearby(location: locationProvider.location) }
}
}

Expand Down
24 changes: 24 additions & 0 deletions iosApp/iosApp/Utils/Inspection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Inspection.swift
// iosApp
//
// This is used with ViewInspector as recommended in their docs.
// See https://github.com/nalexn/ViewInspector/blob/0.9.11/guide.md#approach-2
//
// Created by Simon, Emma on 3/21/24.
// Copyright © 2024 MBTA. All rights reserved.
//

import Combine
import SwiftUI

final class Inspection<V> {
let notice = PassthroughSubject<UInt, Never>()
var callbacks = [UInt: (V) -> Void]()

func visit(_ view: V, _ line: UInt) {
if let callback = callbacks.removeValue(forKey: line) {
callback(view)
}
}
}
Loading

0 comments on commit 6b12ac1

Please sign in to comment.