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

fix: Load correct nearby location on launch #128

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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