summaryrefslogtreecommitdiff
path: root/Jel/Views/SignIn
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-12-12 17:09:15 -0500
committerShav Kinderlehrer <[email protected]>2023-12-12 17:09:15 -0500
commitfbb37567460b689f01eb8a8717b9ac8673652c28 (patch)
treeb6517179cb849732efb8660edfa9899456420d3b /Jel/Views/SignIn
parent02fc87fe2588cdca5188cf1a6d338ce83de65a43 (diff)
downloadjel-fbb37567460b689f01eb8a8717b9ac8673652c28.tar.gz
jel-fbb37567460b689f01eb8a8717b9ac8673652c28.zip
Implement signIn flow
Diffstat (limited to 'Jel/Views/SignIn')
-rw-r--r--Jel/Views/SignIn/AddServerView.swift108
-rw-r--r--Jel/Views/SignIn/SignInToServerView.swift79
-rw-r--r--Jel/Views/SignIn/SignInView.swift42
3 files changed, 229 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))
+
+}
diff --git a/Jel/Views/SignIn/SignInToServerView.swift b/Jel/Views/SignIn/SignInToServerView.swift
new file mode 100644
index 0000000..ae8d82d
--- /dev/null
+++ b/Jel/Views/SignIn/SignInToServerView.swift
@@ -0,0 +1,79 @@
+//
+// SignInToServerView.swift
+// Jel
+//
+// Created by zerocool on 12/12/23.
+//
+
+import SwiftUI
+
+struct SignInToServerView: View {
+ @EnvironmentObject var jellyfinClient: JellyfinClientController
+ @ObservedObject var authState: AuthStateController
+
+ @State var username: String = ""
+ @State var password: String = ""
+
+ @State var isLoading: Bool = false
+ @State var hasError: Bool = false
+
+ var body: some View {
+ VStack {
+ Text("Sign in")
+ .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)
+ }
+
+ func logInToServer() async {
+ isLoading = true
+ hasError = false
+ do {
+ try await jellyfinClient.signIn(username: username, pw: password)
+ } catch {
+ hasError = true
+ }
+ isLoading = false
+ }
+}
+
+#Preview {
+ SignInToServerView(authState: AuthStateController())
+}
diff --git a/Jel/Views/SignIn/SignInView.swift b/Jel/Views/SignIn/SignInView.swift
new file mode 100644
index 0000000..c06788d
--- /dev/null
+++ b/Jel/Views/SignIn/SignInView.swift
@@ -0,0 +1,42 @@
+//
+// SignInView.swift
+// Jel
+//
+// Created by zerocool on 12/12/23.
+//
+
+import SwiftUI
+
+struct SignInView: View {
+ @EnvironmentObject var jellyfinClient: JellyfinClientController
+ @ObservedObject var authState: AuthStateController
+ @State var serverUrlIsValid: Bool = false
+
+ var body: some View {
+ NavigationStack {
+ AddServerView(authState: authState, serverUrlIsValid: $serverUrlIsValid)
+ .navigationDestination(isPresented: $serverUrlIsValid) {
+ SignInToServerView(authState: authState)
+ }
+ }
+ .onAppear {
+ Task {
+ await checkLoadedServerUrl()
+ }
+ }
+ }
+
+ func checkLoadedServerUrl() async {
+ if authState.serverUrl == nil {
+ return
+ }
+
+ if await jellyfinClient.isJellyfinServer() {
+ serverUrlIsValid = true
+ }
+ }
+}
+
+#Preview {
+ SignInView(authState: AuthStateController())
+}