Skip to content

Commit 0d24010

Browse files
More cleanup
1 parent b60e30c commit 0d24010

File tree

3 files changed

+30
-73
lines changed

3 files changed

+30
-73
lines changed

Library/SharedFunctions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ public func defaultShippingLocation(fromLocations locations: [Location]) -> Loca
224224
return currentLocation
225225
}
226226

227-
if let 🇺🇸🦅 = locations.first(where: { $0.country == "US" }) {
228-
return 🇺🇸🦅
227+
if let usaLocation = locations.first(where: { $0.country == "US" }) {
228+
return usaLocation
229229
}
230230

231231
return locations.first

Library/ViewModels/PledgeShippingLocationViewModel.swift

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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,70 +178,12 @@ private func determineShippingLocation(
172178
return defaultShippingLocation(fromLocations: locations)
173179
}
174180

175-
private func shippingLocations(forProject _: Project) -> SignalProducer<[Location], ErrorEnvelope> {
176-
let query = GraphAPI.LocationsByTermQuery(
177-
term: GraphQLNullable.some("America"),
178-
first: GraphQLNullable.some(25)
179-
)
181+
private func shippableLocations() -> SignalProducer<[Location], ErrorEnvelope> {
182+
let query = GraphAPI.ShippableLocationsQuery()
180183
let signal = AppEnvironment.current.apiService.fetch(query: query)
181184
.map { data in
182185
let locations = Location.locations(from: data)
183186
return locations
184187
}
185188
return signal
186189
}
187-
188-
/*
189-
Iterates through all reward shipping preferences to get all possible shipping rules.
190-
*/
191-
192-
private func getShippingRulesForAllRewards(in project: Project) -> SignalProducer<Signal<
193-
[ShippingRule],
194-
ErrorEnvelope
195-
>.Event, Never> {
196-
/// Get all of the reward IDs we'll need to fetch the Shipping Rules. See inner method logic for more details.
197-
let rewardIDsToQuery: Set<Int> = getRewardIDsToQuery(for: project)
198-
199-
/// Initializing the result with a fetch using the first reward ID in the Set to avoid optional warnings
200-
var queryResult: SignalProducer<Signal<[ShippingRule], ErrorEnvelope>.Event, Never>?
201-
202-
/// Fetch the shipping rules for each reward and then consolidate each corresponding SignalProducer into the `queryResult` variable using .merge(with:).
203-
rewardIDsToQuery.forEach { id in
204-
let fetchResult = AppEnvironment.current.apiService.fetchRewardShippingRules(
205-
projectId: project.id,
206-
rewardId: id
207-
)
208-
.ksr_delay(AppEnvironment.current.apiDelayInterval, on: AppEnvironment.current.scheduler)
209-
.map(ShippingRulesEnvelope.lens.shippingRules.view)
210-
.retry(upTo: 3)
211-
.materialize()
212-
213-
queryResult = queryResult?.merge(with: fetchResult) ?? fetchResult
214-
}
215-
216-
return queryResult ?? SignalProducer(value: .value([]))
217-
}
218-
219-
private func getRewardIDsToQuery(for project: Project) -> Set<Int> {
220-
/// Using a Set to avoid adding duplicate reward IDs. Some rewards may have the same shipping preferences.
221-
var rewardIDsToQuery = Set<Int>()
222-
223-
/// If project contains a reward with an `unrestricted` shipping preference, we can query just that reward. This will return ALL available locations.
224-
if let reward = project.rewards
225-
.first(where: { $0.isUnRestrictedShippingPreference && $0.shipping.enabled }) {
226-
rewardIDsToQuery.insert(reward.id)
227-
}
228-
229-
/// 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.
230-
if rewardIDsToQuery.isEmpty {
231-
let restrictedRewards = project.rewards
232-
.filter {
233-
($0.isRestrictedShippingPreference || $0.hasNoShippingPreference)
234-
&& $0.shipping.enabled
235-
}
236-
237-
restrictedRewards.forEach { rewardIDsToQuery.insert($0.id) }
238-
}
239-
240-
return rewardIDsToQuery
241-
}

Library/ViewModels/RewardsCollectionViewModelTests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ final class RewardsCollectionViewModelTests: TestCase {
1010
private let reloadDataWithValues = TestObserver<[Reward], Never>()
1111
private let scrollToRewardIndex = TestObserver<Int, Never>()
1212
private let goToCustomizeYourReward = TestObserver<PledgeViewData, Never>()
13+
private let shippingLocationViewHidden = TestObserver<Bool, Never>()
1314

1415
private let vm = RewardsCollectionViewModel()
1516

@@ -22,6 +23,8 @@ final class RewardsCollectionViewModelTests: TestCase {
2223
self.vm.outputs.scrollToRewardIndexPath.map { $0.row }.observe(self.scrollToRewardIndex.observer)
2324

2425
self.vm.outputs.goToCustomizeYourReward.observe(self.goToCustomizeYourReward.observer)
26+
27+
self.vm.outputs.shippingLocationViewHidden.observe(self.shippingLocationViewHidden.observer)
2528
}
2629

2730
func testRewardsOrdered() {
@@ -203,6 +206,8 @@ final class RewardsCollectionViewModelTests: TestCase {
203206
self.vm.viewDidLoad()
204207
self.vm.viewDidLayoutSubviews()
205208

209+
self.shippingLocationViewHidden.assertLastValue(true)
210+
206211
self.vm.inputs.shippingLocationSelected(location2)
207212
self.vm.inputs.rewardSelected(with: reward.id)
208213

@@ -249,6 +254,8 @@ final class RewardsCollectionViewModelTests: TestCase {
249254
self.vm.viewDidLoad()
250255
self.vm.viewDidLayoutSubviews()
251256

257+
self.shippingLocationViewHidden.assertLastValue(true)
258+
252259
self.vm.inputs.shippingLocationSelected(location1)
253260
self.vm.inputs.rewardSelected(with: reward.id)
254261

@@ -300,12 +307,14 @@ final class RewardsCollectionViewModelTests: TestCase {
300307
|> Project.lens.rewardData.rewards .~ rewards
301308

302309
self.vm.configure(with: testProject, refTag: nil, context: .createPledge, secretRewardToken: nil)
303-
// Because the shipping location is powered by the available shipping rules,
304-
// if there are no shippable rewards, the location element may be hidden and output `nil` once.
305310
self.vm.inputs.shippingLocationSelected(nil)
306311
self.vm.viewDidLoad()
307312
self.vm.viewDidLayoutSubviews()
308313

314+
// Because the shipping location is powered by the available shipping rules,
315+
// if there are no shippable rewards, the location element may be hidden and output `nil` once.
316+
self.shippingLocationViewHidden.assertLastValue(false)
317+
309318
self.vm.inputs.rewardSelected(with: digitalReward.id)
310319

311320
self.goToCustomizeYourReward.assertDidEmitValue()

0 commit comments

Comments
 (0)