-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f517237
commit bbd93d1
Showing
6 changed files
with
136 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// WebViewManager.swift | ||
// Applivery | ||
// | ||
// Created by Fran Alarza on 29/11/24. | ||
// | ||
|
||
|
||
import SafariServices | ||
import Combine | ||
|
||
protocol AppliverySafariManagerProtocol { | ||
func openSafari(from url: URL) | ||
var tokenPublisher: AnyPublisher<String?, Never> { get } | ||
} | ||
|
||
final class AppliverySafariManager: NSObject, AppliverySafariManagerProtocol { | ||
|
||
static let shared = AppliverySafariManager() | ||
|
||
private let windowPresenter: WindowPresentable | ||
|
||
private var safariViewController: SFSafariViewController? | ||
|
||
private let tokenSubject = CurrentValueSubject<String?, Never>(nil) | ||
var tokenPublisher: AnyPublisher<String?, Never> { | ||
tokenSubject.eraseToAnyPublisher() | ||
} | ||
|
||
// MARK: - Init | ||
init( | ||
windowPresenter: WindowPresentable = WindowManager() | ||
) { | ||
self.windowPresenter = windowPresenter | ||
super.init() | ||
} | ||
|
||
// MARK: - Public Methods | ||
func openSafari(from url: URL) { | ||
let safariVC = SFSafariViewController(url: url) | ||
safariVC.delegate = self | ||
windowPresenter.present(viewController: safariVC) | ||
safariViewController = safariVC | ||
} | ||
|
||
func urlReceived(url: URL) { | ||
guard let token = getTokenFromURL(url: url) else { return } | ||
|
||
removeWindow() | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in | ||
self?.tokenSubject.send(token) | ||
} | ||
} | ||
|
||
// MARK: - Private Helpers | ||
|
||
private func getTokenFromURL(url: URL) -> String? { | ||
guard | ||
let components = URLComponents(url: url, resolvingAgainstBaseURL: false), | ||
let bearer = components.queryItems?.first(where: { $0.name == "bearer" })?.value | ||
else { | ||
return nil | ||
} | ||
return bearer | ||
} | ||
|
||
private func removeWindow() { | ||
if let safariVC = safariViewController { | ||
windowPresenter.dismiss(viewController: safariVC) | ||
} | ||
safariViewController = nil | ||
windowPresenter.hide() | ||
} | ||
} | ||
|
||
// MARK: - SafariViewControllerDelegate | ||
extension AppliverySafariManager: SFSafariViewControllerDelegate { | ||
func safariViewControllerDidFinish(_ controller: SFSafariViewController) { | ||
tokenSubject.send(nil) | ||
removeWindow() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// WindowManager.swift | ||
// Applivery | ||
// | ||
// Created by Fran Alarza on 23/12/24. | ||
// | ||
|
||
import UIKit | ||
|
||
|
||
protocol WindowPresentable: AnyObject { | ||
func present(viewController: UIViewController) | ||
func dismiss(viewController: UIViewController) | ||
func hide() | ||
} | ||
|
||
final class WindowManager: WindowPresentable { | ||
|
||
private var window: UIWindow? | ||
|
||
init() {} | ||
|
||
func present(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 dismiss(viewController: UIViewController) { | ||
viewController.dismiss(animated: true) | ||
} | ||
|
||
func hide() { | ||
window?.isHidden = true | ||
window = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters