-
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.
Merge pull request #33 from GoodRequest/feature/debugman
feat: Debugman Integration
- Loading branch information
Showing
13 changed files
with
486 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// ConnectionsManager.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 21/05/2024. | ||
// | ||
|
||
import Foundation | ||
import MultipeerConnectivity | ||
|
||
final class ConnectionsManager: NSObject, ObservableObject { | ||
|
||
let session: MCSession | ||
let browser: MCNearbyServiceBrowser | ||
|
||
@Published var connectedPeers: [MCPeerID] = [] | ||
|
||
override init() { | ||
let appName = Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String | ||
let peer = MCPeerID( | ||
displayName: UIDevice.current.name + " - " + (appName ?? "") | ||
) | ||
|
||
browser = MCNearbyServiceBrowser( | ||
peer: peer, | ||
serviceType: "Debugman" | ||
) | ||
session = MCSession( | ||
peer: peer, | ||
securityIdentity: nil, | ||
encryptionPreference: .none | ||
) | ||
super.init() | ||
|
||
session.delegate = self | ||
} | ||
|
||
func cancelConnection(to peer: MCPeerID) { | ||
session.cancelConnectPeer(peer) | ||
} | ||
|
||
func sendToAllPeers(message: String) { | ||
guard !session.connectedPeers.isEmpty else { return } | ||
|
||
let data = Data(message.utf8) | ||
do { | ||
try session.send(data, toPeers: session.connectedPeers, with: .reliable) | ||
} catch { | ||
print("error sending data to peers: \(error)") | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
extension ConnectionsManager: MCSessionDelegate { | ||
|
||
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) { | ||
switch state { | ||
case .notConnected: | ||
print("not connected to peer: \(peerID.displayName)") | ||
|
||
case .connecting: | ||
print("connecting to peer: \(peerID.displayName)") | ||
|
||
case .connected: | ||
print("connected to peer: \(peerID.displayName)") | ||
|
||
@unknown default: | ||
print("unknown state: \(state)") | ||
} | ||
|
||
DispatchQueue.main.async { [weak self] in | ||
self?.connectedPeers = session.connectedPeers | ||
} | ||
} | ||
|
||
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { | ||
print("received data from peer: \(peerID)") | ||
} | ||
|
||
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { | ||
print("received stream from peer: \(peerID)") | ||
} | ||
|
||
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) { | ||
print("started receiving resource from peer: \(peerID)") | ||
} | ||
|
||
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?) { | ||
print("finished receiving resource from peer: \(peerID)") | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
Sources/AppDebugMode/Providers/ProxySettingsProvider.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,40 @@ | ||
// | ||
// ProxySettingsProvider.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 28/05/2024. | ||
// | ||
|
||
import Foundation | ||
import GoodPersistence | ||
|
||
public final class ProxySettingsProvider { | ||
|
||
@UserDefaultValue("proxyIpAdress", defaultValue: "") | ||
var proxyIpAdress: String | ||
|
||
@UserDefaultValue("proxyPort", defaultValue: 8080) | ||
var proxyPort: Int | ||
|
||
public static let shared = ProxySettingsProvider() | ||
|
||
public var urlSessionConfiguration: URLSessionConfiguration { | ||
let urlSessionConfig = URLSessionConfiguration.default | ||
urlSessionConfig.connectionProxyDictionary = [AnyHashable: Any]() | ||
urlSessionConfig.connectionProxyDictionary?[kCFNetworkProxiesHTTPEnable as String] = 1 | ||
urlSessionConfig.connectionProxyDictionary?[kCFNetworkProxiesHTTPProxy as String] = proxyIpAdress | ||
urlSessionConfig.connectionProxyDictionary?[kCFNetworkProxiesHTTPPort as String] = proxyPort | ||
urlSessionConfig.connectionProxyDictionary?["HTTPSProxy"] = proxyIpAdress | ||
urlSessionConfig.connectionProxyDictionary?["HTTPSPort"] = proxyPort | ||
|
||
return urlSessionConfig | ||
} | ||
|
||
func updateProxySettings(ipAddress: String, port: Int) { | ||
proxyIpAdress = ipAddress | ||
proxyPort = port | ||
|
||
exit(0) | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
Sources/AppDebugMode/Screens/ConnectionsSettingsView/ConnectedPeers/ConnectedPeersView.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,83 @@ | ||
// | ||
// ConnectedPeersView.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 28/05/2024. | ||
// | ||
|
||
import SwiftUI | ||
import MultipeerConnectivity | ||
|
||
struct ConnectedPeersView: View { | ||
|
||
@State var isBrowserPresented: Bool = false | ||
@EnvironmentObject var connectionsManager: ConnectionsManager | ||
|
||
var body: some View { | ||
Group { | ||
if connectionsManager.connectedPeers.isEmpty { | ||
Text("No connected peers") | ||
.foregroundColor(AppDebugColors.textPrimary) | ||
} | ||
|
||
ForEach(connectionsManager.connectedPeers, id: \.displayName) { connectedPeer in | ||
Text(connectedPeer.displayName) | ||
|
||
.foregroundColor(AppDebugColors.textPrimary) | ||
} | ||
.onDelete { offset in | ||
for index in offset { | ||
connectionsManager.cancelConnection(to: connectionsManager.connectedPeers[index]) | ||
} | ||
} | ||
|
||
ButtonFilled(text: "Open browser") { | ||
isBrowserPresented = true | ||
} | ||
.sheet(isPresented: $isBrowserPresented) { | ||
BrowserRepresentableViewController( | ||
browser: connectionsManager.browser, | ||
session: connectionsManager.session | ||
) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// MARK: - BrowserRepresentableViewController | ||
|
||
private struct BrowserRepresentableViewController: UIViewControllerRepresentable { | ||
|
||
let browser: MCNearbyServiceBrowser | ||
let session: MCSession | ||
|
||
func makeUIViewController(context: Context) -> UIViewController { | ||
let viewController = MCBrowserViewController( | ||
browser: browser, | ||
session: session | ||
) | ||
viewController.delegate = context.coordinator | ||
|
||
return viewController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator() | ||
} | ||
|
||
final class Coordinator: NSObject, MCBrowserViewControllerDelegate { | ||
|
||
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) { | ||
browserViewController.dismiss(animated: true) | ||
} | ||
|
||
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) { | ||
browserViewController.dismiss(animated: true) | ||
} | ||
|
||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
Sources/AppDebugMode/Screens/ConnectionsSettingsView/ConnectionsSettingsView.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,50 @@ | ||
// | ||
// ConnectionsSettingsView.swift | ||
// | ||
// | ||
// Created by Matus Klasovity on 21/05/2024. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ConnectionsSettingsView: View { | ||
|
||
var body: some View { | ||
List { | ||
connectedPeersSection() | ||
proxySettingsSection() | ||
} | ||
.navigationTitle("Connections") | ||
.listStyle(.insetGrouped) | ||
.listBackgroundColor(AppDebugColors.backgroundPrimary, for: .insetGrouped) | ||
} | ||
|
||
} | ||
|
||
// MARK: - Components | ||
|
||
private extension ConnectionsSettingsView { | ||
|
||
func connectedPeersSection() -> some View { | ||
Section { | ||
ConnectedPeersView() | ||
.listRowSeparatorColor(AppDebugColors.primary, for: .insetGrouped) | ||
.listRowBackground(AppDebugColors.backgroundSecondary) | ||
} header: { | ||
Text("Connected peers") | ||
.foregroundColor(AppDebugColors.textSecondary) | ||
} | ||
} | ||
|
||
func proxySettingsSection() -> some View { | ||
Section { | ||
ProxySettingsView() | ||
.listRowBackground(AppDebugColors.backgroundSecondary) | ||
.foregroundColor(AppDebugColors.textPrimary) | ||
} header: { | ||
Text("Proxy settings") | ||
.foregroundColor(AppDebugColors.textSecondary) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.