addedfunctions to check state

large code cleanup thanks to this
This commit is contained in:
neon443
2025-06-25 12:40:16 +01:00
parent 66547dc58e
commit 46ba88aacb
3 changed files with 23 additions and 11 deletions

View File

@@ -20,8 +20,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
@Published var title: String = "" @Published var title: String = ""
@Published var state: SSHState = .idle @Published var state: SSHState = .idle
var connected: Bool { var connected: Bool {
return !(state == .idle || state == .connecting) return checkConnected(state)
// return state == .authorized || state == .shellOpen || state == .authorizing
} }
// @Published var connected: Bool = false // @Published var connected: Bool = false
// @Published var authorized: Bool = false // @Published var authorized: Bool = false
@@ -57,6 +56,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
func go() { func go() {
guard !connected else { guard !connected else {
withAnimation { state = .idle }
Task { Task {
await disconnect() await disconnect()
} }
@@ -130,8 +130,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
func disconnect() async { func disconnect() async {
await MainActor.run { await MainActor.run {
withAnimation { state = .idle } withAnimation { state = .idle }
// withAnimation { connected = false }
// withAnimation { authorized = false }
withAnimation { testSuceeded = nil } withAnimation { testSuceeded = nil }
} }
@@ -171,7 +169,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
} }
} }
if state == .authorized {} else { if !checkAuth(state) {
go() go()
} }
@@ -180,7 +178,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
return return
} }
guard state == .authorized else { guard checkAuth(state) else {
withAnimation { testSuceeded = false } withAnimation { testSuceeded = false }
return return
} }
@@ -378,7 +376,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
status = ssh_channel_request_shell(self.channel) status = ssh_channel_request_shell(self.channel)
guard status == SSH_OK else { return } guard status == SSH_OK else { return }
withAnimation { state = .shellOpen }
} }
func readFromChannel() -> String? { func readFromChannel() -> String? {

View File

@@ -14,3 +14,15 @@ enum SSHState {
case authorized case authorized
case shellOpen 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
}

View File

@@ -24,13 +24,14 @@ struct ConnectionView: View {
var body: some View { var body: some View {
NavigationStack { NavigationStack {
List { List {
Text("\(handler.state)")
Section { Section {
HStack { HStack {
Text(handler.connected ? "connected" : "not connected") Text(handler.connected ? "connected" : "not connected")
.modifier(foregroundColorStyle(handler.connected ? .green : .red)) .modifier(foregroundColorStyle(handler.connected ? .green : .red))
Text(handler.state == .authorized ? "authorized" : "unauthorized") Text(checkAuth(handler.state) ? "authorized" : "unauthorized")
.modifier(foregroundColorStyle(handler.state == .authorized ? .green : .red)) .modifier(foregroundColorStyle(checkAuth(handler.state) ? .green : .red))
} }
TextField("address", text: $handler.host.address) TextField("address", text: $handler.host.address)
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
@@ -91,7 +92,7 @@ struct ConnectionView: View {
} label: { } label: {
Label("Show Terminal", systemImage: "apple.terminal") Label("Show Terminal", systemImage: "apple.terminal")
} }
.disabled(!handler.connected || !(handler.state == .authorized)) .disabled(!checkShell(handler.state))
Button() { Button() {
handler.testExec() handler.testExec()
@@ -123,7 +124,7 @@ struct ConnectionView: View {
ToolbarItem() { ToolbarItem() {
Button() { Button() {
handler.go() handler.go()
showTerminal = handler.connected && handler.state == .authorized showTerminal = checkShell(handler.state)
} label: { } label: {
Label( Label(
handler.connected ? "Disconnect" : "Connect", handler.connected ? "Disconnect" : "Connect",
@@ -134,6 +135,7 @@ struct ConnectionView: View {
} }
} }
.fullScreenCover(isPresented: $showTerminal) { .fullScreenCover(isPresented: $showTerminal) {
Text("\(handler.state)")
ShellView(handler: handler) ShellView(handler: handler)
} }
.onDisappear { .onDisappear {