-
-
Notifications
You must be signed in to change notification settings - Fork 795
Add Wi-Fi connectivity live activity #1305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheBestVivBoy
wants to merge
4
commits into
TheBoredTeam:dev
Choose a base branch
from
TheBestVivBoy:vivan/network-live-activity
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ enum SneakContentType { | |
| case music | ||
| case mic | ||
| case battery | ||
| case network | ||
| case download | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
boringNotch/components/Live activities/BoringNetwork.swift
This file contains hidden or 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 @@ | ||
| import SwiftUI | ||
|
|
||
| struct BoringNetworkActivityView: View { | ||
| var statusText: String | ||
| var symbolName: String | ||
| var isConnected: Bool | ||
| var textWidth: CGFloat | ||
| var centerWidth: CGFloat | ||
|
|
||
| var body: some View { | ||
| HStack(spacing: 0) { | ||
| HStack { | ||
| Text(statusText) | ||
| .font(.subheadline) | ||
| .foregroundStyle(.white) | ||
| .lineLimit(1) | ||
| .minimumScaleFactor(0.9) | ||
| } | ||
| .frame(width: textWidth, alignment: .leading) | ||
|
|
||
| Rectangle() | ||
| .fill(.black) | ||
| .frame(width: centerWidth) | ||
|
|
||
| HStack { | ||
| Image(systemName: symbolName) | ||
| .foregroundStyle(isConnected ? .white : .gray) | ||
| .frame(width: 18, height: 18) | ||
| .contentTransition(.interpolate) | ||
| } | ||
| .frame(width: 76, alignment: .trailing) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,96 @@ | ||
| import Foundation | ||
| import Network | ||
|
|
||
| @MainActor | ||
| final class NetworkActivityManager { | ||
|
|
||
| struct NetworkState: Equatable { | ||
| enum Status: Equatable { | ||
| case connected | ||
| case disconnected | ||
| case requiresConnection | ||
| } | ||
|
|
||
| let status: Status | ||
| let isConstrained: Bool | ||
| let isExpensive: Bool | ||
| } | ||
|
|
||
| enum NetworkEvent { | ||
| case stateChanged(NetworkState, isInitial: Bool) | ||
| } | ||
|
|
||
| static let shared = NetworkActivityManager() | ||
|
|
||
| private let monitor = NWPathMonitor(requiredInterfaceType: .wifi) | ||
| private let monitorQueue = DispatchQueue(label: "com.boringnotch.network-activity") | ||
| private var observers: [Int: (NetworkEvent) -> Void] = [:] | ||
| private var nextObserverId = 0 | ||
| private var latestState: NetworkState? | ||
|
|
||
| private init() { | ||
| startMonitoring() | ||
| } | ||
|
|
||
| func addObserver(_ observer: @escaping (NetworkEvent) -> Void) -> Int { | ||
| let id = nextObserverId | ||
| nextObserverId += 1 | ||
| observers[id] = observer | ||
|
|
||
| if let latestState { | ||
| observer(.stateChanged(latestState, isInitial: true)) | ||
| } | ||
|
|
||
| return id | ||
| } | ||
|
|
||
| func removeObserver(byId id: Int) { | ||
| observers.removeValue(forKey: id) | ||
| } | ||
|
|
||
| private func startMonitoring() { | ||
| monitor.pathUpdateHandler = { [weak self] path in | ||
| Task { @MainActor in | ||
| self?.handlePathUpdate(path) | ||
| } | ||
| } | ||
| monitor.start(queue: monitorQueue) | ||
| } | ||
|
|
||
| private func handlePathUpdate(_ path: NWPath) { | ||
| let newState = normalizedState(from: path) | ||
| let isInitial = latestState == nil | ||
|
|
||
| guard isInitial || latestState != newState else { return } | ||
|
|
||
| latestState = newState | ||
| notifyObservers(event: .stateChanged(newState, isInitial: isInitial)) | ||
| } | ||
|
|
||
| private func normalizedState(from path: NWPath) -> NetworkState { | ||
| let status: NetworkState.Status | ||
|
|
||
| switch path.status { | ||
| case .satisfied: | ||
| status = path.usesInterfaceType(.wifi) ? .connected : .disconnected | ||
| case .requiresConnection: | ||
| status = .requiresConnection | ||
| case .unsatisfied: | ||
| status = .disconnected | ||
| @unknown default: | ||
| status = .disconnected | ||
| } | ||
|
|
||
| return NetworkState( | ||
| status: status, | ||
| isConstrained: path.isConstrained, | ||
| isExpensive: path.isExpensive | ||
| ) | ||
| } | ||
|
|
||
| private func notifyObservers(event: NetworkEvent) { | ||
| observers.values.forEach { observer in | ||
| observer(event) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,111 @@ | ||
| import Foundation | ||
| import SwiftUI | ||
|
|
||
| @MainActor | ||
| class NetworkStatusViewModel: ObservableObject { | ||
|
|
||
| private let coordinator = BoringViewCoordinator.shared | ||
|
|
||
| @Published private(set) var state = NetworkActivityManager.NetworkState( | ||
| status: .disconnected, | ||
| isConstrained: false, | ||
| isExpensive: false | ||
| ) | ||
|
|
||
| private let networkManager = NetworkActivityManager.shared | ||
| private var networkManagerId: Int? | ||
|
|
||
| static let shared = NetworkStatusViewModel() | ||
|
|
||
| private init() { | ||
| setupMonitor() | ||
| } | ||
|
|
||
| var statusText: String { | ||
| switch state.status { | ||
| case .connected: | ||
| if state.isConstrained { | ||
| return "Limited Wi-Fi" | ||
| } | ||
| if state.isExpensive { | ||
| return "Hotspot Connected" | ||
| } | ||
| return "Wi-Fi Connected" | ||
| case .disconnected: | ||
| return "Wi-Fi Disconnected" | ||
| case .requiresConnection: | ||
| return "Wi-Fi Unavailable" | ||
| } | ||
| } | ||
|
|
||
| var symbolName: String { | ||
| switch state.status { | ||
| case .connected: | ||
| return "wifi" | ||
| case .disconnected, .requiresConnection: | ||
| return "wifi.slash" | ||
| } | ||
| } | ||
|
|
||
| var isConnected: Bool { | ||
| state.status == .connected | ||
| } | ||
|
|
||
| var preferredNotificationWidth: CGFloat { | ||
| textWidth + 76 + 16 | ||
| } | ||
|
|
||
| var textWidth: CGFloat { | ||
| switch state.status { | ||
| case .connected: | ||
| if state.isConstrained { | ||
| return 185 | ||
| } | ||
| if state.isExpensive { | ||
| return 215 | ||
| } | ||
| return 190 | ||
| case .disconnected: | ||
| return 235 | ||
| case .requiresConnection: | ||
| return 210 | ||
| } | ||
| } | ||
|
|
||
| private func setupMonitor() { | ||
| networkManagerId = networkManager.addObserver { [weak self] event in | ||
| self?.handleNetworkEvent(event) | ||
| } | ||
| } | ||
|
|
||
| private func handleNetworkEvent(_ event: NetworkActivityManager.NetworkEvent) { | ||
| switch event { | ||
| case .stateChanged(let newState, let isInitial): | ||
| if isInitial { | ||
| state = newState | ||
| return | ||
| } | ||
|
|
||
| withAnimation { | ||
| state = newState | ||
| } | ||
|
|
||
| notifyImportantChangeStatus() | ||
| } | ||
| } | ||
|
|
||
| private func notifyImportantChangeStatus(delay: Double = 0.0) { | ||
| Task { | ||
| try? await Task.sleep(for: .seconds(delay)) | ||
| coordinator.toggleExpandingView(status: true, type: .network) | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| guard let networkManagerId else { return } | ||
| let manager = networkManager | ||
| Task { @MainActor in | ||
| manager.removeObserver(byId: networkManagerId) | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the network activity is shown,
ContentViewusespreferredNotificationWidthas the full chin width, butBoringNetworkActivityViewalso renders a center spacer ofvm.closedNotchSize.width + 10between the text and icon. On the default non-notch width this makes the rendered activity roughlytextWidth + 195 + 76, while the chin is onlytextWidth + 76 + 16, so withhideTitleBarenabled the lower chin area is substantially narrower than the visible activity and leaves the right side without the expected black fill/hit area. Include the center spacer in this width calculation or compute the width whereclosedNotchSizeis available.Useful? React with 👍 / 👎.