Skip to content

Commit

Permalink
feature: NetworkTracking 모듈
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihyun247 committed Aug 20, 2024
1 parent b68d8a0 commit cc92ce2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public enum Domain: String, Modulable {
case UserService
case CatService
case PomodoroService
case NetworkTracking
}
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()
}
20 changes: 20 additions & 0 deletions Projects/Domain/NetworkTracking/Project.swift
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 Projects/Domain/NetworkTracking/Sources/NetworkTracking.swift
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()
}
)
}
}

0 comments on commit cc92ce2

Please sign in to comment.