Skip to content

Commit

Permalink
feat: detect foreground scene
Browse files Browse the repository at this point in the history
  • Loading branch information
FranAlarza committed Dec 30, 2024
1 parent 4991656 commit 9a13e21
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
7 changes: 5 additions & 2 deletions AppliverySDK/Applivery/AppliverySDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,13 @@ public class AppliverySDK: NSObject {
host = tenant
hostDownload = tenant
self.startInteractor.start()
showFirstWindow()
}

private func showFirstWindow() {
window = AppliveryWindow(frame: UIScreen.main.bounds)
guard window != nil else {
window = AppliveryWindow(frame: UIScreen.main.bounds)
return
}
}

/**
Expand Down Expand Up @@ -369,6 +371,7 @@ public class AppliverySDK: NSObject {
- Version: 2.7
*/
@objc public func feedbackEvent() {
showFirstWindow()
logInfo("Presenting feedback formulary")
app.presentFeedbackForm()
}
Expand Down
19 changes: 11 additions & 8 deletions AppliverySDK/Managers/AppliverySafariManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ final class AppliverySafariManager: NSObject, AppliverySafariManagerProtocol {
func openSafari(from url: URL, from viewController: UIViewController) {
let safariVC = SFSafariViewController(url: url)
safariVC.delegate = self
safariVC.modalPresentationStyle = .fullScreen

viewController.present(safariVC, animated: true, completion: nil)
self.safariViewController = safariVC
}

func closeWebView() {
safariViewController?.dismiss(animated: true, completion: nil)
safariViewController = nil
func closeWebView(completion: (() -> Void)? = nil) {
DispatchQueue.main.async { [weak self] in
self?.safariViewController?.dismiss(animated: true, completion: completion)
self?.safariViewController = nil
}
}

func urlReceived(url: URL) {
if let token = getTokenFromURL(url: url) {
closeWebView()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
closeWebView() { [weak self] in
self?.tokenSubject.send(token)
}
} else {
closeWebView()
}
closeWebView()

}

// MARK: - Private Helpers
Expand All @@ -68,6 +71,6 @@ final class AppliverySafariManager: NSObject, AppliverySafariManagerProtocol {
// MARK: - SafariViewControllerDelegate
extension AppliverySafariManager: SFSafariViewControllerDelegate {
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
tokenSubject.send(nil)
closeWebView()
}
}
4 changes: 1 addition & 3 deletions AppliverySDK/Services/LoginService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ final class LoginService: LoginServiceProtocol {
Task {
if let url = await downloadService.downloadURL(lastBuildId) {
await MainActor.run {
if app.openUrl(url) {
// Do things here
} else {
if app.openUrl(url) {} else {
let error = NSError.appliveryError(literal(.errorDownloadURL))
logError(error)
}
Expand Down
33 changes: 18 additions & 15 deletions AppliverySDK/Wrappers/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,24 @@ class App: AppProtocol {
return isReady
}

func topViewController() -> UIViewController? {

if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = scene.windows.first {

if var topController = window.rootViewController {
while let presentedController = topController.presentedViewController {
topController = presentedController
}

return topController
}
func topViewController() -> UIViewController? {
guard let windowScene = UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.first(where: { $0.activationState == .foregroundActive }) else {
logInfo("The scene is not active")
return nil
}

return nil
}

guard let window = windowScene.windows.first else {
logInfo("The scene is missing a window")
return nil
}

var topController = window.rootViewController
while let presented = topController?.presentedViewController {
topController = presented
}

return topController
}
}

0 comments on commit 9a13e21

Please sign in to comment.