-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Sources/Amplitude/ObjC/ObjCNetworkConnectivityCheckerPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
@objc(AMPNetworkConnectivityCheckerPlugin) | ||
public class ObjCNetworkConnectivityCheckerPlugin: NSObject { | ||
@objc public static let Disabled: NSNumber? = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Sources/Amplitude/Plugins/NetworkConnectivityCheckerPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// NetworkConnectivityCheckerPlugin.swift | ||
// Amplitude-Swift | ||
// | ||
// Created by Xinyi.Ye on 1/26/24. | ||
// | ||
|
||
import Foundation | ||
import Network | ||
import Combine | ||
|
||
// Define a custom struct to represent network path status | ||
public struct NetworkPath { | ||
public var status: NWPath.Status | ||
|
||
public init(status: NWPath.Status) { | ||
self.status = status | ||
} | ||
} | ||
|
||
// Protocol for creating network paths | ||
protocol PathCreationProtocol { | ||
var networkPathPublisher: AnyPublisher<NetworkPath, Never>? { get } | ||
func start() | ||
} | ||
|
||
// Implementation of PathCreationProtocol using NWPathMonitor | ||
final class PathCreation: PathCreationProtocol { | ||
public var networkPathPublisher: AnyPublisher<NetworkPath, Never>? | ||
private let subject = PassthroughSubject<NWPath, Never>() | ||
private let monitor = NWPathMonitor() | ||
|
||
func start() { | ||
monitor.pathUpdateHandler = subject.send | ||
networkPathPublisher = subject | ||
.map { NetworkPath(status: $0.status) } | ||
.eraseToAnyPublisher() | ||
monitor.start(queue: .main) | ||
} | ||
} | ||
|
||
open class NetworkConnectivityCheckerPlugin: BeforePlugin { | ||
public static let Disabled: Bool? = nil | ||
var pathCreation: PathCreationProtocol | ||
private var pathUpdateCancellable: AnyCancellable? | ||
|
||
init(pathCreation: PathCreationProtocol = PathCreation()) { | ||
self.pathCreation = pathCreation | ||
super.init() | ||
} | ||
|
||
open override func setup(amplitude: Amplitude) { | ||
super.setup(amplitude: amplitude) | ||
amplitude.logger?.debug(message: "Installing NetworkConnectivityCheckerPlugin, offline feature should be supported.") | ||
|
||
pathCreation.start() | ||
pathUpdateCancellable = pathCreation.networkPathPublisher? | ||
.sink(receiveValue: { [weak self] networkPath in | ||
let isOffline = !(networkPath.status == .satisfied) | ||
if self?.amplitude?.configuration.offline == isOffline { | ||
return | ||
} | ||
self?.amplitude?.logger?.debug(message: "Network connectivity changed to \(isOffline ? "offline" : "online").") | ||
self?.amplitude?.configuration.offline = isOffline | ||
if !isOffline { | ||
amplitude.flush() | ||
} | ||
}) | ||
} | ||
|
||
open override func teardown() { | ||
pathUpdateCancellable?.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Tests/AmplitudeTests/Plugins/NetworkConnectivityCheckerPluginTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// NetworkConnectivityCheckerPluginTests.swift | ||
// Amplitude-SwiftTests | ||
// | ||
// Created by Xinyi.Ye on 1/29/24. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import AmplitudeSwift | ||
|
||
final class NetworkConnectivityCheckerPluginTests: XCTestCase { | ||
private var mockPathCreation: MockPathCreation! | ||
private var plugin: NetworkConnectivityCheckerPlugin! | ||
private var amplitude: Amplitude! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
mockPathCreation = MockPathCreation() | ||
amplitude = Amplitude(configuration: Configuration(apiKey: "test-api-key")) | ||
plugin = NetworkConnectivityCheckerPlugin(pathCreation: mockPathCreation) | ||
plugin.setup(amplitude: amplitude) | ||
} | ||
|
||
func testNetworkBecomesOnline() { | ||
mockPathCreation.simulateNetworkChange(status: .satisfied) | ||
XCTAssertEqual(amplitude.configuration.offline, false) | ||
} | ||
|
||
func testNetworkBecomesOffline() { | ||
mockPathCreation.simulateNetworkChange(status: .unsatisfied) | ||
XCTAssertEqual(amplitude.configuration.offline, true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters