summaryrefslogtreecommitdiff
path: root/Jel/Views/SignIn/AddServerView.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Jel/Views/SignIn/AddServerView.swift')
-rw-r--r--Jel/Views/SignIn/AddServerView.swift108
1 files changed, 108 insertions, 0 deletions
diff --git a/Jel/Views/SignIn/AddServerView.swift b/Jel/Views/SignIn/AddServerView.swift
new file mode 100644
index 0000000..516b982
--- /dev/null
+++ b/Jel/Views/SignIn/AddServerView.swift
@@ -0,0 +1,108 @@
+//
+// AddServerView.swift
+// Jel
+//
+// Created by zerocool on 12/11/23.
+//
+
+import SwiftUI
+
+struct AddServerView: View {
+ @EnvironmentObject var jellyfinClient: JellyfinClientController
+ @ObservedObject var authState: AuthStateController
+ @Binding var serverUrlIsValid: Bool
+
+ @State var serverUrlString: String = "http://"
+ @State var urlHasError: Bool = false
+ @State var currentErrorMessage: String = ""
+ @State var isLoading: Bool = false
+
+ @FocusState var serverUrlIsFocused: Bool
+
+ var body: some View {
+ VStack {
+ Text("Connect to a server")
+ .font(.title)
+ HStack {
+ TextField(text: $serverUrlString) {
+ Text("http://jellyfin.example.com")
+ }
+ .keyboardType(.URL)
+ .textContentType(.URL)
+ .textFieldStyle(.roundedBorder)
+ .textInputAutocapitalization(.never)
+ .autocorrectionDisabled()
+ .focused($serverUrlIsFocused)
+ .onSubmit {
+ Task {
+ await checkServerUrl()
+ }
+ }
+
+
+ if !isLoading {
+ Button(action: {
+ Task {
+ await checkServerUrl()
+ }
+ }) {
+ Label("Connect", systemImage: "arrow.right")
+ .labelStyle(.iconOnly)
+ }
+ .buttonStyle(.bordered)
+ } else {
+ ProgressView()
+ .progressViewStyle(.circular)
+ .padding([.leading, .trailing])
+ }
+ }
+ .padding()
+ .disabled(isLoading)
+
+ if urlHasError {
+ Text(currentErrorMessage)
+ .font(.callout)
+ .foregroundStyle(.red)
+ }
+ }
+ }
+
+ func checkServerUrl() async {
+ isLoading = true
+ serverUrlIsFocused = false
+ if isValidUrl(data: serverUrlString) {
+ let url = URL(string: serverUrlString)!
+ jellyfinClient.setUrl(url: url)
+ if await jellyfinClient.isJellyfinServer() {
+ authState.serverUrl = url
+ authState.save()
+ urlHasError = false
+ serverUrlIsValid = true
+ } else {
+ urlHasError = true
+ currentErrorMessage = "Server not responding"
+ }
+
+ } else {
+ urlHasError = true
+ currentErrorMessage = "Invalid url"
+ }
+
+ isLoading = false
+ }
+
+ func isValidUrl(data: String) -> Bool {
+ if let url = URL(string: data) {
+ if UIApplication.shared.canOpenURL(url) {
+ return true
+ }
+ }
+ return false
+ }
+
+}
+
+#Preview {
+ AddServerView(authState: AuthStateController(), serverUrlIsValid: .constant(false))
+
+}