diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index e3a57ee..9d3f5d3 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -20,8 +20,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { @Published var title: String = "" @Published var state: SSHState = .idle var connected: Bool { - return !(state == .idle || state == .connecting) -// return state == .authorized || state == .shellOpen || state == .authorizing + return checkConnected(state) } // @Published var connected: Bool = false // @Published var authorized: Bool = false @@ -57,6 +56,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { func go() { guard !connected else { + withAnimation { state = .idle } Task { await disconnect() } @@ -130,8 +130,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { func disconnect() async { await MainActor.run { withAnimation { state = .idle } -// withAnimation { connected = false } -// withAnimation { authorized = false } withAnimation { testSuceeded = nil } } @@ -171,7 +169,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { } } - if state == .authorized {} else { + if !checkAuth(state) { go() } @@ -180,7 +178,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { return } - guard state == .authorized else { + guard checkAuth(state) else { withAnimation { testSuceeded = false } return } @@ -378,7 +376,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { status = ssh_channel_request_shell(self.channel) guard status == SSH_OK else { return } - + withAnimation { state = .shellOpen } } func readFromChannel() -> String? { diff --git a/ShhShell/SSH/SSHState.swift b/ShhShell/SSH/SSHState.swift index db31e61..3eed7b3 100644 --- a/ShhShell/SSH/SSHState.swift +++ b/ShhShell/SSH/SSHState.swift @@ -14,3 +14,15 @@ enum SSHState { case authorized case shellOpen } + +func checkConnected(_ state: SSHState) -> Bool { + return !(state == .idle || state == .connecting) +} + +func checkAuth(_ state: SSHState) -> Bool { + return state == .authorized || state == .shellOpen +} + +func checkShell(_ state: SSHState) -> Bool { + return state == .shellOpen +} diff --git a/ShhShell/Views/ConnectionView.swift b/ShhShell/Views/ConnectionView.swift index f9aeae6..f992ea1 100644 --- a/ShhShell/Views/ConnectionView.swift +++ b/ShhShell/Views/ConnectionView.swift @@ -24,13 +24,14 @@ struct ConnectionView: View { var body: some View { NavigationStack { List { + Text("\(handler.state)") Section { HStack { Text(handler.connected ? "connected" : "not connected") .modifier(foregroundColorStyle(handler.connected ? .green : .red)) - Text(handler.state == .authorized ? "authorized" : "unauthorized") - .modifier(foregroundColorStyle(handler.state == .authorized ? .green : .red)) + Text(checkAuth(handler.state) ? "authorized" : "unauthorized") + .modifier(foregroundColorStyle(checkAuth(handler.state) ? .green : .red)) } TextField("address", text: $handler.host.address) .textFieldStyle(.roundedBorder) @@ -91,7 +92,7 @@ struct ConnectionView: View { } label: { Label("Show Terminal", systemImage: "apple.terminal") } - .disabled(!handler.connected || !(handler.state == .authorized)) + .disabled(!checkShell(handler.state)) Button() { handler.testExec() @@ -123,7 +124,7 @@ struct ConnectionView: View { ToolbarItem() { Button() { handler.go() - showTerminal = handler.connected && handler.state == .authorized + showTerminal = checkShell(handler.state) } label: { Label( handler.connected ? "Disconnect" : "Connect", @@ -134,6 +135,7 @@ struct ConnectionView: View { } } .fullScreenCover(isPresented: $showTerminal) { + Text("\(handler.state)") ShellView(handler: handler) } .onDisappear {