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

Allow setting TKUIResultsFetcher.modeReplacementHandler #254

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions Sources/TripKitUI/helper/RxTripKit/TKUIResultsFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class TKUIResultsFetcher {
///
/// - Parameters:
/// - request: The request for which to fetch trips
/// - modes: The modes to enable. If set to `nil` then it'll use the modes as set in the user defaults (see `TKUserProfileHelper` for more)
/// - classifier: Optional classifier, see `TKTripClassifier` for more
/// - Returns: Stream of fetching the results, multiple call backs as different
/// modes are fetched.
Expand All @@ -64,23 +65,32 @@ public class TKUIResultsFetcher {
}

// 1. Fetch current location if necessary
let prepared: Single<TripRequest>
var prepared: Single<(TripRequest, Set<String>?)>
if request.usesCurrentLocation {
prepared = TKLocationManager.shared.rx
.fetchCurrentLocation(within: Constants.secondsToRefine)
.map { location in
request.override(currentLocation: location)
return request
return (request, nil)
}

} else {
prepared = .just(request)
prepared = .just((request, nil))
}

if let modeAdjuster = Self.modeReplacementHandler {
prepared = prepared.flatMap { (request: TripRequest, _) in
let modesToAdjust: Set<String> = modes ?? request.modes
let region = request.startRegion ?? request.spanningRegion
return modeAdjuster(region, modesToAdjust)
.map { (request, $0) }
}
}

// 2. Then we can kick off the requests
return prepared
.asObservable()
.flatMapLatest { request -> Observable<Progress> in
.flatMapLatest { (request, modes) -> Observable<Progress> in
return TKRouter.rx.multiFetchRequest(
for: request, modes: modes,
classifier: classifier,
Expand All @@ -100,6 +110,10 @@ public class TKUIResultsFetcher {
return TKNamedCoordinate(coordinate: location.coordinate)
}

/// Replace the set of provided modes with the modes returned by the `Single`, called before
/// by `streamTrips` at the very start and information is not cached.
public static var modeReplacementHandler: ((TKRegion, Set<String>) -> Single<Set<String>>)? = nil

}


Expand Down