// // SignInToServerView.swift // Jel // // Created by zerocool on 12/12/23. // import SwiftUI struct SignInToServerView: View { @EnvironmentObject var jellyfinClient: JellyfinClientController @ObservedObject var authState: AuthStateController @State var username: String = "" @State var password: String = "" @State var isLoading: Bool = false @State var hasError: Bool = false var body: some View { VStack { Text("Sign in") .font(.title) TextField(text: $username) { Text("Username") } .textContentType(.username) SecureField(text: $password) { Text("Password") } .textContentType(.password) .onSubmit { Task { await logInToServer() } } if !isLoading { Button { Task { await logInToServer() } } label: { Text("Sign in") } .disabled(username.isEmpty || password.isEmpty) } else { ProgressView() .progressViewStyle(.circular) } if hasError { Text("Unable to sign in") .font(.callout) .foregroundStyle(.red) } } .padding() .textFieldStyle(.roundedBorder) .textInputAutocapitalization(.never) .disabled(isLoading) } func logInToServer() async { isLoading = true hasError = false do { try await jellyfinClient.signIn(username: username, pw: password) } catch { hasError = true } isLoading = false } } #Preview { SignInToServerView(authState: AuthStateController()) }