@@ -51,14 +51,20 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
5151 let shippingShouldBeginLoading = project
5252 . mapConst ( true )
5353
54- let locations : Signal < [ Location ] , Never > = project. switchMap { p -> SignalProducer < [ Location ] , Never > in
55- shippingLocations ( forProject: p)
56- . demoteErrors ( replaceErrorWith: [ ] )
57- }
54+ let locations : Signal < [ Location ] ? , Never > = configData
55+ . ignoreValues ( )
56+ . switchMap { _ in
57+ shippableLocations ( )
58+ . wrapInOptional ( )
59+ . demoteErrors ( replaceErrorWith: nil )
60+ }
61+
62+ let loadedLocations = locations. skipNil ( )
63+ let erroredLocations = locations. filter { $0. isNil }
5864
5965 let shippingRulesLoadingCompleted = locations
66+ . demoteErrors ( replaceErrorWith: [ ] )
6067 . mapConst ( false )
61- . ksr_debounce ( . seconds( 1 ) , on: AppEnvironment . current. scheduler)
6268
6369 let isLoading = Signal . merge (
6470 shippingShouldBeginLoading,
@@ -70,21 +76,21 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
7076
7177 let initialShippingLocation = Signal . combineLatest (
7278 project,
73- locations ,
79+ loadedLocations ,
7480 selectedLocationId
7581 )
7682 . map ( determineShippingLocation)
7783
78- self . shippingRulesError = locations . map { $0 == [ ] ? " Shoot " : nil }
79- . skipNil ( )
84+ self . shippingRulesError = erroredLocations
85+ . mapConst ( Strings . We_were_unable_to_load_the_shipping_destinations ( ) )
8086
8187 self . notifyDelegateOfSelectedShippingLocation = Signal . merge (
8288 initialShippingLocation. skipNil ( ) ,
8389 self . shippingLocationUpdatedSignal
8490 )
8591
8692 self . presentShippingLocations = Signal . combineLatest (
87- locations ,
93+ loadedLocations ,
8894 self . notifyDelegateOfSelectedShippingLocation
8995 )
9096 . takeWhen ( self . shippingLocationButtonTappedSignal)
@@ -172,7 +178,7 @@ private func determineShippingLocation(
172178 return defaultShippingLocation ( fromLocations: locations)
173179}
174180
175- private func shippingLocations ( forProject _ : Project ) -> SignalProducer < [ Location ] , ErrorEnvelope > {
181+ private func shippableLocations ( ) -> SignalProducer < [ Location ] , ErrorEnvelope > {
176182 let query = GraphAPI . ShippableLocationsQuery ( )
177183 let signal = AppEnvironment . current. apiService. fetch ( query: query)
178184 . map { data in
@@ -181,58 +187,3 @@ private func shippingLocations(forProject _: Project) -> SignalProducer<[Locatio
181187 }
182188 return signal
183189}
184-
185- /*
186- Iterates through all reward shipping preferences to get all possible shipping rules.
187- */
188-
189- private func getShippingRulesForAllRewards( in project: Project ) -> SignalProducer < Signal <
190- [ ShippingRule ] ,
191- ErrorEnvelope
192- > . Event , Never > {
193- /// Get all of the reward IDs we'll need to fetch the Shipping Rules. See inner method logic for more details.
194- let rewardIDsToQuery : Set < Int > = getRewardIDsToQuery ( for: project)
195-
196- /// Initializing the result with a fetch using the first reward ID in the Set to avoid optional warnings
197- var queryResult : SignalProducer < Signal < [ ShippingRule ] , ErrorEnvelope > . Event , Never > ?
198-
199- /// Fetch the shipping rules for each reward and then consolidate each corresponding SignalProducer into the `queryResult` variable using .merge(with:).
200- rewardIDsToQuery. forEach { id in
201- let fetchResult = AppEnvironment . current. apiService. fetchRewardShippingRules (
202- projectId: project. id,
203- rewardId: id
204- )
205- . ksr_delay ( AppEnvironment . current. apiDelayInterval, on: AppEnvironment . current. scheduler)
206- . map ( ShippingRulesEnvelope . lens. shippingRules. view)
207- . retry ( upTo: 3 )
208- . materialize ( )
209-
210- queryResult = queryResult? . merge ( with: fetchResult) ?? fetchResult
211- }
212-
213- return queryResult ?? SignalProducer ( value: . value( [ ] ) )
214- }
215-
216- private func getRewardIDsToQuery( for project: Project ) -> Set < Int > {
217- /// Using a Set to avoid adding duplicate reward IDs. Some rewards may have the same shipping preferences.
218- var rewardIDsToQuery = Set < Int > ( )
219-
220- /// If project contains a reward with an `unrestricted` shipping preference, we can query just that reward. This will return ALL available locations.
221- if let reward = project. rewards
222- . first ( where: { $0. isUnRestrictedShippingPreference && $0. shipping. enabled } ) {
223- rewardIDsToQuery. insert ( reward. id)
224- }
225-
226- /// If project does not contain a reward with an `unrestricted` shipping preference, then we'll need to query all other rewards to capture all possible shipping locations.
227- if rewardIDsToQuery. isEmpty {
228- let restrictedRewards = project. rewards
229- . filter {
230- ( $0. isRestrictedShippingPreference || $0. hasNoShippingPreference)
231- && $0. shipping. enabled
232- }
233-
234- restrictedRewards. forEach { rewardIDsToQuery. insert ( $0. id) }
235- }
236-
237- return rewardIDsToQuery
238- }
0 commit comments