Skip to content

Commit

Permalink
feat: create-hosting-window-for-auth-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
FranAlarza committed Dec 23, 2024
1 parent 98e56bf commit f517237
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 deletions AppliverySDK/Data/Persisters/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Keychain: KeychainAccessible {
guard status == errSecSuccess else {
throw KeychainError.unhandledError(status: status)
}
logInfo("Password stored for account \(account), password: \(data)")
logInfo("Password stored for account \(account)")
}

func retrieve(for account: String) throws -> String {
Expand All @@ -64,7 +64,7 @@ final class Keychain: KeychainAccessible {
let password = String(data: data, encoding: .utf8) else {
throw KeychainError.unexpectedPasswordData
}
logInfo("Retrieved password for account \(account), password: \(password)")
logInfo("Retrieved password for account \(account)")
return password
} else {
throw KeychainError.unhandledError(status: status)
Expand Down
6 changes: 3 additions & 3 deletions AppliverySDK/Interactors/StartInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class StartInteractor {
// MARK: Internal Methods

func start() {
try? keychain.remove(for: app.bundleId())
logInfo("Applivery is starting... ")
logInfo("SDK Version: \(GlobalConfig.shared.app.getSDKVersion())")
setupBindings()
Expand Down Expand Up @@ -126,9 +127,8 @@ private extension StartInteractor {
logInfo("Opening auth web view...")
let redirectURL = try await loginService.getRedirectURL()
await MainActor.run {
if let url = redirectURL,
let rootViewController = UIApplication.shared.windows.first?.rootViewController {
webViewManager.showWebView(url: url, from: rootViewController)
if let url = redirectURL {
webViewManager.showWebView(url: url)
}
}
} catch {
Expand Down
37 changes: 26 additions & 11 deletions AppliverySDK/Managers/AppliveryWebViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import SafariServices
import Combine

protocol AppliveryWebViewManagerProtocol {
func showWebView(url: URL, from viewController: UIViewController)
func closeWebView()
func showWebView(url: URL)
var tokenPublisher: AnyPublisher<String?, Never> { get }
}

Expand All @@ -20,28 +19,26 @@ final class AppliveryWebViewManager: NSObject, AppliveryWebViewManagerProtocol {
static public let shared = AppliveryWebViewManager()

private weak var safariViewController: SFSafariViewController?
private var window: UIWindow?

private let tokenSubject = CurrentValueSubject<String?, Never>(nil)
var tokenPublisher: AnyPublisher<String?, Never> {
return tokenSubject.eraseToAnyPublisher()
}

private override init() {}

func showWebView(url: URL, from viewController: UIViewController) {
func showWebView(url: URL) {
let safariVC = SFSafariViewController(url: url)
safariVC.delegate = self
viewController.present(safariVC, animated: true, completion: nil)
setNewWindow(viewController: safariVC)
log("Showing web view in a new window")
self.safariViewController = safariVC
}

func closeWebView() {
safariViewController?.dismiss(animated: true, completion: nil)
safariViewController = nil
}

func urlReceived(url: URL) {
if let token = getTokenfromURL(url: url) {
closeWebView()
removeWindow()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.tokenSubject.send(token)
}
Expand All @@ -52,7 +49,7 @@ final class AppliveryWebViewManager: NSObject, AppliveryWebViewManagerProtocol {
extension AppliveryWebViewManager: SFSafariViewControllerDelegate {
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
tokenSubject.send(nil)
safariViewController = nil
removeWindow()
}
}

Expand All @@ -64,4 +61,22 @@ private extension AppliveryWebViewManager {
}
return nil
}

func setNewWindow(viewController: UIViewController) {
let hostingViewController = UIViewController()
let newWindow = UIWindow(frame: UIScreen.main.bounds)
newWindow.rootViewController = hostingViewController
newWindow.windowLevel = UIWindow.Level(rawValue: CGFloat.greatestFiniteMagnitude)
newWindow.makeKeyAndVisible()
hostingViewController.present(viewController, animated: true)

self.window = newWindow
}

func removeWindow() {
safariViewController?.dismiss(animated: true, completion: nil)
safariViewController = nil
window?.isHidden = true
window = nil
}
}
4 changes: 2 additions & 2 deletions AppliverySDK/Modules/VideoReport/VideoPreviewScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ struct VideoPreviewScreen: View {
self.presentationMode.wrappedValue.dismiss()
}, label: {
Text("X")
.font(.system(size: 20))
.foregroundColor(.black)
.font(.system(size: 20, weight: .bold))
.foregroundColor(.blue)
}),
trailing:
Button(action: {
Expand Down
4 changes: 2 additions & 2 deletions AppliverySDK/Services/LoginService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ final class LoginService: LoginServiceProtocol {
do {
logInfo("Opening auth web view...")
let redirectURL = try await loginRepository.getRedirctURL()
if let url = redirectURL, let rootViewController = UIApplication.shared.windows.first?.rootViewController {
webViewManager.showWebView(url: url, from: rootViewController)
if let url = redirectURL {
webViewManager.showWebView(url: url)
}
} catch {
log("Error obtaining redirect URL: \(error.localizedDescription)")
Expand Down

0 comments on commit f517237

Please sign in to comment.