-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
82 additions
and
0 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
24 changes: 24 additions & 0 deletions
24
Projects/Domain/NetworkTracking/Interface/NetworkTrackingInterface.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,24 @@ | ||
// | ||
// NetworkTrackingInterface.swift | ||
// NetworkTracking | ||
// | ||
// Created by 김지현 on 8/21/24. | ||
// | ||
|
||
import Foundation | ||
import Network | ||
|
||
import Dependencies | ||
import DependenciesMacros | ||
|
||
@DependencyClient | ||
public struct NetworkTracking { | ||
public var start: @Sendable () -> Void | ||
public var updateNetworkConnected: @Sendable () -> AsyncThrowingStream<Bool, Error> = { .never } | ||
public var cancel: @Sendable () -> Void | ||
} | ||
|
||
extension NetworkTracking: TestDependencyKey { | ||
public static let previewValue = Self() | ||
public static let testValue = Self() | ||
} |
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,20 @@ | ||
import ProjectDescription | ||
import ProjectDescriptionHelpers | ||
|
||
@_spi(Domain) | ||
@_spi(Core) | ||
import DependencyPlugin | ||
|
||
let project: Project = .makeTMABasedProject( | ||
module: Domain.NetworkTracking, | ||
scripts: [], | ||
targets: [ | ||
.sources, | ||
.interface | ||
], | ||
dependencies: [ | ||
.interface: [ | ||
.dependency(rootModule: Core.self) | ||
] | ||
] | ||
) |
37 changes: 37 additions & 0 deletions
37
Projects/Domain/NetworkTracking/Sources/NetworkTracking.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,37 @@ | ||
// | ||
// NetworkTracking.swift | ||
// NetworkTracking | ||
// | ||
// Created by 김지현 on 8/21/24. | ||
// | ||
|
||
import Foundation | ||
import Network | ||
|
||
import NetworkTrackingInterface | ||
|
||
import Dependencies | ||
|
||
extension NetworkTracking { | ||
public static let liveValue: NetworkTracking = .live() | ||
|
||
public static func live() -> NetworkTracking { | ||
let networkMonitor = NWPathMonitor() | ||
return NetworkTracking( | ||
start: { | ||
networkMonitor.start(queue: DispatchQueue.global()) | ||
}, | ||
updateNetworkConnected: { | ||
return AsyncThrowingStream<Bool, Error> { continuation in | ||
networkMonitor.pathUpdateHandler = { path in | ||
let isConnected = path.status == .satisfied ? true : false | ||
continuation.yield(isConnected) | ||
} | ||
} | ||
}, | ||
cancel: { | ||
networkMonitor.cancel() | ||
} | ||
) | ||
} | ||
} |