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