Skip to content

Commit 994f37f

Browse files
Tinkering still
1 parent 9b84a67 commit 994f37f

File tree

3 files changed

+54
-44
lines changed

3 files changed

+54
-44
lines changed

Kickstarter-iOS/Features/PledgeShippingLocation/PledgeShippingLocationViewController.swift

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ final class PledgeShippingLocationViewController: UIViewController {
131131
self.delegate?.pledgeShippingLocationViewController(self, didSelect: location)
132132
}
133133

134-
self.viewModel.outputs.presentShippingRules
134+
self.viewModel.outputs.presentShippingLocations
135135
.observeForUI()
136-
.observeValues { [weak self] project, shippingRules, location in
137-
self?.presentShippingRules(
138-
project: project, shippingRules: shippingRules, selectedLocation: location
136+
.observeValues { [weak self] locations, location in
137+
self?.presentShippingLocations(
138+
locations: locations, selectedLocation: location
139139
)
140140
}
141141

@@ -168,21 +168,10 @@ final class PledgeShippingLocationViewController: UIViewController {
168168

169169
// MARK: - Functions
170170

171-
private func presentShippingRules(
172-
project: Project, shippingRules _: [ShippingRule], selectedLocation: Location
171+
private func presentShippingLocations(
172+
locations: [Location],
173+
selectedLocation: Location
173174
) {
174-
let locations: [Location] = project.rewards.map { $0.shippingRulesExpanded }
175-
.map { rules in
176-
rules?.map { $0.location }
177-
}
178-
.reduce([]) { allLocations, locations in
179-
guard let locations = locations else {
180-
return allLocations
181-
}
182-
183-
return allLocations + locations
184-
}
185-
186175
let viewController = ShippingLocationsViewController(
187176
withLocations: locations,
188177
selectedLocation: selectedLocation

Library/SharedFunctions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ public func defaultShippingRule(fromShippingRules shippingRules: [ShippingRule])
217217
return shippingRuleInUSA ?? shippingRules.first
218218
}
219219

220+
public func defaultShippingLocation(fromLocations locations: [Location]) -> Location? {
221+
let environmentCountry = AppEnvironment.current.config?.countryCode
222+
if let currentLocation = locations
223+
.first(where: { $0.country == environmentCountry }) {
224+
return currentLocation
225+
}
226+
227+
if let 🇺🇸🦅 = locations.first(where: { $0.country == "US" }) {
228+
return 🇺🇸🦅
229+
}
230+
231+
return locations.first
232+
}
233+
220234
public func formattedAmountForRewardOrBacking(
221235
project: Project,
222236
rewardOrBacking: Either<Reward, Backing>

Library/ViewModels/PledgeShippingLocationViewModel.swift

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import GraphAPI
23
import KsApi
34
import Prelude
45
import ReactiveExtensions
@@ -20,7 +21,7 @@ public protocol PledgeShippingLocationViewModelInputs {
2021
public protocol PledgeShippingLocationViewModelOutputs {
2122
var adaptableStackViewIsHidden: Signal<Bool, Never> { get }
2223
var dismissShippingRules: Signal<Void, Never> { get }
23-
var presentShippingRules: Signal<(Project, [ShippingRule], Location), Never> { get }
24+
var presentShippingLocations: Signal<([Location], Location), Never> { get }
2425
var notifyDelegateOfSelectedShippingLocation: Signal<Location, Never> { get }
2526
var shimmerLoadingViewIsHidden: Signal<Bool, Never> { get }
2627
var shippingLocationButtonTitle: Signal<String, Never> { get }
@@ -50,13 +51,12 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
5051
let shippingShouldBeginLoading = project
5152
.mapConst(true)
5253

53-
let shippingRulesEvent = project
54-
.switchMap { project -> SignalProducer<Signal<[ShippingRule], ErrorEnvelope>.Event, Never> in
55-
getShippingRulesForAllRewards(in: project)
54+
let shippingLocationsEvent = project
55+
.switchMap { project in
56+
shippingLocations(forProject: project)
5657
}
5758

58-
let shippingRulesLoadingCompleted = shippingRulesEvent
59-
.filter { $0.isTerminating }
59+
let shippingRulesLoadingCompleted = shippingLocationsEvent
6060
.mapConst(false)
6161
.ksr_debounce(.seconds(1), on: AppEnvironment.current.scheduler)
6262

@@ -68,28 +68,22 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
6868
self.adaptableStackViewIsHidden = isLoading
6969
self.shimmerLoadingViewIsHidden = isLoading.negate()
7070

71-
let shippingRules = shippingRulesEvent.values()
72-
73-
let initialShippingRule = Signal.combineLatest(
71+
let initialShippingLocation = Signal.combineLatest(
7472
project,
75-
shippingRules,
73+
shippingLocationsEvent,
7674
selectedLocationId
7775
)
78-
.map(determineShippingRule)
79-
.map { $0?.location }
76+
.map(determineShippingLocation)
8077

81-
self.shippingRulesError = shippingRulesEvent.errors().map { _ in
82-
Strings.We_were_unable_to_load_the_shipping_destinations()
83-
}
78+
self.shippingRulesError = Signal.never // TODO:
8479

8580
self.notifyDelegateOfSelectedShippingLocation = Signal.merge(
86-
initialShippingRule.skipNil(),
81+
initialShippingLocation.skipNil(),
8782
self.shippingLocationUpdatedSignal
8883
)
8984

90-
self.presentShippingRules = Signal.combineLatest(
91-
project,
92-
shippingRulesEvent.values(),
85+
self.presentShippingLocations = Signal.combineLatest(
86+
shippingLocationsEvent,
9387
self.notifyDelegateOfSelectedShippingLocation
9488
)
9589
.takeWhen(self.shippingLocationButtonTappedSignal)
@@ -134,7 +128,7 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
134128

135129
public let adaptableStackViewIsHidden: Signal<Bool, Never>
136130
public let dismissShippingRules: Signal<Void, Never>
137-
public let presentShippingRules: Signal<(Project, [ShippingRule], Location), Never>
131+
public let presentShippingLocations: Signal<([Location], Location), Never>
138132
public let notifyDelegateOfSelectedShippingLocation: Signal<Location, Never>
139133
public let shimmerLoadingViewIsHidden: Signal<Bool, Never>
140134
public let shippingLocationButtonTitle: Signal<String, Never>
@@ -163,18 +157,31 @@ private func shippingValue(of project: Project, with shippingRuleCost: Double) -
163157
return Format.attributedPlusSign(combinedAttributes) + attributedCurrency
164158
}
165159

166-
private func determineShippingRule(
160+
private func determineShippingLocation(
167161
with project: Project,
168-
shippingRules: [ShippingRule],
162+
locations: [Location],
169163
selectedLocationId: Int?
170-
) -> ShippingRule? {
164+
) -> Location? {
171165
if
172166
let locationId = selectedLocationId ?? project.personalization.backing?.locationId,
173-
let selectedShippingRule = shippingRules.first(where: { $0.location.id == locationId }) {
174-
return selectedShippingRule
167+
let selectedShippingLocation = locations.first(where: { $0.id == locationId }) {
168+
return selectedShippingLocation
175169
}
176170

177-
return defaultShippingRule(fromShippingRules: shippingRules)
171+
return defaultShippingLocation(fromLocations: locations)
172+
}
173+
174+
private func shippingLocations(forProject _: Project) -> SignalProducer<[Location], ErrorEnvelope> {
175+
let query = GraphAPI.LocationsByTermQuery(
176+
term: GraphQLNullable.some("America"),
177+
first: GraphQLNullable.some(25)
178+
)
179+
let signal = AppEnvironment.current.apiService.fetch(query: query)
180+
.map { data in
181+
let locations = Location.locations(from: data)
182+
return locations
183+
}
184+
return signal
178185
}
179186

180187
/*

0 commit comments

Comments
 (0)