Skip to content

Commit 38f00ea

Browse files
committed
Send support availability to AlamofireNetwork
1 parent d37e1f7 commit 38f00ea

File tree

32 files changed

+271
-121
lines changed

32 files changed

+271
-121
lines changed

Modules/Sources/NetworkingCore/Network/AlamofireNetwork.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ public class AlamofireNetwork: Network {
5757

5858
public var session: URLSession { Session.default.session }
5959

60-
private var subscription: AnyCancellable?
60+
private var siteSubscription: AnyCancellable?
6161

6262
/// Thread-safe error handler for failure tracking and retry logic
6363
private let errorHandler: AlamofireNetworkErrorHandler
6464

65+
private var appPasswordSupportSubscription: AnyCancellable?
66+
6567
/// Public Initializer
6668
///
6769
/// - Parameters:
@@ -73,6 +75,7 @@ public class AlamofireNetwork: Network {
7375
/// Defaults to false for backward compatibility. Set to true when making concurrent requests immediately after initialization.
7476
public required init(credentials: Credentials?,
7577
selectedSite: AnyPublisher<JetpackSite?, Never>?,
78+
appPasswordSupportState: AnyPublisher<Bool, Never>?,
7679
userDefaults: UserDefaults = .standard,
7780
sessionManager: Alamofire.Session? = nil,
7881
ensuresSessionManagerIsInitialized: Bool = false) {
@@ -111,18 +114,8 @@ public class AlamofireNetwork: Network {
111114
}
112115
}()
113116
updateAuthenticationMode(authenticationMode)
114-
}
115-
116-
public func updateAppPasswordSwitching(enabled: Bool) {
117-
guard let credentials, case .wpcom = credentials else { return }
118-
if enabled, let selectedSite {
119-
observeSelectedSite(selectedSite)
120-
} else {
121-
requestConverter = RequestConverter(siteAddress: nil)
122-
requestAuthenticator.updateAuthenticator(DefaultRequestAuthenticator(credentials: credentials))
123-
requestAuthenticator.delegate = nil
124-
updateAuthenticationMode(.jetpackTunnel)
125-
subscription = nil
117+
if let appPasswordSupportState {
118+
observeAppPasswordSupportState(appPasswordSupportState)
126119
}
127120
}
128121

@@ -279,6 +272,28 @@ public class AlamofireNetwork: Network {
279272
}
280273

281274
private extension AlamofireNetwork {
275+
276+
func observeAppPasswordSupportState(_ appPasswordSupportState: AnyPublisher<Bool, Never>) {
277+
appPasswordSupportSubscription = appPasswordSupportState
278+
.removeDuplicates()
279+
.sink { [weak self] enabled in
280+
self?.updateAppPasswordSwitching(enabled: enabled)
281+
}
282+
}
283+
284+
func updateAppPasswordSwitching(enabled: Bool) {
285+
guard let credentials, case .wpcom = credentials else { return }
286+
if enabled, let selectedSite {
287+
observeSelectedSite(selectedSite)
288+
} else {
289+
requestConverter = RequestConverter(siteAddress: nil)
290+
requestAuthenticator.updateAuthenticator(DefaultRequestAuthenticator(credentials: credentials))
291+
requestAuthenticator.delegate = nil
292+
updateAuthenticationMode(.jetpackTunnel)
293+
siteSubscription = nil
294+
}
295+
}
296+
282297
/// Creates a session manager with request retrier and adapter
283298
///
284299
func makeSession(configuration sessionConfiguration: URLSessionConfiguration) -> Alamofire.Session {
@@ -288,7 +303,7 @@ private extension AlamofireNetwork {
288303
/// Updates `requestConverter` and `requestAuthenticator` when selected site changes
289304
///
290305
func observeSelectedSite(_ selectedSite: AnyPublisher<JetpackSite?, Never>) {
291-
subscription = selectedSite
306+
siteSubscription = selectedSite
292307
.removeDuplicates()
293308
.combineLatest(userDefaults.publisher(for: \.applicationPasswordUnsupportedList))
294309
.sink { [weak self] site, unsupportedList in

Modules/Sources/Yosemite/PointOfSale/Coupons/PointOfSaleCouponFetchStrategyFactory.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ public struct PointOfSaleCouponFetchStrategyFactory {
1616
currencySettings: CurrencySettings,
1717
credentials: Credentials?,
1818
selectedSite: AnyPublisher<JetpackSite?, Never>,
19+
appPasswordSupportState: AnyPublisher<Bool, Never>,
1920
storage: StorageManagerType) {
20-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
21+
let network = AlamofireNetwork(credentials: credentials,
22+
selectedSite: selectedSite,
23+
appPasswordSupportState: appPasswordSupportState)
2124
let remote = CouponsRemote(network: network)
2225
self.siteID = siteID
2326
self.currencySettings = currencySettings

Modules/Sources/Yosemite/PointOfSale/Coupons/PointOfSaleCouponService.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ public final class PointOfSaleCouponService: PointOfSaleCouponServiceProtocol {
3333
currencySettings: CurrencySettings,
3434
credentials: Credentials?,
3535
selectedSite: AnyPublisher<JetpackSite?, Never>,
36+
appPasswordSupportState: AnyPublisher<Bool, Never>,
3637
storage: StorageManagerType) {
37-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
38+
let network = AlamofireNetwork(credentials: credentials,
39+
selectedSite: selectedSite,
40+
appPasswordSupportState: appPasswordSupportState)
3841
self.init(siteID: siteID,
3942
currencySettings: currencySettings,
4043
settingStoreMethods: SettingStoreMethods(storageManager: storage, network: network),

Modules/Sources/Yosemite/PointOfSale/Eligibility/POSSystemStatusService.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
3030

3131
public init(credentials: Credentials?,
3232
selectedSite: AnyPublisher<JetpackSite?, Never>,
33+
appPasswordSupportState: AnyPublisher<Bool, Never>,
3334
storageManager: StorageManagerType) {
34-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
35+
let network = AlamofireNetwork(credentials: credentials,
36+
selectedSite: selectedSite,
37+
appPasswordSupportState: appPasswordSupportState)
3538
self.remote = SystemStatusRemote(network: network)
3639
self.storageManager = storageManager
3740
self.pluginsService = PluginsService(storageManager: storageManager)

Modules/Sources/Yosemite/PointOfSale/Items/PointOfSaleBarcodeScanService.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public final class PointOfSaleBarcodeScanService: PointOfSaleBarcodeScanServiceP
4343
public convenience init(siteID: Int64,
4444
credentials: Credentials?,
4545
selectedSite: AnyPublisher<JetpackSite?, Never>,
46+
appPasswordSupportState: AnyPublisher<Bool, Never>,
4647
currencySettings: CurrencySettings) {
47-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
48+
let network = AlamofireNetwork(credentials: credentials,
49+
selectedSite: selectedSite,
50+
appPasswordSupportState: appPasswordSupportState)
4851
self.init(siteID: siteID,
4952
productsRemote: ProductsRemote(network: network),
5053
currencySettings: currencySettings)

Modules/Sources/Yosemite/PointOfSale/Items/PointOfSaleItemFetchStrategyFactory.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ public final class PointOfSaleItemFetchStrategyFactory: PointOfSaleItemFetchStra
2121

2222
public init(siteID: Int64,
2323
credentials: Credentials?,
24-
selectedSite: AnyPublisher<JetpackSite?, Never>? = nil) {
24+
selectedSite: AnyPublisher<JetpackSite?, Never>? = nil,
25+
appPasswordSupportState: AnyPublisher<Bool, Never>? = nil) {
2526
self.siteID = siteID
26-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
27+
let network = AlamofireNetwork(credentials: credentials,
28+
selectedSite: selectedSite,
29+
appPasswordSupportState: appPasswordSupportState)
2730
self.productsRemote = ProductsRemote(network: network)
2831
self.variationsRemote = ProductVariationsRemote(network: network)
2932
}

Modules/Sources/Yosemite/PointOfSale/OrderList/PointOfSaleOrderListFetchStrategyFactory.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ public final class PointOfSaleOrderListFetchStrategyFactory: PointOfSaleOrderLis
1818
public init(siteID: Int64,
1919
credentials: Credentials?,
2020
selectedSite: AnyPublisher<JetpackSite?, Never>,
21+
appPasswordSupportState: AnyPublisher<Bool, Never>,
2122
currencyFormatter: CurrencyFormatter) {
2223
self.siteID = siteID
23-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
24+
let network = AlamofireNetwork(credentials: credentials,
25+
selectedSite: selectedSite,
26+
appPasswordSupportState: appPasswordSupportState)
2427
self.ordersRemote = OrdersRemote(network: network)
2528
self.currencyFormatter = currencyFormatter
2629
}

Modules/Sources/Yosemite/Tools/POS/POSCatalogFullSyncService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class POSCatalogFullSyncService: POSCatalogFullSyncServiceProtocol
3434

3535
public convenience init?(credentials: Credentials?,
3636
selectedSite: AnyPublisher<JetpackSite?, Never>,
37+
appPasswordSupportState: AnyPublisher<Bool, Never>,
3738
batchSize: Int = 2,
3839
grdbManager: GRDBManagerProtocol) {
3940
guard let credentials else {
@@ -42,6 +43,7 @@ public final class POSCatalogFullSyncService: POSCatalogFullSyncServiceProtocol
4243
}
4344
let network = AlamofireNetwork(credentials: credentials,
4445
selectedSite: selectedSite,
46+
appPasswordSupportState: appPasswordSupportState,
4547
ensuresSessionManagerIsInitialized: true)
4648
let syncRemote = POSCatalogSyncRemote(network: network)
4749
let persistenceService = POSCatalogPersistenceService(grdbManager: grdbManager)

Modules/Sources/Yosemite/Tools/POS/POSCatalogIncrementalSyncService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public final class POSCatalogIncrementalSyncService: POSCatalogIncrementalSyncSe
2626

2727
public convenience init?(credentials: Credentials?,
2828
selectedSite: AnyPublisher<JetpackSite?, Never>,
29+
appPasswordSupportState: AnyPublisher<Bool, Never>,
2930
batchSize: Int = 1,
3031
grdbManager: GRDBManagerProtocol) {
3132
guard let credentials else {
@@ -34,6 +35,7 @@ public final class POSCatalogIncrementalSyncService: POSCatalogIncrementalSyncSe
3435
}
3536
let network = AlamofireNetwork(credentials: credentials,
3637
selectedSite: selectedSite,
38+
appPasswordSupportState: appPasswordSupportState,
3739
ensuresSessionManagerIsInitialized: true)
3840
let syncRemote = POSCatalogSyncRemote(network: network)
3941
let persistenceService = POSCatalogPersistenceService(grdbManager: grdbManager)

Modules/Sources/Yosemite/Tools/POS/POSOrderService.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ public final class POSOrderService: POSOrderServiceProtocol {
1919
private let siteID: Int64
2020
private let ordersRemote: POSOrdersRemoteProtocol
2121

22-
public convenience init?(siteID: Int64, credentials: Credentials?, selectedSite: AnyPublisher<JetpackSite?, Never>) {
22+
public convenience init?(siteID: Int64,
23+
credentials: Credentials?,
24+
selectedSite: AnyPublisher<JetpackSite?, Never>,
25+
appPasswordSupportState: AnyPublisher<Bool, Never>) {
2326
guard let credentials else {
2427
DDLogError("⛔️ Could not create POSOrderService due to not finding credentials")
2528
return nil
2629
}
27-
let network = AlamofireNetwork(credentials: credentials, selectedSite: selectedSite)
30+
let network = AlamofireNetwork(credentials: credentials,
31+
selectedSite: selectedSite,
32+
appPasswordSupportState: appPasswordSupportState)
2833
self.init(siteID: siteID,
2934
ordersRemote: OrdersRemote(network: network))
3035
}

0 commit comments

Comments
 (0)