Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace modifier specific context providers with a generic #25

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 27 additions & 42 deletions Sources/SafariUI/WebAuthentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public struct WebAuthentication {
context.coordinator.isPresented = isPresented
}

final class Coordinator: NSObject {
final class Coordinator: NSObject, WebAuthenticationCoordinator {
init(parent: Presenter) {
self.parent = parent
}
Expand All @@ -119,7 +119,7 @@ public struct WebAuthentication {
}
}

private lazy var contextProvider = ContextProvider(coordinator: self)
private lazy var contextProvider = ContextProvider<Coordinator>(coordinator: self)

private weak var session: ASWebAuthenticationSession?

Expand All @@ -146,25 +146,6 @@ public struct WebAuthentication {

self.session = session
}

private final class ContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {

// MARK: - Initializers

init(coordinator: Coordinator) {
self.coordinator = coordinator
}

// MARK: - API

unowned var coordinator: Coordinator

// MARK: - ASWebAuthenticationPresentationContextProviding

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
coordinator.view.window ?? ASPresentationAnchor()
}
}
}
}
}
Expand Down Expand Up @@ -222,7 +203,7 @@ public struct WebAuthentication {
context.coordinator.item = item
}

final class Coordinator: NSObject {
final class Coordinator: NSObject, WebAuthenticationCoordinator {

// MARK: - Initializers

Expand Down Expand Up @@ -252,7 +233,7 @@ public struct WebAuthentication {

// MARK: - Private

private lazy var contextProvider = ContextProvider(coordinator: self)
private lazy var contextProvider = ContextProvider<Coordinator>(coordinator: self)

private weak var session: ASWebAuthenticationSession?

Expand All @@ -279,25 +260,6 @@ public struct WebAuthentication {

self.session = session
}

private final class ContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {

// MARK: - Initializers

init(coordinator: Coordinator) {
self.coordinator = coordinator
}

// MARK: - API

unowned var coordinator: Coordinator

// MARK: - ASWebAuthenticationPresentationContextProviding

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
coordinator.view.window ?? ASPresentationAnchor()
}
}
}
}
}
Expand Down Expand Up @@ -344,4 +306,27 @@ public struct WebAuthentication {
}
}

private final class ContextProvider<T: WebAuthenticationCoordinator>: NSObject, ASWebAuthenticationPresentationContextProviding {

// MARK: - Initializers

init(coordinator: T) {
self.coordinator = coordinator
}

// MARK: - API

unowned var coordinator: T

// MARK: - ASWebAuthenticationPresentationContextProviding

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
coordinator.view.window ?? ASPresentationAnchor()
}
}

private protocol WebAuthenticationCoordinator: NSObject {
var view: UIView { get }
}

private struct UnknownError: Error {}