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

feat: reset nearby transit scroll position #116

Merged
merged 1 commit into from
Apr 12, 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
100 changes: 73 additions & 27 deletions iosApp/iosApp/Pages/NearbyTransit/NearbyTransitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,27 @@ struct NearbyTransitView: View {
@ObservedObject var scheduleFetcher: ScheduleFetcher
@ObservedObject var predictionsFetcher: PredictionsFetcher
@ObservedObject var alertsFetcher: AlertsFetcher
@State var nearbyWithRealtimeInfo: [StopAssociatedRoute]?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (non-blocking): For my own learning, why move this to state?

Copy link
Contributor Author

@BrandonTR BrandonTR Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we were doing this association with every render of the view and if we store the value this way we don't need to re-run the association with realtime info every render, only when one of the onChange/onReceive modifiers triggers it. Just a small optimization

@State var now = Date.now
@State var scrollPosition: String?

let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
let inspection = Inspection<Self>()

var body: some View {
VStack {
if let nearby = nearbyFetcher.withRealtimeInfo(
schedules: scheduleFetcher.schedules,
predictions: predictionsFetcher.predictions,
alerts: alertsFetcher.alerts,
filterAtTime: now.toKotlinInstant()
) {
List(nearby, id: \.route.id) { nearbyRoute in
NearbyRouteView(nearbyRoute: nearbyRoute, now: now.toKotlinInstant())
}.putAboveWhen(predictionsFetcher.errorText) { errorText in
IconCard(iconName: "network.slash", details: errorText)
}
if let nearbyWithRealtimeInfo {
nearbyList(nearbyWithRealtimeInfo)
} else {
Text("Loading...")
.frame(maxWidth: .infinity)
.padding(.top, 24)
}
}
.onAppear {
getNearby(location: location)
joinPredictions()
updateNearbyRoutes()
didAppear?(self)
}
.onChange(of: globalFetcher.response) { _ in
Expand All @@ -55,31 +51,59 @@ struct NearbyTransitView: View {
getNearby(location: newLocation)
}
.onChange(of: nearbyFetcher.nearbyByRouteAndStop) { _ in
updateNearbyRoutes()
getSchedule()
joinPredictions()
scrollToTop()
}
.onChange(of: scheduleFetcher.schedules) { _ in
updateNearbyRoutes()
}
.onChange(of: predictionsFetcher.predictions) { _ in
updateNearbyRoutes()
}
.onChange(of: alertsFetcher.alerts) { _ in
updateNearbyRoutes()
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .inactive {
leavePredictions()
} else if newPhase == .active {
joinPredictions()
} else if newPhase == .background {
leavePredictions()
}
onSceneChange(newPhase)
}
.onReceive(timer) { input in
now = input
updateNearbyRoutes()
}
.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) }
errorCard(errorText)
}
}

private func nearbyList(_ routes: [StopAssociatedRoute]) -> some View {
ScrollViewReader { proxy in
List(routes, id: \.route.id) { nearbyRoute in
NearbyRouteView(nearbyRoute: nearbyRoute, now: now.toKotlinInstant())
}
.onChange(of: scrollPosition) { id in
guard let id else { return }
withAnimation {
proxy.scrollTo(id, anchor: .center)
scrollPosition = nil
}
}
.putAboveWhen(predictionsFetcher.errorText) { errorText in
IconCard(iconName: "network.slash", details: errorText)
}
}
}

private func errorCard(_ errorText: Text) -> some View {
IconCard(iconName: "network.slash", details: errorText)
.refreshable(nearbyFetcher.loading) { getNearby(location: location) }
}

var didAppear: ((Self) -> Void)?

func getNearby(location: CLLocationCoordinate2D) {
Expand All @@ -99,17 +123,39 @@ struct NearbyTransitView: View {
}

func joinPredictions() {
Task {
guard let stopIds = nearbyFetcher.nearbyByRouteAndStop?
.stopIds() else { return }
let stopIdList = Array(stopIds)
predictionsFetcher.run(stopIds: stopIdList)
}
guard let stopIds = nearbyFetcher.nearbyByRouteAndStop?
.stopIds() else { return }
let stopIdList = Array(stopIds)
predictionsFetcher.run(stopIds: stopIdList)
}

func leavePredictions() {
predictionsFetcher.leave()
}

private func scrollToTop() {
guard let id = nearbyWithRealtimeInfo?.first?.route.id else { return }
scrollPosition = id
}

private func updateNearbyRoutes() {
nearbyWithRealtimeInfo = nearbyFetcher.withRealtimeInfo(
schedules: scheduleFetcher.schedules,
predictions: predictionsFetcher.predictions,
alerts: alertsFetcher.alerts,
filterAtTime: now.toKotlinInstant()
)
}

private func onSceneChange(_ phase: ScenePhase) {
Task {
predictionsFetcher.leave()
if phase == .inactive {
leavePredictions()
} else if phase == .active {
joinPredictions()
} else if phase == .background {
leavePredictions()
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions iosApp/iosApp/Utils/Backport/PartialSheetModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private extension PartialSheetRepresentable {
controller.animateChanges {
controller.detents = detents.map(\.uiKitDetent)
controller.prefersScrollingExpandsWhenScrolledToEdge = true
controller.prefersGrabberVisible = true

if let largestUndimmedDetent {
controller.largestUndimmedDetentIdentifier = .init(largestUndimmedDetent.rawValue)
Expand Down
Loading