/* 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 SwiftUI import Stinsen struct ConnectToServerView: View { @EnvironmentObject var mainRouter: MainCoordinator.Router @StateObject var viewModel = ConnectToServerViewModel() @State var uri = "" var body: some View { List { Section { TextField(NSLocalizedString("Server URL", comment: ""), text: $uri) .disableAutocorrection(true) .autocapitalization(.none) .keyboardType(.URL) Button { viewModel.connectToServer(uri: uri) } label: { HStack { Text("Connect") Spacer() if viewModel.isLoading { ProgressView() } } } .disabled(viewModel.isLoading || uri.isEmpty) } Section(header: Text("Discovered Servers")) { if viewModel.searching { ProgressView() } ForEach(viewModel.discoveredServers.sorted(by: { $0.name < $1.name }), id: \.id) { discoveredServer in Button(action: { viewModel.connectToServer(uri: discoveredServer.url.absoluteString) }, label: { HStack { Text(discoveredServer.name) .font(.headline) Text("• \(discoveredServer.host)") .font(.subheadline) .foregroundColor(.secondary) Spacer() if viewModel.isLoading { ProgressView() } } }) } } .onAppear(perform: self.viewModel.discoverServers) .headerProminence(.increased) } .alert(item: $viewModel.errorMessage) { _ in Alert(title: Text("\(viewModel.errorMessage?.code ?? -1)\n\(viewModel.errorMessage?.title ?? "Error")"), message: Text(viewModel.errorMessage?.displayMessage ?? "Error"), dismissButton: .cancel()) } .navigationTitle("Connect") .onAppear { AppURLHandler.shared.appURLState = .allowedInLogin } } } //struct ConnectToServerView: View { // @EnvironmentObject var mainRouter: MainCoordinator.Router // @StateObject var viewModel = ConnectToServerViewModel() // @State var username = "" // @State var password = "" // @State var uri = "" // // var body: some View { // ZStack { // Form { // if viewModel.isConnectedServer { // if viewModel.publicUsers.isEmpty { // Section(header: Text("Login to \(SessionManager.main.currentLogin.server.name)")) { // TextField(NSLocalizedString("Username", comment: ""), text: $username) // .disableAutocorrection(true) // .autocapitalization(.none) // SecureField(NSLocalizedString("Password", comment: ""), text: $password) // .disableAutocorrection(true) // .autocapitalization(.none) // Button { // viewModel.login() // } label: { // HStack { // Text("Login") // Spacer() // if viewModel.isLoading { // ProgressView() // } // } // }.disabled(viewModel.isLoading || username.isEmpty) // } // // Section { // Button { // viewModel.isConnectedServer = false // } label: { // HStack { // HStack { // Image(systemName: "chevron.left") // Text("Change Server") // } // Spacer() // } // } // } // } else { // Section(header: Text("Login to \(SessionManager.main.currentLogin.server.name)")) { // ForEach(viewModel.publicUsers, id: \.id) { publicUser in // HStack { // Button(action: { // // TODO: todo // print("TODO") //// if SessionManager.current.doesUserHaveSavedSession(userID: publicUser.id!) { //// let user = SessionManager.current.getSavedSession(userID: publicUser.id!) //// SessionManager.current.loginWithSavedSession(user: user) //// mainRouter.root(\.mainTab) //// } else { //// username = publicUser.name ?? "" //// viewModel.selectedPublicUser = publicUser //// viewModel.hidePublicUsers() //// if !(publicUser.hasPassword ?? true) { //// password = "" //// DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { //// viewModel.login() //// } //// } //// } // }) { // HStack { // Text(publicUser.name ?? "").font(.subheadline).fontWeight(.semibold) // Spacer() // if publicUser.primaryImageTag != nil { // ImageView(src: URL(string: "\(SessionManager.main.currentLogin.server.uri)/Users/\(publicUser.id ?? "")/Images/Primary?width=60&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() // 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("Connect Manually")) { // TextField(NSLocalizedString("Server URL", comment: ""), text: $uri) // .disableAutocorrection(true) // .autocapitalization(.none) // .keyboardType(.URL) // Button { // viewModel.connectToServer() // } label: { // HStack { // Text("Connect") // Spacer() // if viewModel.isLoading { // ProgressView() // } // } // } // .disabled(viewModel.isLoading || uri.isEmpty) // } // // Section(header: Text("Discovered Servers")) { // if self.viewModel.searching { // ProgressView() // } // ForEach(self.viewModel.servers, id: \.id) { server in // Button(action: { // viewModel.connectToServer(at: server.url) // }, label: { // HStack { // Text(server.name) // .font(.headline) // Text("• \(server.host)") // .font(.subheadline) // .foregroundColor(.secondary) // Spacer() // if viewModel.isLoading { // ProgressView() // } // } // // }) // } // } // .onAppear(perform: self.viewModel.discoverServers) // } // } // } // .onChange(of: uri) { uri in // viewModel.uriSubject.send(uri) // } // .onChange(of: username) { username in // viewModel.usernameSubject.send(username) // } // .onChange(of: password) { password in // viewModel.passwordSubject.send(password) // } // .alert(item: $viewModel.errorMessage) { _ in // Alert(title: Text("\(viewModel.errorMessage?.code ?? -1)\n\(viewModel.errorMessage?.title ?? "Error")"), // message: Text(viewModel.errorMessage?.displayMessage ?? "Error"), // dismissButton: .cancel()) // } // .navigationTitle(NSLocalizedString("Connect to Server", comment: "")) // .onAppear { // AppURLHandler.shared.appURLState = .allowedInLogin // } // } //}