Skip to content

Commit

Permalink
Beta updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shilgapira committed Feb 9, 2025
1 parent 1f527f2 commit f79e548
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
47 changes: 26 additions & 21 deletions src/flows/FlowBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ protocol FlowBridgeDelegate: AnyObject {
func bridgeDidFailLoading(_ bridge: FlowBridge, error: DescopeError)
func bridgeDidFinishLoading(_ bridge: FlowBridge)
func bridgeDidBecomeReady(_ bridge: FlowBridge)
// func bridgeShouldShowScreen(_ bridge: FlowBridge, screenId: String) -> Bool
// func bridgeDidShowScreen(_ bridge: FlowBridge, screenId: String)
func bridgeDidInterceptNavigation(_ bridge: FlowBridge, url: URL, external: Bool)
func bridgeDidReceiveRequest(_ bridge: FlowBridge, request: FlowBridgeRequest)
func bridgeDidFailAuthentication(_ bridge: FlowBridge, error: DescopeError)
Expand All @@ -18,8 +16,8 @@ protocol FlowBridgeDelegate: AnyObject {
enum FlowBridgeRequest {
case oauthNative(clientId: String, stateId: String, nonce: String, implicit: Bool)
case webAuth(variant: String, startURL: URL, finishURL: URL?)
case beforeScreen(screenId: String)
case afterScreen(screenId: String)
case beforeScreen(screen: String, data: [String: Any])
case afterScreen(screen: String)
}

enum FlowBridgeResponse {
Expand Down Expand Up @@ -248,11 +246,11 @@ private extension FlowBridgeRequest {
}
self = .webAuth(variant: type, startURL: startURL, finishURL: finishURL)
case "beforeScreen":
guard let screenId = payload["screenId"] as? String else { return nil }
self = .beforeScreen(screenId: screenId)
guard let screen = payload["screen"] as? String, let data = payload["data"] as? [String: Any] else { return nil }
self = .beforeScreen(screen: screen, data: data)
case "afterScreen":
guard let screenId = payload["screenId"] as? String else { return nil }
self = .afterScreen(screenId: screenId)
guard let screen = payload["screen"] as? String else { return nil }
self = .afterScreen(screen: screen)
default:
return nil
}
Expand Down Expand Up @@ -325,7 +323,7 @@ private let namespace = "_Descope_Bridge"
private let setupScript = """
// Signal to other subsystems that they're running inside a bridge-enabled webview
window.IsDescopeBridge = true
window.isDescopeBridge = true
// Redirect console to bridge
window.console.log = (s) => { window.webkit.messageHandlers.\(FlowBridgeMessage.log.rawValue).postMessage({ tag: 'log', message: s }) }
Expand All @@ -341,14 +339,13 @@ private let initScript = """
// Called directly below
function \(namespace)_initialize() {
let interval
let component = \(namespace)_find()
if (component) {
\(namespace)_prepare(component)
return
}
let interval
interval = setInterval(() => {
let component = \(namespace)_find()
if (component) {
Expand Down Expand Up @@ -394,36 +391,44 @@ function \(namespace)_prepare(component) {
window.webkit.messageHandlers.\(FlowBridgeMessage.success.rawValue).postMessage(JSON.stringify(event.detail))
})
component.addEventListener('page-updated', (event) => {
component.addEventListener('screen-updated', (event) => {
if (!event.detail.screenName) {
return false
}
window.webkit.messageHandlers.\(FlowBridgeMessage.bridge.rawValue).postMessage({
type: 'afterScreen',
payload: {
screenId: 'todo',
screen: event.detail.screenName,
},
})
})
const previousPageUpdate = component.onPageUpdate
component.onPageUpdate = async (state, ref) => {
const previousScreenUpdate = component.onScreenUpdate
component.onScreenUpdate = async (screen, data, next, ref) => {
if (!screen) {
return false
}
const promise = new Promise((resolve, reject) => {
console.log(`onPageUpdate: ${JSON.stringify(state)}`)
console.log(`onScreenUpdate: ${screen}, ${JSON.stringify(data)}`)
window.webkit.messageHandlers.\(FlowBridgeMessage.bridge.rawValue).postMessage({
type: 'beforeScreen',
payload: {
screenId: state.screenId,
screen,
data,
},
})
component.pendingNext = state.next
component.pendingNext = next
component.pendingResolve = resolve
})
const shouldOverride = await promise
if (!shouldOverride && previousPageUpdate) {
return await previousPageUpdate(state, ref)
if (!shouldOverride && previousScreenUpdate) {
return await previousScreenUpdate(screen, data, next, ref)
}
return shouldOverride
}
component.lazyInit()
// ensure we support old web-components without this function
component.lazyInit?.()
}
// Called when the Descope web-component is ready to notify the bridge
Expand Down
22 changes: 8 additions & 14 deletions src/flows/FlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public protocol DescopeFlowCoordinatorDelegate: AnyObject {
/// and do a quick animatad transition to show the flow once this method is called.
func coordinatorDidBecomeReady(_ coordinator: DescopeFlowCoordinator)

func coordinatorShouldShowScreen(_ coordinator: DescopeFlowCoordinator, screenId: String) -> Bool
func coordinatorShouldShowScreen(_ coordinator: DescopeFlowCoordinator, screen: String, data: [String: Any]) -> Bool

func coordinatorDidShowScreen(_ coordinator: DescopeFlowCoordinator, screenId: String)
func coordinatorDidShowScreen(_ coordinator: DescopeFlowCoordinator, screen: String)

/// Called when the user taps on a web link in the flow.
///
Expand Down Expand Up @@ -272,16 +272,16 @@ public class DescopeFlowCoordinator {
handleOAuthNative(clientId: clientId, stateId: stateId, nonce: nonce, implicit: implicit)
case let .webAuth(variant, startURL, finishURL):
handleWebAuth(variant: variant, startURL: startURL, finishURL: finishURL)
case let .beforeScreen(screenId):
logger(.info, "Should show screen", screenId)
case let .beforeScreen(screen, data):
logger(.info, "Should show screen", screen)
var show = true
if let delegate {
show = delegate.coordinatorShouldShowScreen(self, screenId: screenId)
show = delegate.coordinatorShouldShowScreen(self, screen: screen, data: data)
}
bridge.send(response: .beforeScreen(override: !show))
case let .afterScreen(screenId):
logger(.info, "Did show screen", screenId)
delegate?.coordinatorDidShowScreen(self, screenId: screenId)
case let .afterScreen(screen):
logger(.info, "Did show screen", screen)
delegate?.coordinatorDidShowScreen(self, screen: screen)
}
}

Expand Down Expand Up @@ -385,12 +385,6 @@ extension DescopeFlowCoordinator: FlowBridgeDelegate {
handleReady()
}

// func bridgeShouldShowScreen(_ bridge: FlowBridge, screenId: String) -> Bool {
// }

// func bridgeDidShowScreen(_ bridge: FlowBridge, screenId: String) {
// }

func bridgeDidInterceptNavigation(_ bridge: FlowBridge, url: URL, external: Bool) {
delegate?.coordinatorDidInterceptNavigation(self, url: url, external: external)
}
Expand Down
12 changes: 6 additions & 6 deletions src/flows/FlowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public protocol DescopeFlowViewDelegate: AnyObject {
/// and do a quick animatad transition to show the flow once this method is called.
func flowViewDidBecomeReady(_ flowView: DescopeFlowView)

func flowViewShouldShowScreen(_ flowView: DescopeFlowView, screenId: String) -> Bool
func flowViewShouldShowScreen(_ flowView: DescopeFlowView, screen: String, data: [String: Any]) -> Bool

func flowViewDidShowScreen(_ flowView: DescopeFlowView, screenId: String)
func flowViewDidShowScreen(_ flowView: DescopeFlowView, screen: String)

/// Called when the user taps on a web link in the flow.
///
Expand Down Expand Up @@ -273,14 +273,14 @@ private class FlowCoordinatorDelegateProxy: DescopeFlowCoordinatorDelegate {
view.delegate?.flowViewDidBecomeReady(view)
}

func coordinatorShouldShowScreen(_ coordinator: DescopeFlowCoordinator, screenId: String) -> Bool {
func coordinatorShouldShowScreen(_ coordinator: DescopeFlowCoordinator, screen: String, data: [String: Any]) -> Bool {
guard let view, let delegate = view.delegate else { return true }
return delegate.flowViewShouldShowScreen(view, screenId: screenId)
return delegate.flowViewShouldShowScreen(view, screen: screen, data: data)
}

func coordinatorDidShowScreen(_ coordinator: DescopeFlowCoordinator, screenId: String) {
func coordinatorDidShowScreen(_ coordinator: DescopeFlowCoordinator, screen: String) {
guard let view else { return }
view.delegate?.flowViewDidShowScreen(view, screenId: screenId)
view.delegate?.flowViewDidShowScreen(view, screen: screen)
}

func coordinatorDidInterceptNavigation(_ coordinator: DescopeFlowCoordinator, url: URL, external: Bool) {
Expand Down
4 changes: 2 additions & 2 deletions src/flows/FlowViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ open class DescopeFlowViewController: UIViewController {
}

extension DescopeFlowViewController: DescopeFlowViewDelegate {
public func flowViewShouldShowScreen(_ flowView: DescopeFlowView, screenId: String) -> Bool {
public func flowViewShouldShowScreen(_ flowView: DescopeFlowView, screen: String, data: [String: Any]) -> Bool {
return true // TODO
}

public func flowViewDidShowScreen(_ flowView: DescopeFlowView, screenId: String) {
public func flowViewDidShowScreen(_ flowView: DescopeFlowView, screen: String) {
// TODO
}

Expand Down

0 comments on commit f79e548

Please sign in to comment.