/* JellyfinPlayer/Swiftfin is subject to the terms of the Mozilla Public * License, v2.0. If a copy of the MPL was not distributed with this * file, you can obtain one at https://mozilla.org/MPL/2.0/. * * Copyright 2021 Aiden Vigue & Jellyfin Contributors */ import JellyfinAPI import SwiftUI struct ConnectToServerView: View { @StateObject var viewModel = ConnectToServerViewModel() @Binding var isLoggedIn: Bool var body: some View { ZStack { Form { if viewModel.isConnectedServer { if viewModel.publicUsers.isEmpty { Section(header: Text("Login to \(ServerEnvironment.current.server.name ?? "")")) { TextField("Username", text: $viewModel.username) .disableAutocorrection(true) .autocapitalization(.none) SecureField("Password", text: $viewModel.password) .disableAutocorrection(true) .autocapitalization(.none) Button { viewModel.login() } label: { HStack { Text("Login") Spacer() if viewModel.isLoading { ProgressView() } } }.disabled(viewModel.isLoading || viewModel.username.isEmpty) } Section { Button { viewModel.isConnectedServer = false } label: { HStack { HStack { Image(systemName: "chevron.left") Text("Change Server") } Spacer() } } } } else { Section(header: Text("Login to \(ServerEnvironment.current.server.name ?? "")")) { ForEach(viewModel.publicUsers, id: \.id) { publicUser in HStack { Button(action: { viewModel.username = publicUser.name ?? "" viewModel.publicUsers.removeAll() if !(publicUser.hasPassword ?? true) { viewModel.password = "" viewModel.login() } }) { HStack { Text(publicUser.name ?? "").font(.subheadline).fontWeight(.semibold) Spacer() if publicUser.primaryImageTag != nil { ImageView(src: URL(string: "\(ServerEnvironment.current.server.baseURI ?? "")/Users/\(publicUser.id ?? "")/Images/Primary?width=200&quality=80&tag=\(publicUser.primaryImageTag!)")!) .frame(width: 60, height: 60) .cornerRadius(30.0) } else { Image(systemName: "person.fill") .foregroundColor(Color(red: 1, green: 1, blue: 1).opacity(0.8)) .font(.system(size: 35)) .frame(width: 60, height: 60) .background(Color(red: 98 / 255, green: 121 / 255, blue: 205 / 255)) .cornerRadius(30.0) .shadow(radius: 6) } } } } } } Section { Button { viewModel.publicUsers.removeAll() viewModel.username = "" } label: { HStack { Text("Other User").font(.subheadline).fontWeight(.semibold) Spacer() Image(systemName: "person.fill.questionmark") .foregroundColor(Color(red: 1, green: 1, blue: 1).opacity(0.8)) .font(.system(size: 35)) .frame(width: 60, height: 60) .background(Color(red: 98 / 255, green: 121 / 255, blue: 205 / 255)) .cornerRadius(30.0) .shadow(radius: 6) } } } } } else { Section(header: Text("Server Information")) { TextField("Jellyfin Server URL", text: $viewModel.uri) .disableAutocorrection(true) .autocapitalization(.none) Button { viewModel.connectToServer() } label: { HStack { Text("Connect") Spacer() } if viewModel.isLoading { ProgressView() } } .disabled(viewModel.isLoading || viewModel.uri.isEmpty) } } } } .alert(item: $viewModel.errorMessage) { _ in Alert(title: Text("Error"), message: Text("message"), dismissButton: .default(Text("Try again"))) } .onReceive(viewModel.$isLoggedIn, perform: { flag in isLoggedIn = flag }) .navigationTitle("Connect to Server") } }