diff --git a/Plugins/DependencyPlugin/ProjectDescriptionHelpers/Module/Domain.swift b/Plugins/DependencyPlugin/ProjectDescriptionHelpers/Module/Domain.swift index 24044ad..42e269d 100644 --- a/Plugins/DependencyPlugin/ProjectDescriptionHelpers/Module/Domain.swift +++ b/Plugins/DependencyPlugin/ProjectDescriptionHelpers/Module/Domain.swift @@ -15,4 +15,5 @@ public enum Domain: String, Modulable { case UserService case CatService case PomodoroService + case NetworkTracking } diff --git a/Projects/Domain/NetworkTracking/Interface/NetworkTrackingInterface.swift b/Projects/Domain/NetworkTracking/Interface/NetworkTrackingInterface.swift new file mode 100644 index 0000000..43abfe4 --- /dev/null +++ b/Projects/Domain/NetworkTracking/Interface/NetworkTrackingInterface.swift @@ -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 = { .never } + public var cancel: @Sendable () -> Void +} + +extension NetworkTracking: TestDependencyKey { + public static let previewValue = Self() + public static let testValue = Self() +} diff --git a/Projects/Domain/NetworkTracking/Project.swift b/Projects/Domain/NetworkTracking/Project.swift new file mode 100644 index 0000000..7058008 --- /dev/null +++ b/Projects/Domain/NetworkTracking/Project.swift @@ -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) + ] + ] +) diff --git a/Projects/Domain/NetworkTracking/Sources/NetworkTracking.swift b/Projects/Domain/NetworkTracking/Sources/NetworkTracking.swift new file mode 100644 index 0000000..dded881 --- /dev/null +++ b/Projects/Domain/NetworkTracking/Sources/NetworkTracking.swift @@ -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 { continuation in + networkMonitor.pathUpdateHandler = { path in + let isConnected = path.status == .satisfied ? true : false + continuation.yield(isConnected) + } + } + }, + cancel: { + networkMonitor.cancel() + } + ) + } +}