Skip to content

Commit

Permalink
refactor(iOS): Move stop details fetching into VM
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaSimon committed Dec 18, 2024
1 parent 1c94685 commit 9582f34
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 306 deletions.
4 changes: 0 additions & 4 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
9A2A6B6E2D07F7EB00E39AF5 /* StopDetailsViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A2A6B6D2D07F7EB00E39AF5 /* StopDetailsViewModelTests.swift */; };
9A2BCBDE2CED365200FB2913 /* StopDetailsPageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A2BCBDD2CED365200FB2913 /* StopDetailsPageTests.swift */; };
9A2BCBE02CED366300FB2913 /* StopDetailsViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A2BCBDF2CED366300FB2913 /* StopDetailsViewTests.swift */; };
9A2BCBE22CEE8A9F00FB2913 /* StopDetailsPageHandlerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A2BCBE12CEE8A9F00FB2913 /* StopDetailsPageHandlerExtension.swift */; };
9A320F962CD3E4CF0096D7B1 /* UpcomingTripAccessibilityFormatters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A320F952CD3E4CF0096D7B1 /* UpcomingTripAccessibilityFormatters.swift */; };
9A37F3052BACCC40001714FE /* DoubleRoundedExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A37F3042BACCC40001714FE /* DoubleRoundedExtension.swift */; };
9A37F3072BACCCA5001714FE /* CoordinateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A37F3062BACCCA5001714FE /* CoordinateExtension.swift */; };
Expand Down Expand Up @@ -423,7 +422,6 @@
9A2A6B6D2D07F7EB00E39AF5 /* StopDetailsViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsViewModelTests.swift; sourceTree = "<group>"; };
9A2BCBDD2CED365200FB2913 /* StopDetailsPageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsPageTests.swift; sourceTree = "<group>"; };
9A2BCBDF2CED366300FB2913 /* StopDetailsViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsViewTests.swift; sourceTree = "<group>"; };
9A2BCBE12CEE8A9F00FB2913 /* StopDetailsPageHandlerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopDetailsPageHandlerExtension.swift; sourceTree = "<group>"; };
9A320F952CD3E4CF0096D7B1 /* UpcomingTripAccessibilityFormatters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpcomingTripAccessibilityFormatters.swift; sourceTree = "<group>"; };
9A37F3042BACCC40001714FE /* DoubleRoundedExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoubleRoundedExtension.swift; sourceTree = "<group>"; };
9A37F3062BACCCA5001714FE /* CoordinateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoordinateExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1125,7 +1123,6 @@
9AF29DF52CF54319005AA4A3 /* DepartureTile.swift */,
9AF093772BD943A4001DF39F /* DirectionPicker.swift */,
9ACE4FCF2CE6707900FEB006 /* StopDetailsPage.swift */,
9A2BCBE12CEE8A9F00FB2913 /* StopDetailsPageHandlerExtension.swift */,
9AF29E052CFE2C4C005AA4A3 /* StopDetailsFilteredDepartureDetails.swift */,
9AF29DFF2CF63330005AA4A3 /* StopDetailsFilteredHeader.swift */,
9AF29DF72CF5454E005AA4A3 /* StopDetailsFilteredView.swift */,
Expand Down Expand Up @@ -1583,7 +1580,6 @@
6E2027902BD989AC0037554F /* ProductionAppView.swift in Sources */,
6E04D4302C1A17340055FD99 /* StopDeparturesSummaryList.swift in Sources */,
9A74A2112BE2D71400E57102 /* AnnotationLabel.swift in Sources */,
9A2BCBE22CEE8A9F00FB2913 /* StopDetailsPageHandlerExtension.swift in Sources */,
6EEF219E2BF2927E0023A3E9 /* VehicleCardView.swift in Sources */,
9A9E7DCF2C2200C9000DA1FD /* LineHeader.swift in Sources */,
8C05C5812CD568DE000381E8 /* MoreNavLink.swift in Sources */,
Expand Down
14 changes: 8 additions & 6 deletions iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,18 @@ struct ContentView: View {
viewportProvider: viewportProvider
)
.toolbar(.hidden, for: .tabBar)
.onAppear {
let filtered = stopFilter != nil
screenTracker.track(
screen: filtered ? .stopDetailsFiltered : .stopDetailsUnfiltered
)
}
}
// Set id per stop so that transitioning from one stop to another is handled by removing
// the existing stop view & creating a new one
.id(stopId)
.onChange(of: stopId) { nextStopId in stopDetailsVM.handleStopChange(nextStopId) }
.onAppear {
stopDetailsVM.handleStopAppear(stopId)
screenTracker.track(
screen: stopFilter != nil ? .stopDetailsFiltered : .stopDetailsUnfiltered
)
}
.onDisappear { stopDetailsVM.leaveStopPredictions() }
.transition(transition)

