11import Foundation
2+ import GraphAPI
23import KsApi
34import Prelude
45import ReactiveExtensions
@@ -20,7 +21,7 @@ public protocol PledgeShippingLocationViewModelInputs {
2021public 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