aboutsummaryrefslogtreecommitdiff
path: root/Hotline
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2023-12-08 10:04:55 -0800
committerDustin Mierau <dustin@mierau.me>2023-12-08 10:04:55 -0800
commit537ed932a4c8d639089f276e322de4e946b0d26b (patch)
tree1d3604a5f777be87f2d3d405ddfe5725c687b99f /Hotline
parent767eb58da13d83aa321fff12c97fe433cd8fa734 (diff)
Starting to move to async/await model so the client code is no longer burdoned by observation and isn't bound to this app.
Diffstat (limited to 'Hotline')
-rw-r--r--Hotline/Hotline/HotlineClient.swift1
-rw-r--r--Hotline/Hotline/HotlineTrackerClient.swift34
-rw-r--r--Hotline/HotlineApp.swift2
-rw-r--r--Hotline/Models/Hotline.swift21
-rw-r--r--Hotline/Models/Server.swift16
-rw-r--r--Hotline/Models/Tracker.swift20
-rw-r--r--Hotline/Views/TrackerView.swift6
7 files changed, 94 insertions, 6 deletions
diff --git a/Hotline/Hotline/HotlineClient.swift b/Hotline/Hotline/HotlineClient.swift
index 36872cb..f545c3c 100644
--- a/Hotline/Hotline/HotlineClient.swift
+++ b/Hotline/Hotline/HotlineClient.swift
@@ -377,7 +377,6 @@ class HotlineClient {
DispatchQueue.main.async {
if var pf = parentFile {
- print("\(pf.name) <= \(files.count) files")
pf.files = files
}
else {
diff --git a/Hotline/Hotline/HotlineTrackerClient.swift b/Hotline/Hotline/HotlineTrackerClient.swift
index c437ee6..f93949e 100644
--- a/Hotline/Hotline/HotlineTrackerClient.swift
+++ b/Hotline/Hotline/HotlineTrackerClient.swift
@@ -28,8 +28,8 @@ class HotlineTrackerClient {
0x00, 0x01 // Version
])
- @ObservationIgnored private let serverAddress: NWEndpoint.Host
- @ObservationIgnored private let serverPort: NWEndpoint.Port
+ @ObservationIgnored private var serverAddress: NWEndpoint.Host
+ @ObservationIgnored private var serverPort: NWEndpoint.Port
@ObservationIgnored private var connection: NWConnection?
@ObservationIgnored private var bytes = Data()
@ObservationIgnored private var maxDataLength: Int = 0
@@ -39,17 +39,45 @@ class HotlineTrackerClient {
var connectionStatus: HotlineTrackerStatus = .disconnected
var servers: [HotlineServer] = []
+ init() {
+ let t = HotlineTracker("hltracker.com")
+ self.tracker = t
+ self.serverAddress = NWEndpoint.Host(t.address)
+ self.serverPort = NWEndpoint.Port(rawValue: t.port)!
+ }
+
init(tracker: HotlineTracker) {
self.tracker = tracker
self.serverAddress = NWEndpoint.Host(tracker.address)
self.serverPort = NWEndpoint.Port(rawValue: tracker.port)!
}
- func fetch(_ callback: (() -> Void)? = nil) {
+ func fetch(callback: (() -> Void)? = nil) {
self.reset()
self.connect(callback)
}
+ func fetch2(address: String, port: Int, callback: (([Server]) -> Void)? = nil) {
+ self.serverAddress = NWEndpoint.Host(address)
+ self.serverPort = NWEndpoint.Port(rawValue: UInt16(port))!
+
+ self.reset()
+ self.connect { [weak self] in
+ var allServers: [Server] = []
+
+ if let servers = self?.servers {
+ for server in servers {
+ let s = Server(name: server.name!, description: server.description, address: server.address, port: Int(server.port))
+ allServers.append(s)
+ }
+ }
+
+ DispatchQueue.main.async {
+ callback?(allServers)
+ }
+ }
+ }
+
private func reset() {
self.maxDataLength = 0
self.serverCount = 0
diff --git a/Hotline/HotlineApp.swift b/Hotline/HotlineApp.swift
index 3e714d8..82fa7d4 100644
--- a/Hotline/HotlineApp.swift
+++ b/Hotline/HotlineApp.swift
@@ -20,6 +20,8 @@ struct HotlineApp: App {
@State private var hotline = HotlineClient()
@State private var tracker = HotlineTrackerClient(tracker: HotlineTracker("hltracker.com"))
+ private var model = Hotline(trackerClient: HotlineTrackerClient())
+
var body: some Scene {
WindowGroup {
diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift
new file mode 100644
index 0000000..70f68a9
--- /dev/null
+++ b/Hotline/Models/Hotline.swift
@@ -0,0 +1,21 @@
+import SwiftUI
+
+@Observable final class Hotline {
+ let trackerClient: HotlineTrackerClient
+
+
+
+ init(trackerClient: HotlineTrackerClient) {
+ self.trackerClient = trackerClient
+ }
+
+ @MainActor func getServers(address: String, port: Int) async {
+ let fetchedServers = await withCheckedContinuation { [weak self] continuation in
+ self?.trackerClient.fetch() {
+ continuation.resume(returning: [])
+ }
+ }
+
+
+ }
+}
diff --git a/Hotline/Models/Server.swift b/Hotline/Models/Server.swift
new file mode 100644
index 0000000..c2063ea
--- /dev/null
+++ b/Hotline/Models/Server.swift
@@ -0,0 +1,16 @@
+import SwiftUI
+
+@Observable final class Server {
+ let name: String
+ let description: String?
+ let users: Int = 0
+ let address: String
+ let port: Int
+
+ init(name: String, description: String?, address: String, port: Int) {
+ self.name = name
+ self.description = description
+ self.address = address
+ self.port = port
+ }
+}
diff --git a/Hotline/Models/Tracker.swift b/Hotline/Models/Tracker.swift
new file mode 100644
index 0000000..c9dfb5c
--- /dev/null
+++ b/Hotline/Models/Tracker.swift
@@ -0,0 +1,20 @@
+import SwiftUI
+
+@Observable final class Tracker {
+ let service: HotlineTrackerClient
+ let address: String
+ let port: Int
+ var servers: [Server] = []
+
+ init(address: String, port: Int = 5498, service: HotlineTrackerClient) {
+ self.address = address
+ self.port = port
+ self.service = service
+ }
+
+ func fetchServers() {
+ self.service.fetch2(address: self.address, port: self.port) { servers in
+ self.servers = servers
+ }
+ }
+}
diff --git a/Hotline/Views/TrackerView.swift b/Hotline/Views/TrackerView.swift
index c8d0f2e..1c61cf4 100644
--- a/Hotline/Views/TrackerView.swift
+++ b/Hotline/Views/TrackerView.swift
@@ -7,9 +7,12 @@ struct TrackerView: View {
@Environment(HotlineState.self) private var appState
@Environment(HotlineClient.self) private var hotline
- @Environment(HotlineTrackerClient.self) private var tracker
+ @Environment(HotlineTrackerClient.self) private var trackerClient
+ @Environment(Hotline.self) private var model: Hotline
@Environment(\.colorScheme) var colorScheme
+// @State private var tracker = Tracker(address: "hltracker.com", service: trackerService)
+
@State private var selectedServer: HotlineServer?
@State var scrollOffset: CGFloat = CGFloat.zero
@@ -32,7 +35,6 @@ struct TrackerView: View {
case .loggingIn:
return 0.5
case .loggedIn:
-
return 1.0
}
}