case let .legacyStopDetails(stop, filter):
Expand Down
52 changes: 42 additions & 10 deletions iosApp/iosApp/Pages/StopDetails/StopDetailsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,69 @@ struct StopDetailsPage: View {

var body: some View {
stopDetails
.onChange(of: stopId) { nextStopId in changeStop(nextStopId) }
.onChange(of: stopDetailsVM.global) { _ in updateDepartures() }
.onChange(of: stopDetailsVM.pinnedRoutes) { _ in updateDepartures() }
.onChange(of: stopDetailsVM.predictionsByStop) { _ in updateDepartures() }
.onChange(of: stopDetailsVM.schedulesResponse) { _ in updateDepartures() }
.onChange(of: stopDetailsVM.stopData) { stopData in
errorBannerVM.loadingWhenPredictionsStale = !(stopData?.predictionsLoaded ?? true)
updateDepartures()
}
.onChange(of: stopFilter) { nextStopFilter in setTripFilter(stopFilter: nextStopFilter) }
.onChange(of: internalDepartures) { _ in
let nextStopFilter = setStopFilter()
setTripFilter(stopFilter: nextStopFilter)
}
.onAppear { loadEverything() }
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.task(id: stopId) {
while !Task.isCancelled {
now = Date.now
updateDepartures()
checkPredictionsStale()
stopDetailsVM.checkStopPredictionsStale()
try? await Task.sleep(for: .seconds(5))
}
}
.onDisappear { stopDetailsVM.leavePredictions() }
.onReceive(inspection.notice) { inspection.visit(self, $0) }
.withScenePhaseHandlers(
onActive: {
stopDetailsVM.returnFromBackground()
joinPredictions()
stopDetailsVM.joinStopPredictions(stopId)
},
onInactive: stopDetailsVM.leavePredictions,
onInactive: stopDetailsVM.leaveStopPredictions,
onBackground: {
stopDetailsVM.leavePredictions()
stopDetailsVM.leaveStopPredictions()
errorBannerVM.loadingWhenPredictionsStale = true
}
)
}

func setStopFilter() -> StopDetailsFilter? {
let nextStopFilter = stopFilter ?? internalDepartures?.autoStopFilter()
if stopFilter != nextStopFilter {
nearbyVM.setLastStopDetailsFilter(stopId, nextStopFilter)
}
return nextStopFilter
}

func setTripFilter(stopFilter: StopDetailsFilter?) {
let tripFilter = internalDepartures?.autoTripFilter(
stopFilter: stopFilter,
currentTripFilter: tripFilter,
filterAtTime: now.toKotlinInstant()
)
nearbyVM.setLastTripDetailsFilter(stopId, tripFilter)
}

func updateDepartures() {
Task {
if stopId != stopDetailsVM.stopData?.stopId { return }
let nextDepartures = stopDetailsVM.getDepartures(
stopId: stopId,
alerts: nearbyVM.alerts,
useTripHeadsigns: nearbyVM.tripHeadsignsEnabled,
now: now
)
Task { @MainActor in
nearbyVM.setDepartures(stopId, nextDepartures)
internalDepartures = nextDepartures
}
}
}
}
111 changes: 0 additions & 111 deletions iosApp/iosApp/Pages/StopDetails/StopDetailsPageHandlerExtension.swift

This file was deleted.

Loading

0 comments on commit 9582f34

Please sign in to comment.