From 8c55cece2b18eeda75ec7e9580fc6f7a9cb13bcf Mon Sep 17 00:00:00 2001 From: James Wolfe <56296015+wolfej94@users.noreply.github.com> Date: Fri, 18 Feb 2022 03:20:21 +0000 Subject: [PATCH] Further adjustments for swiftUI --- PassportKit.podspec | 2 +- Sources/Network/PassportAuthAPI.swift | 8 ++++---- Sources/PassportKit.swift | 10 ++++++++-- Sources/PassportViewModel.swift | 25 ++++++++----------------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/PassportKit.podspec b/PassportKit.podspec index c1193a8..5c7d7a1 100644 --- a/PassportKit.podspec +++ b/PassportKit.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = "PassportKit" - spec.version = "1.7.1" + spec.version = "1.7.2" spec.license = "MIT" spec.summary = "Swift library used for quick and easy oauth authentication." spec.homepage = "https://github.com/appoly/PassportKit" diff --git a/Sources/Network/PassportAuthAPI.swift b/Sources/Network/PassportAuthAPI.swift index 5792c76..e114ff1 100644 --- a/Sources/Network/PassportAuthAPI.swift +++ b/Sources/Network/PassportAuthAPI.swift @@ -40,13 +40,13 @@ enum PassportAuthAPI { case .login(let configuration, let model): switch configuration.mode { case .sanctum: - let username = model.email!.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" - let password = model.password!.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" + let username = model.email.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" + let password = model.password.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" return "username=\(username)&password=\(password)" .data(using: .utf8) case .standard(let clientID, let clientSecret): - let username = model.email!.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" - let password = model.password!.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" + let username = model.email.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" + let password = model.password.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" let clientID = clientID.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" let clientSecret = clientSecret.addingPercentEncoding(withAllowedCharacters: Self.allowedCharacters) ?? "" return "username=\(username)&password=\(password)&client_id=\(clientID)&client_secret=\(clientSecret)&grant_type=password" diff --git a/Sources/PassportKit.swift b/Sources/PassportKit.swift index db3fe82..354e8ea 100644 --- a/Sources/PassportKit.swift +++ b/Sources/PassportKit.swift @@ -9,6 +9,7 @@ import Foundation import LocalAuthentication +import SwiftUI @@ -59,11 +60,16 @@ public class PassportKit: NSObject, ObservableObject { public func setup(_ configuration: PassportConfiguration) { self.configuration = configuration self.authManager = PassportKitAuthenticationManager(configuration.keychainID) - self.isAuthenticated = self.authManager.isAuthenticated - NotificationCenter.default.addObserver(forName: .passportKitAuthenticationStateChanged, object: nil, queue: .main) { [weak self] _ in + withAnimation { [weak self] in guard let self = self else { return } self.isAuthenticated = self.authManager.isAuthenticated } + NotificationCenter.default.addObserver(forName: .passportKitAuthenticationStateChanged, object: nil, queue: .main) { _ in + withAnimation { [weak self] in + guard let self = self else { return } + self.isAuthenticated = self.authManager.isAuthenticated + } + } } diff --git a/Sources/PassportViewModel.swift b/Sources/PassportViewModel.swift index e31a5ab..7291cba 100644 --- a/Sources/PassportViewModel.swift +++ b/Sources/PassportViewModel.swift @@ -10,15 +10,16 @@ import Foundation import UIKit +import SwiftUI -public class PassportViewModel: NSObject { +open class PassportViewModel: NSObject, ObservableObject { // MARK: - Variables - private(set) var email: String? - private(set) var password: String? + @Published public var email: String = "" + @Published public var password: String = "" private let passwordRegex: String? private let authController = PassportKitAuthService() @@ -37,22 +38,12 @@ public class PassportViewModel: NSObject { // MARK: - Setters @objc public func setPassword(_ sender: UITextField) { - password = sender.text + password = sender.text ?? "" } @objc public func setEmail(_ sender: UITextField) { - email = sender.text - } - - - public func setPassword(string: String) { - password = string - } - - - public func setEmail(string: String) { - email = string + email = sender.text ?? "" } @@ -61,8 +52,8 @@ public class PassportViewModel: NSObject { public func validateForLogin(completion: @escaping PassportKitValidationResponse) { - let email = self.email ?? "" - let password = self.password ?? "" + let email = self.email + let password = self.password guard !email.isEmpty else { DispatchQueue.main.async { completion(PassportKitValidationError.missingEmail) }