diff options
Diffstat (limited to 'Hotline/macOS')
| -rw-r--r-- | Hotline/macOS/MessageBoardEditorView.swift | 2 | ||||
| -rw-r--r-- | Hotline/macOS/ServerView.swift | 40 | ||||
| -rw-r--r-- | Hotline/macOS/TrackerView.swift | 17 |
3 files changed, 52 insertions, 7 deletions
diff --git a/Hotline/macOS/MessageBoardEditorView.swift b/Hotline/macOS/MessageBoardEditorView.swift index 029616f..474384e 100644 --- a/Hotline/macOS/MessageBoardEditorView.swift +++ b/Hotline/macOS/MessageBoardEditorView.swift @@ -20,7 +20,7 @@ struct MessageBoardEditorView: View { let cleanedText = text.trimmingCharacters(in: .whitespacesAndNewlines) - await model.postToMessageBoard(text: cleanedText) + model.postToMessageBoard(text: cleanedText) let _ = await model.getMessageBoard() // let success = await model.postNewsArticle(title: title, body: text, at: path, parentID: parentID) diff --git a/Hotline/macOS/ServerView.swift b/Hotline/macOS/ServerView.swift index e9f4a96..1e53e49 100644 --- a/Hotline/macOS/ServerView.swift +++ b/Hotline/macOS/ServerView.swift @@ -143,6 +143,8 @@ struct ServerView: View { @Environment(\.scenePhase) private var scenePhase @Environment(\.modelContext) private var modelContext + @State private var reconnectTimer: Task<Void, Never>? + @State private var shouldReconnect: Bool = false @State private var model: Hotline = Hotline(trackerClient: HotlineTrackerClient(), client: HotlineClient()) @State private var state: ServerState = ServerState(selection: .chat) @State private var agreementShown: Bool = false @@ -151,6 +153,7 @@ struct ServerView: View { @State private var connectPassword: String = "" @State private var connectNameSheetPresented: Bool = false @State private var connectName: String = "" + @State private var autoconnect: Bool = false @Binding var server: Server @@ -227,6 +230,16 @@ struct ServerView: View { .onDisappear { model.disconnect() } + .onChange(of: model.status) { + if model.status == .loggedIn { + reconnectTimer?.cancel() + shouldReconnect = server.autoconnect + } + if model.status == .disconnected && shouldReconnect { + shouldReconnect = false + startReconnectTimer() + } + } .alert(model.errorMessage ?? "Server Error", isPresented: $model.errorDisplayed) { Button("OK") {} } @@ -248,6 +261,15 @@ struct ServerView: View { .focusedSceneValue(\.activeServerState, state) } + private func startReconnectTimer() { + reconnectTimer = Task { + while !Task.isCancelled { + connectToServer() + try? await Task.sleep(for: .seconds(5)) + } + } + } + var connectForm: some View { VStack(alignment: .center) { GroupBox { @@ -297,7 +319,7 @@ struct ServerView: View { Button("Connect") { Task { - await connectToServer() + connectToServer() } } @@ -335,6 +357,7 @@ struct ServerView: View { TextField("Bookmark Name", text: $connectName) .textFieldStyle(.roundedBorder) .controlSize(.large) + Toggle("Connect on Startup", isOn: $autoconnect) } .frame(width: 250) .padding() @@ -353,19 +376,24 @@ struct ServerView: View { connectNameSheetPresented = false connectName = "" Task.detached { - let (host, port) = Server.parseServerAddressAndPort(connectAddress) - let login: String? = connectLogin.isEmpty ? nil : connectLogin - let password: String? = connectPassword.isEmpty ? nil : connectPassword + let (host, port) = await Server.parseServerAddressAndPort(connectAddress) + let login: String? = await connectLogin.isEmpty ? nil : connectLogin + let password: String? = await connectPassword.isEmpty ? nil : connectPassword if !host.isEmpty { - let newBookmark = Bookmark(type: .server, name: name, address: host, port: port, login: login, password: password) - Bookmark.add(newBookmark, context: modelContext) + let newBookmark = await Bookmark(type: .server, name: name, address: host, port: port, login: login, password: password, autoconnect: autoconnect) + await MainActor.run { + Bookmark.add(newBookmark, context: modelContext) + } } } } } } } + .onAppear { + autoconnect = false + } } } diff --git a/Hotline/macOS/TrackerView.swift b/Hotline/macOS/TrackerView.swift index 0fac8b0..cdeef7f 100644 --- a/Hotline/macOS/TrackerView.swift +++ b/Hotline/macOS/TrackerView.swift @@ -24,6 +24,11 @@ struct TrackerView: View { List(selection: $selection) { ForEach(bookmarks, id: \.self) { bookmark in TrackerItemView(bookmark: bookmark) + .onAppear { + if bookmark.autoconnect, let server = bookmark.server { + openWindow(id: "server", value: server) + } + } .tag(bookmark) if bookmark.type == .tracker && bookmark.expanded { @@ -403,7 +408,19 @@ struct TrackerItemView: View { .frame(width: 11, height: 11, alignment: .center) .opacity(0.5) .padding(.leading, 3) + Image(systemName: "bolt.fill") + .resizable() + .renderingMode(.template) + .aspectRatio(contentMode: .fit) + .frame(width: 13, height: 13, alignment: .center) + .opacity(0.5) + .padding(.leading, 3) .padding(.trailing, 2) + .foregroundStyle(bookmark.autoconnect ? Color.accentColor : Color.gray) + .onTapGesture { + bookmark.autoconnect.toggle() + try? bookmark.modelContext?.save() + } Image("Server") .resizable() .scaledToFit() |