1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
import SwiftUI
import SwiftData
import Foundation
import UniformTypeIdentifiers
enum TrackerSelection: Hashable {
case bookmark(Bookmark)
case bookmarkServer(BookmarkServer)
case bonjourGroup
case bonjourServer(BonjourState.BonjourServer)
var server: Server? {
switch self {
case .bookmark(let b): b.server
case .bookmarkServer(let t): t.server
case .bonjourGroup: nil
case .bonjourServer(let b): b.server
}
}
}
struct TrackerView: View {
@Environment(\.colorScheme) private var colorScheme
@Environment(\.openWindow) private var openWindow
@Environment(\.controlActiveState) private var controlActiveState
@Environment(\.modelContext) private var modelContext
@State private var refreshing = false
@State private var trackerSheetPresented: Bool = false
@State private var trackerSheetBookmark: Bookmark? = nil
@State private var serverSheetBookmark: Bookmark? = nil
@State private var attemptedPrepopulate: Bool = false
@State private var fileDropActive = false
@State private var bookmarkExportActive = false
@State private var bookmarkExport: BookmarkDocument? = nil
@State private var expandedTrackers: Set<Bookmark> = []
@State private var trackerServers: [Bookmark: [BookmarkServer]] = [:]
@State private var loadingTrackers: Set<Bookmark> = []
@State private var fetchTasks: [Bookmark: Task<Void, Never>] = [:]
@State private var searchText: String = ""
@State private var isSearching = false
@Query(sort: \Bookmark.order) private var bookmarks: [Bookmark]
@Binding var selection: TrackerSelection?
private var filteredBookmarks: [Bookmark] {
guard !self.searchText.isEmpty else {
return self.bookmarks
}
let searchWords = self.searchText.lowercased().split(separator: " ").map(String.init)
return self.bookmarks.filter { bookmark in
// Always show tracker bookmarks (filter only their servers)
if bookmark.type == .tracker {
return true
}
// Filter server bookmarks by search text
return self.bookmarkMatchesSearch(bookmark, searchWords: searchWords)
}
}
private func bookmarkMatchesSearch(_ bookmark: Bookmark, searchWords: [String]) -> Bool {
let searchableText = "\(bookmark.name) \(bookmark.address)".lowercased()
// All search words must match
return searchWords.allSatisfy { word in
searchableText.contains(word)
}
}
private func filteredServers(for bookmark: Bookmark) -> [BookmarkServer] {
let servers = self.trackerServers[bookmark] ?? []
print("TrackerView.filteredServers: Looking up servers for \(bookmark.name), found \(servers.count) servers")
guard !self.searchText.isEmpty else {
return servers
}
let searchWords = self.searchText.lowercased().split(separator: " ").map(String.init)
return servers.filter { server in
let searchableText = "\(server.name ?? "") \(server.address) \(server.description ?? "")".lowercased()
// All search words must match
return searchWords.allSatisfy { word in
searchableText.contains(word)
}
}
}
var bonjourRowView: some View {
HStack(alignment: .center, spacing: 6) {
Button {
AppState.shared.bonjourState.isExpanded.toggle()
} label: {
Text(Image(systemName: AppState.shared.bonjourState.isExpanded ? "chevron.down" : "chevron.right"))
.bold()
.font(.system(size: 10))
.opacity(0.5)
.frame(alignment: .center)
}
.buttonStyle(.plain)
.frame(width: 10)
.padding(.leading, 4)
.padding(.trailing, 2)
Image(systemName: "bonjour")
.resizable()
.scaledToFit()
.symbolRenderingMode(.multicolor)
.frame(width: 16, height: 16, alignment: .center)
Text("Bonjour").bold().lineLimit(1).truncationMode(.tail)
if AppState.shared.bonjourState.isBrowsing {
ProgressView()
.controlSize(.mini)
}
Spacer(minLength: 0)
if AppState.shared.bonjourState.isExpanded && !AppState.shared.bonjourState.discoveredServers.isEmpty {
HStack(spacing: 4) {
Text(String(AppState.shared.bonjourState.discoveredServers.count))
SpinningGlobeView()
.fontWeight(.semibold)
.frame(width: 12, height: 12)
}
.padding(.horizontal, 6)
.padding(.vertical, 2)
.foregroundStyle(.secondary)
// .background(.quinary)
.clipShape(.capsule)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onChange(of: AppState.shared.bonjourState.isExpanded) { oldState, newState in
if newState {
AppState.shared.bonjourState.startBrowsing()
}
else {
AppState.shared.bonjourState.stopBrowsing()
}
}
}
var body: some View {
List(selection: $selection) {
ForEach(filteredBookmarks, id: \.self) { bookmark in
TrackerItemView(
bookmark: bookmark,
isExpanded: self.expandedTrackers.contains(bookmark),
isLoading: self.loadingTrackers.contains(bookmark),
count: self.trackerServers[bookmark]?.count ?? 0
) {
self.toggleExpanded(for: bookmark)
}
.onAppear {
if bookmark.autoconnect, let server = bookmark.server {
openWindow(id: "server", value: server)
}
}
.tag(TrackerSelection.bookmark(bookmark))
if bookmark.type == .tracker && self.expandedTrackers.contains(bookmark) {
ForEach(self.filteredServers(for: bookmark), id: \.self) { trackedServer in
TrackerBookmarkServerView(server: trackedServer)
.moveDisabled(true)
.deleteDisabled(true)
.tag(TrackerSelection.bookmarkServer(trackedServer))
.padding(.leading, 16 + 8 + 10)
}
}
}
.onMove { movedIndexes, destinationIndex in
Bookmark.move(movedIndexes, to: destinationIndex, context: modelContext)
}
.onDelete { deletedIndexes in
Bookmark.delete(at: deletedIndexes, context: modelContext)
}
self.bonjourRowView
.tag(TrackerSelection.bonjourGroup)
if AppState.shared.bonjourState.isExpanded {
ForEach(AppState.shared.bonjourState.discoveredServers, id: \.self) { record in
BonjourServerRow(server: record)
.tag(TrackerSelection.bonjourServer(record))
.padding(.leading, 16 + 8 + 10)
}
}
}
.onDeleteCommand {
switch self.selection {
case .bookmark(let bookmark):
Bookmark.delete(bookmark, context: modelContext)
default:
break
}
// if let bookmark = selection,
// bookmark.type != .temporary {
// Bookmark.delete(bookmark, context: modelContext)
// }
}
.environment(\.defaultMinListRowHeight, 34)
.listStyle(.inset)
.alternatingRowBackgrounds(.enabled)
.onChange(of: AppState.shared.cloudKitReady) {
if attemptedPrepopulate {
print("Tracker: Already attempted to prepopulate bookmarks")
return
}
print("Tracker: Prepopulating bookmarks")
attemptedPrepopulate = true
// Make sure default bookmarks are there when empty.
Bookmark.populateDefaults(context: modelContext)
}
.onAppear {
// Bookmark.deleteAll(context: modelContext)
}
.contextMenu(forSelectionType: TrackerSelection.self) { items in
if let item = items.first {
switch item {
case .bookmark(let bookmark):
self.bookmarkContextMenu(bookmark)
case .bookmarkServer(let server):
self.bookmarkServerContextMenu(server)
case .bonjourGroup:
EmptyView()
case .bonjourServer(let bonjourServer):
self.bonjourServerContextMenu(bonjourServer)
}
}
} primaryAction: { items in
guard let clickedItem = items.first else {
return
}
switch clickedItem {
case .bookmark(let bookmark):
if bookmark.type == .server {
if let s = bookmark.server {
openWindow(id: "server", value: s)
}
}
else if bookmark.type == .tracker {
if NSEvent.modifierFlags.contains(.option) {
trackerSheetBookmark = bookmark
}
else {
self.toggleExpanded(for: bookmark)
}
}
case .bookmarkServer(let bookmarkServer):
openWindow(id: "server", value: bookmarkServer.server)
case .bonjourGroup:
AppState.shared.bonjourState.isExpanded.toggle()
case .bonjourServer(let bonjourServer):
if let server = bonjourServer.server {
openWindow(id: "server", value: server)
}
}
}
.fileExporter(isPresented: $bookmarkExportActive, document: bookmarkExport, contentTypes: [.data], defaultFilename: "\(bookmarkExport?.bookmark.name ?? "Hotline Bookmark").hlbm", onCompletion: { result in
switch result {
case .success(let fileURL):
print("Hotline Bookmark: Successfully exported:", fileURL)
case .failure(let err):
print("Hotline Bookmark: Failed to export:", err)
}
bookmarkExport = nil
bookmarkExportActive = false
}, onCancellation: {})
.onKeyPress(.rightArrow) {
switch self.selection {
case .bookmark(let bookmark):
if bookmark.type == .tracker {
self.setExpanded(true, for: bookmark)
return .handled
}
default:
break
}
return .ignored
}
.onKeyPress(.leftArrow) {
switch self.selection {
case .bookmark(let bookmark):
if bookmark.type == .tracker {
self.setExpanded(false, for: bookmark)
return .handled
}
default:
break
}
return .ignored
}
.onDrop(of: [UTType.fileURL], isTargeted: $fileDropActive) { providers, dropPoint in
for provider in providers {
let _ = provider.loadDataRepresentation(for: UTType.fileURL) { dataRepresentation, err in
// HOTLINE CREATOR CODE: 1213484099
// HOTLINE BOOKMARK TYPE CODE: 1213489773
if let filePathData = dataRepresentation,
let filePath = String(data: filePathData, encoding: .utf8),
let fileURL = URL(string: filePath) {
print("Hotline Bookmark: Dropped from ", fileURL.path(percentEncoded: false))
DispatchQueue.main.async {
if let newBookmark = Bookmark(fileURL: fileURL) {
print("Hotline Bookmark: Added bookmark.")
Bookmark.add(newBookmark, context: modelContext)
}
else {
print("Hotline Bookmark: Failed to parse.")
}
}
}
}
}
return true
}
.sheet(item: $trackerSheetBookmark) { item in
TrackerBookmarkSheet(item)
}
.sheet(isPresented: $trackerSheetPresented) {
TrackerBookmarkSheet()
}
.sheet(item: $serverSheetBookmark) { item in
ServerBookmarkSheet(item)
}
.navigationTitle("Servers")
.toolbar {
if #available(macOS 26.0, *) {
ToolbarItem(placement: .navigation) {
self.hotlineLogoImage
}
.sharedBackgroundVisibility(.hidden)
}
else {
ToolbarItem(placement: .navigation) {
self.hotlineLogoImage
}
}
ToolbarItem(placement: .primaryAction) {
Button {
self.refreshing = true
self.refresh()
self.refreshing = false
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
}
.disabled(refreshing)
.help("Refresh Trackers")
}
ToolbarItem(placement: .primaryAction) {
Button {
trackerSheetPresented = true
} label: {
Label("Add Tracker", systemImage: "point.3.filled.connected.trianglepath.dotted")
}
.help("Add Tracker")
}
ToolbarItem(placement: .primaryAction) {
Button {
openWindow(id: "server")
} label: {
Label("Connect to Server", systemImage: "globe.americas.fill")
}
.help("Connect to Server")
}
}
.onOpenURL(perform: { url in
if let s = Server(url: url) {
openWindow(id: "server", value: s)
}
})
.searchable(text: $searchText, isPresented: $isSearching, placement: .automatic, prompt: "Search")
.background(Button("", action: { isSearching = true }).keyboardShortcut("f").hidden())
}
private var hotlineLogoImage: some View {
Image("Hotline")
.resizable()
.renderingMode(.template)
.scaledToFit()
.foregroundColor(Color(hex: 0xE10000))
.frame(width: 9)
.opacity(controlActiveState == .inactive ? 0.5 : 1.0)
}
@ViewBuilder
func bookmarkServerContextMenu(_ server: BookmarkServer) -> some View {
Button {
NSPasteboard.general.clearContents()
let displayAddress = (server.port == HotlinePorts.DefaultServerPort) ? server.address : "\(server.address):\(server.port)"
NSPasteboard.general.setString("hotline://\(displayAddress)", forType: .string)
} label: {
Label("Copy Link", systemImage: "link")
}
Button {
NSPasteboard.general.clearContents()
let displayAddress = (server.port == HotlinePorts.DefaultServerPort) ? server.address : "\(server.address):\(server.port)"
NSPasteboard.general.setString(displayAddress, forType: .string)
} label: {
Label("Copy Address", systemImage: "doc.on.doc")
}
Divider()
Button {
let newBookmark = Bookmark(type: .server, name: server.name ?? server.address, address: server.address, port: server.port, login: nil, password: nil)
Bookmark.add(newBookmark, context: modelContext)
} label: {
Label("Bookmark", systemImage: "bookmark")
}
}
@ViewBuilder
func bookmarkContextMenu(_ bookmark: Bookmark) -> some View {
Button {
let linkString: String = switch bookmark.type {
case .tracker: "hotlinetracker://\(bookmark.displayAddress)"
case .server: "hotline://\(bookmark.displayAddress)"
}
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(linkString, forType: .string)
} label: {
Label("Copy Link", systemImage: "link")
}
Button {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(bookmark.displayAddress, forType: .string)
} label: {
Label("Copy Address", systemImage: "doc.on.doc")
}
Divider()
if bookmark.type == .tracker {
Button {
trackerSheetBookmark = bookmark
} label: {
Label("Edit Tracker...", systemImage: "pencil")
}
}
if bookmark.type == .server {
Button {
serverSheetBookmark = bookmark
} label: {
Label("Edit Bookmark...", systemImage: "pencil")
}
Button {
bookmarkExport = BookmarkDocument(bookmark: bookmark)
bookmarkExportActive = true
} label: {
Label("Export Bookmark...", systemImage: "square.and.arrow.down")
}
}
Divider()
Button {
Bookmark.delete(bookmark, context: modelContext)
} label: {
if bookmark.type == .tracker {
Label("Delete Tracker", systemImage: "xmark")
}
else {
Label("Delete Bookmark", systemImage: "bookmark.slash")
}
}
}
@ViewBuilder
func bonjourServerContextMenu(_ bonjourServer: BonjourState.BonjourServer) -> some View {
Button {
guard let server = bonjourServer.server else {
return
}
NSPasteboard.general.clearContents()
let displayAddress = (server.port == HotlinePorts.DefaultServerPort) ? server.address : "\(server.address):\(server.port)"
NSPasteboard.general.setString("hotline://\(displayAddress)", forType: .string)
} label: {
Label("Copy Link", systemImage: "link")
}
Button {
guard let server = bonjourServer.server else {
return
}
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(server.displayAddress, forType: .string)
} label: {
Label("Copy Address", systemImage: "doc.on.doc")
}
Divider()
Button {
guard let server = bonjourServer.server else {
return
}
let newBookmark = Bookmark(type: .server, name: server.name ?? server.address, address: server.address, port: server.port, login: nil, password: nil)
Bookmark.add(newBookmark, context: modelContext)
} label: {
Label("Bookmark", systemImage: "bookmark")
}
}
func refresh() {
// When a tracker is selected, refresh only that tracker.
if let trackerSelection = self.selection {
switch trackerSelection {
case .bookmark(let bookmark):
if bookmark.type == .tracker {
if self.expandedTrackers.contains(bookmark) {
// Already expanded, cancel old fetch and start new one
self.fetchTasks[bookmark]?.cancel()
let task = Task {
await self.fetchServers(for: bookmark)
}
self.fetchTasks[bookmark] = task
} else {
// Not expanded, expand it (which also fetches)
self.setExpanded(true, for: bookmark)
}
return
}
break
default:
break
}
}
// Otherwise refresh/expand all trackers.
for bookmark in self.bookmarks {
if bookmark.type == .tracker {
if self.expandedTrackers.contains(bookmark) {
// Already expanded, cancel old fetch and start new one
self.fetchTasks[bookmark]?.cancel()
let task = Task {
await self.fetchServers(for: bookmark)
}
self.fetchTasks[bookmark] = task
} else {
// Not expanded, expand it (which also fetches)
self.setExpanded(true, for: bookmark)
}
}
}
}
func toggleExpanded(for bookmark: Bookmark) {
guard bookmark.type == .tracker else { return }
self.setExpanded(!self.expandedTrackers.contains(bookmark), for: bookmark)
}
func setExpanded(_ expanded: Bool, for bookmark: Bookmark) {
guard bookmark.type == .tracker else { return }
if expanded && !self.expandedTrackers.contains(bookmark) {
self.expandedTrackers.insert(bookmark)
let task = Task {
await self.fetchServers(for: bookmark)
}
self.fetchTasks[bookmark] = task
} else if !expanded && self.expandedTrackers.contains(bookmark) {
// Cancel ongoing fetch and clear data
self.fetchTasks[bookmark]?.cancel()
self.fetchTasks[bookmark] = nil
self.expandedTrackers.remove(bookmark)
self.trackerServers[bookmark] = nil
self.loadingTrackers.remove(bookmark)
}
}
private func fetchServers(for bookmark: Bookmark) async {
print("TrackerView.fetchServers: Starting fetch for bookmark: \(bookmark.name)")
self.loadingTrackers.insert(bookmark)
let servers = await bookmark.fetchServers()
print("TrackerView.fetchServers: Got \(servers.count) servers from bookmark.fetchServers()")
await MainActor.run {
print("TrackerView.fetchServers: Assigning \(servers.count) servers to trackerServers[\(bookmark.name)]")
self.trackerServers[bookmark] = servers
self.loadingTrackers.remove(bookmark)
self.fetchTasks[bookmark] = nil // Clean up completed task
print("TrackerView.fetchServers: trackerServers now has \(self.trackerServers.count) entries")
print("TrackerView.fetchServers: Verification - trackerServers[\(bookmark.name)] now has \(self.trackerServers[bookmark]?.count ?? -1) servers")
}
}
}
#if DEBUG
private struct TrackerViewPreview: View {
@State var selection: TrackerSelection? = nil
var body: some View {
TrackerView(selection: $selection)
}
}
#Preview {
TrackerViewPreview()
}
#endif
|