Skip to content

Commit

Permalink
fix: Load correct nearby location on launch (#128)
Browse files Browse the repository at this point in the history
When the app was started with location permissions already granted,
nearby transit was instead loading the default camera location, rather
that the phone's current GPS location. The NearbyTransitLocationProvider
was implemented in a silly way where it was effectively immutable, and on
app startup, `currentLocation` and `viewportProvider.viewport` were both
changing and running their `onChange` at the same time, so the current
location was getting overridden by the default camera state.
  • Loading branch information
EmmaSimon authored Apr 11, 2024
1 parent 671b0a8 commit 33976a4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import CoreLocation

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

@Published var location: CLLocationCoordinate2D

Expand All @@ -20,6 +20,52 @@ class NearbyTransitLocationProvider: ObservableObject {
self.cameraLocation = cameraLocation
self.isFollowing = isFollowing

location = if isFollowing, currentLocation != nil { currentLocation! } else { cameraLocation }
location = Self.resolveLocation(currentLocation, cameraLocation, isFollowing)
handleFollowingCamera()
}

static func resolveLocation(
_ currentLocation: CLLocationCoordinate2D?,
_ cameraLocation: CLLocationCoordinate2D,
_ isFollowing: Bool
) -> CLLocationCoordinate2D {
if isFollowing, currentLocation != nil { currentLocation! } else { cameraLocation }
}

func updateCurrentLocation(_ newLocation: CLLocationCoordinate2D?) {
currentLocation = newLocation
handleFollowingCamera()
updateLocation()
}

func updateCameraLocation(_ newLocation: CLLocationCoordinate2D) {
if cameraLocation.isRoughlyEqualTo(newLocation) {
return
}
cameraLocation = newLocation
updateLocation()
}

func updateIsFollowing(_ newFollowing: Bool, withCameraLocation newLocation: CLLocationCoordinate2D? = nil) {
isFollowing = newFollowing
if newLocation != nil {
cameraLocation = newLocation!
}
handleFollowingCamera()
updateLocation()
}

private func updateLocation() {
location = Self.resolveLocation(currentLocation, cameraLocation, isFollowing)
}

/*
Reset camera location to current location when viewport is following.
This prevents loading stale camera locations when the viewport stops following.
*/
private func handleFollowingCamera() {
if isFollowing, currentLocation != nil {
cameraLocation = currentLocation!
}
}
}
40 changes: 13 additions & 27 deletions iosApp/iosApp/Pages/NearbyTransit/NearbyTransitPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftUI
@_spi(Experimental) import MapboxMaps

struct NearbyTransitPageView: View {
let currentLocation: CLLocationCoordinate2D?
var currentLocation: CLLocationCoordinate2D?
@ObservedObject var globalFetcher: GlobalFetcher
@ObservedObject var nearbyFetcher: NearbyFetcher
@ObservedObject var scheduleFetcher: ScheduleFetcher
Expand All @@ -23,7 +23,9 @@ struct NearbyTransitPageView: View {
@ObservedObject var alertsFetcher: AlertsFetcher

@State var cancellables: [AnyCancellable]
@State var locationProvider: NearbyTransitLocationProvider
@StateObject var locationProvider: NearbyTransitLocationProvider

let inspection = Inspection<Self>()

init(
currentLocation: CLLocationCoordinate2D?,
Expand All @@ -43,16 +45,16 @@ struct NearbyTransitPageView: View {
self.alertsFetcher = alertsFetcher

cancellables = .init()
locationProvider = .init(
_locationProvider = StateObject(wrappedValue: .init(
currentLocation: currentLocation,
cameraLocation: viewportProvider.cameraState.center,
isFollowing: viewportProvider.viewport.isFollowing
)
))
}

var body: some View {
NearbyTransitView(
locationProvider: locationProvider,
location: locationProvider.location,
globalFetcher: globalFetcher,
nearbyFetcher: nearbyFetcher,
scheduleFetcher: scheduleFetcher,
Expand All @@ -64,36 +66,20 @@ struct NearbyTransitPageView: View {
viewportProvider.$cameraState
.debounce(for: .seconds(0.5), scheduler: DispatchQueue.main)
.sink { newCameraState in
let shouldUpdateLocation = !viewportProvider.viewport.isFollowing &&
!locationProvider.location.isRoughlyEqualTo(newCameraState.center)
if shouldUpdateLocation {
locationProvider = .init(
currentLocation: currentLocation,
cameraLocation: newCameraState.center,
isFollowing: viewportProvider.viewport.isFollowing
)
}
locationProvider.updateCameraLocation(newCameraState.center)
}
)
}
.onChange(of: currentLocation) { newLocation in
let shouldUpdateLocation = viewportProvider.viewport.isFollowing
&& !locationProvider.location.isRoughlyEqualTo(newLocation)
if shouldUpdateLocation {
locationProvider = .init(
currentLocation: newLocation,
cameraLocation: viewportProvider.cameraState.center,
isFollowing: viewportProvider.viewport.isFollowing
)
}
locationProvider.updateCurrentLocation(newLocation)
}
.onChange(of: viewportProvider.viewport) { newViewport in
locationProvider = .init(
currentLocation: currentLocation,
cameraLocation: viewportProvider.cameraState.center,
isFollowing: newViewport.isFollowing
locationProvider.updateIsFollowing(
newViewport.isFollowing,
withCameraLocation: viewportProvider.cameraState.center
)
}
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.navigationTitle("Nearby Transit")
.navigationBarTitleDisplayMode(.inline)
}
Expand Down
10 changes: 5 additions & 5 deletions iosApp/iosApp/Pages/NearbyTransit/NearbyTransitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SwiftUI

struct NearbyTransitView: View {
@Environment(\.scenePhase) private var scenePhase
@ObservedObject var locationProvider: NearbyTransitLocationProvider
var location: CLLocationCoordinate2D
@ObservedObject var globalFetcher: GlobalFetcher
@ObservedObject var nearbyFetcher: NearbyFetcher
@ObservedObject var scheduleFetcher: ScheduleFetcher
Expand Down Expand Up @@ -44,14 +44,14 @@ struct NearbyTransitView: View {
}
}
.onAppear {
getNearby(location: locationProvider.location)
getNearby(location: location)
joinPredictions()
didAppear?(self)
}
.onChange(of: globalFetcher.response) { _ in
getNearby(location: locationProvider.location)
getNearby(location: location)
}
.onChange(of: locationProvider.location) { newLocation in
.onChange(of: location) { newLocation in
getNearby(location: newLocation)
}
.onChange(of: nearbyFetcher.nearbyByRouteAndStop) { _ in
Expand All @@ -76,7 +76,7 @@ struct NearbyTransitView: View {
}
.replaceWhen(nearbyFetcher.errorText) { errorText in
IconCard(iconName: "network.slash", details: errorText)
.refreshable(nearbyFetcher.loading) { getNearby(location: locationProvider.location) }
.refreshable(nearbyFetcher.loading) { getNearby(location: location) }
}
}

Expand Down
Loading

0 comments on commit 33976a4

Please sign in to comment.