jel/Jel/Views/SignIn/SignInToServerView.swift

90 lines
1.9 KiB
Swift

//
// SignInToServerView.swift
// Jel
//
// Created by zerocool on 12/12/23.
//
import SwiftUI
struct SignInToServerView: View {
@EnvironmentObject var jellyfinClient: JellyfinClientController
@StateObject var authState: AuthStateController = AuthStateController.shared
@State var username: String = ""
@State var password: String = ""
@State var isLoading: Bool = false
@State var hasError: Bool = false
@State var serverName: String = ""
var body: some View {
VStack {
Text("Sign in to \(serverName)")
.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)
.onAppear {
Task {
if let serverInfo = await jellyfinClient.getPublicServerInfo() {
serverName = serverInfo.serverName ?? "Unknown"
}
}
}
}
func logInToServer() async {
isLoading = true
hasError = false
do {
try await jellyfinClient.signIn(username: username, pw: password)
} catch {
hasError = true
}
isLoading = false
}
}
// FIXME: Preview broken for this view
//#Preview {
// SignInToServerView()
//}