Skip to content

Commit e14642e

Browse files
committed
Move ConnectivityObserver to WooFoundation for reusability and set to environment in POS
1 parent d65bcf4 commit e14642e

File tree

15 files changed

+58
-30
lines changed

15 files changed

+58
-30
lines changed

Modules/Sources/PointOfSale/DependencyProviders/POSDependencyProviding.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public protocol POSDependencyProviding {
1212
var currency: CurrencySettings { get }
1313
var featureFlags: POSFeatureFlagProviding { get }
1414
var session: POSSessionManagerProviding { get }
15+
var connectivity: ConnectivityObserver { get }
1516
}
1617

1718
/// Analytics service abstraction

Modules/Sources/PointOfSale/Environment/POSEnvironmentKeys.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SwiftUI
2+
import Combine
23
import WooFoundation
34
import Experiments
45
import protocol Yosemite.Action
@@ -48,6 +49,11 @@ public struct POSSessionManagerKey: EnvironmentKey {
4849
public static let defaultValue: POSSessionManagerProviding = DefaultPOSSessionManager()
4950
}
5051

52+
/// Environment key for POS connectivity
53+
public struct POSConnectivityKey: EnvironmentKey {
54+
public static let defaultValue: ConnectivityObserver = DefaultPOSConnectivity()
55+
}
56+
5157
// Default implementations for testing/previews
5258
private struct DefaultPOSStores: POSStoresProviding {
5359
func dispatch(_ action: Yosemite.Action) {}
@@ -61,6 +67,13 @@ private struct DefaultPOSFeatureFlags: POSFeatureFlagProviding {
6167
func isFeatureFlagEnabled(_ flag: FeatureFlag) -> Bool { false }
6268
}
6369

70+
private class DefaultPOSConnectivity: ConnectivityObserver {
71+
@Published private(set) var currentStatus: ConnectivityStatus = .reachable(type: .ethernetOrWiFi)
72+
var statusPublisher: AnyPublisher<ConnectivityStatus, Never> { $currentStatus.eraseToAnyPublisher() }
73+
func startObserving() {}
74+
func stopObserving() {}
75+
}
76+
6477
public extension EnvironmentValues {
6578
var posAnalytics: POSAnalyticsProviding {
6679
get { self[POSAnalyticsKey.self] }
@@ -86,4 +99,9 @@ public extension EnvironmentValues {
8699
get { self[POSSessionManagerKey.self] }
87100
set { self[POSSessionManagerKey.self] = newValue }
88101
}
102+
103+
var posConnectivity: ConnectivityObserver {
104+
get { self[POSConnectivityKey.self] }
105+
set { self[POSConnectivityKey.self] = newValue }
106+
}
89107
}

WooCommerce/Classes/Tools/Connectivity/ConnectivityObserver.swift renamed to Modules/Sources/WooFoundation/Utilities/Connectivity/ConnectivityObserver.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Combine
22

33
/// Interface for the observing connectivity
44
///
5-
protocol ConnectivityObserver {
5+
public protocol ConnectivityObserver {
66
/// Getter for current state of the connectivity.
77
var currentStatus: ConnectivityStatus { get }
88

@@ -21,7 +21,7 @@ protocol ConnectivityObserver {
2121
/// - unknown: It is unknown whether the network is reachable.
2222
/// - notReachable: The network is not reachable.
2323
/// - reachable: The network is reachable.
24-
enum ConnectivityStatus: Equatable {
24+
public enum ConnectivityStatus: Equatable {
2525
case unknown
2626
case notReachable
2727
case reachable(type: ConnectionType)
@@ -32,7 +32,7 @@ enum ConnectivityStatus: Equatable {
3232
/// - ethernetOrWiFi: The connection type is either over Ethernet or WiFi.
3333
/// - cellular: The connection type is a cellular connection.
3434
/// - other: The connection type is via a local loopback network, virtual network or other unknown types.
35-
enum ConnectionType: Equatable {
35+
public enum ConnectionType: Equatable {
3636
case ethernetOrWiFi
3737
case cellular
3838
case other

WooCommerce/Classes/Tools/Connectivity/DefaultConnectivityObserver.swift renamed to Modules/Sources/WooFoundation/Utilities/Connectivity/DefaultConnectivityObserver.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import Combine
22
import Network
33

4-
final class DefaultConnectivityObserver: ConnectivityObserver {
4+
public final class DefaultConnectivityObserver: ConnectivityObserver {
55

66
/// Network monitor to evaluate connection.
77
///
88
private let networkMonitor: NetworkMonitoring
99
private let observingQueue: DispatchQueue = .global(qos: .background)
1010

11-
@Published private(set) var currentStatus: ConnectivityStatus = .unknown
11+
@Published private(set) public var currentStatus: ConnectivityStatus = .unknown
1212

13-
var statusPublisher: AnyPublisher<ConnectivityStatus, Never> {
13+
public var statusPublisher: AnyPublisher<ConnectivityStatus, Never> {
1414
$currentStatus.eraseToAnyPublisher()
1515
}
1616

17+
public convenience init() {
18+
self.init(networkMonitor: NWPathMonitor())
19+
}
20+
1721
init(networkMonitor: NetworkMonitoring = NWPathMonitor()) {
1822
self.networkMonitor = networkMonitor
1923
startObserving()
@@ -25,11 +29,11 @@ final class DefaultConnectivityObserver: ConnectivityObserver {
2529
}
2630
}
2731

28-
func startObserving() {
32+
public func startObserving() {
2933
networkMonitor.start(queue: observingQueue)
3034
}
3135

32-
func stopObserving() {
36+
public func stopObserving() {
3337
networkMonitor.cancel()
3438
}
3539

WooCommerce/Classes/POS/Adaptors/POSServiceLocatorAdaptor.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ import struct Yosemite.Site
1212
/// Adaptor that bridges main app ServiceLocator to POS dependency abstraction to support POS modularization
1313
final class POSServiceLocatorAdaptor: POSDependencyProviding {
1414
var analytics: POSAnalyticsProviding {
15-
return POSAnalyticsAdaptor()
15+
POSAnalyticsAdaptor()
1616
}
1717

1818
var stores: POSStoresProviding {
19-
return POSStoresAdaptor(stores: ServiceLocator.stores)
19+
POSStoresAdaptor(stores: ServiceLocator.stores)
2020
}
2121

2222
var currency: CurrencySettings {
23-
return ServiceLocator.currencySettings
23+
ServiceLocator.currencySettings
2424
}
2525

2626
var featureFlags: POSFeatureFlagProviding {
27-
return POSFeatureFlagAdaptor(featureFlagService: ServiceLocator.featureFlagService)
27+
POSFeatureFlagAdaptor(featureFlagService: ServiceLocator.featureFlagService)
2828
}
2929

3030
var session: POSSessionManagerProviding {
31-
return POSSessionManagerAdaptor(sessionManager: ServiceLocator.stores.sessionManager)
31+
POSSessionManagerAdaptor(sessionManager: ServiceLocator.stores.sessionManager)
32+
}
33+
34+
var connectivity: ConnectivityObserver {
35+
ServiceLocator.connectivityObserver
3236
}
3337
}
3438

WooCommerce/Classes/POS/Presentation/PointOfSaleEntryPointView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct PointOfSaleEntryPointView: View {
8585
.environment(\.posStores, services.stores)
8686
.environment(\.posFeatureFlags, services.featureFlags)
8787
.environment(\.posSession, services.session)
88+
.environment(\.posConnectivity, services.connectivity)
8889
.injectKeyboardObserver()
8990
.onAppear {
9091
onPointOfSaleModeActiveStateChange(true)

WooCommerce/Classes/POS/Presentation/Reusable Views/POSConnectivityView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import SwiftUI
22
import Combine
3+
import enum WooFoundation.ConnectivityStatus
34

45
struct POSConnectivityView: View {
5-
let connectivityObserver: ConnectivityObserver = ServiceLocator.connectivityObserver
66
@State private var isVisible = false
77
@State private var cancellable: AnyCancellable?
8+
@Environment(\.posConnectivity) private var connectivityObserver
89

910
var body: some View {
1011
ZStack(alignment: .top) {

WooCommerce/Classes/POS/Utils/PreviewHelpers.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ final class POSPreviewServices: POSDependencyProviding {
298298
var currency: CurrencySettings = CurrencySettings()
299299
var featureFlags: POSFeatureFlagProviding = POSPreviewFeatureFlags()
300300
var session: POSSessionManagerProviding = POSPreviewSessionManager()
301+
var connectivity: ConnectivityObserver = POSPreviewConnectivity()
301302
}
302303

303304
// Preview implementations for all service types
@@ -313,4 +314,11 @@ private struct POSPreviewFeatureFlags: POSFeatureFlagProviding {
313314
func isFeatureFlagEnabled(_ flag: FeatureFlag) -> Bool { false }
314315
}
315316

317+
private class POSPreviewConnectivity: ConnectivityObserver {
318+
@Published private(set) var currentStatus: ConnectivityStatus = .reachable(type: .ethernetOrWiFi)
319+
var statusPublisher: AnyPublisher<ConnectivityStatus, Never> { $currentStatus.eraseToAnyPublisher() }
320+
func startObserving() {}
321+
func stopObserving() {}
322+
}
323+
316324
#endif

WooCommerce/Classes/System/WooNavigationController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Combine
22
import UIKit
3+
import protocol WooFoundation.ConnectivityObserver
4+
import enum WooFoundation.ConnectivityStatus
35

46
/// Subclass to set Woo styling. Removes back button text on managed view controllers.
57
///

WooCommerce/Classes/ViewRelated/Dashboard/DashboardView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import struct Yosemite.StoreOnboardingTask
55
import struct Yosemite.Coupon
66
import struct Yosemite.Order
77
import struct Yosemite.DashboardCard
8+
import enum WooFoundation.ConnectivityStatus
89

910
/// View for the dashboard screen
1011
///

0 commit comments

Comments
 (0)