-
Notifications
You must be signed in to change notification settings - Fork 1k
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
e844c0f
commit b00b9bd
Showing
5 changed files
with
121 additions
and
1 deletion.
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
84 changes: 84 additions & 0 deletions
84
StripeConnect/StripeConnect/Source/Components/NotificationBannerViewController.swift
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,84 @@ | ||
// | ||
// NotificationBannerViewController.swift | ||
// StripeConnectTests | ||
// | ||
// Created by Mel Ludowise on 9/25/24. | ||
// | ||
|
||
import UIKit | ||
|
||
@_spi(DashboardOnly) | ||
@available(iOS 15, *) | ||
public class NotificationBannerViewController: UIViewController { | ||
let webView: ConnectComponentWebView | ||
|
||
public weak var delegate: NotificationBannerViewControllerDelegate? | ||
|
||
init(componentManager: EmbeddedComponentManager, | ||
collectionOptions: AccountCollectionOptions) { | ||
webView = ConnectComponentWebView( | ||
componentManager: componentManager, | ||
componentType: .notificationBanner | ||
) | ||
super.init(nibName: nil, bundle: nil) | ||
|
||
webView.addMessageHandler(OnLoadErrorMessageHandler { [weak self] value in | ||
guard let self else { return } | ||
self.delegate?.notificationBanner(self, didFailLoadWithError: value.error.connectEmbedError) | ||
}) | ||
webView.addMessageHandler(OnNotificationsChangeHandler { [weak self] value in | ||
guard let self else { return } | ||
self.delegate?.notificationBanner(self, didChangeWithTotal: value.total, andActionRequired: value.actionRequired) | ||
}) | ||
|
||
// TODO(MXMOBILE-2796): Send collection options to web view | ||
|
||
webView.presentPopup = { [weak self] vc in | ||
self?.present(vc, animated: true) | ||
} | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
public override func loadView() { | ||
view = webView | ||
} | ||
} | ||
|
||
@_spi(DashboardOnly) | ||
@available(iOS 15, *) | ||
public protocol NotificationBannerViewControllerDelegate: AnyObject { | ||
/** | ||
Triggered when an error occurs loading the notification banner component | ||
- Parameters: | ||
- notificationBanner: The notification banner component that errored when loading | ||
- error: The error that occurred when loading the component | ||
*/ | ||
func notificationBanner(_ notificationBanner: NotificationBannerViewController, | ||
didFailLoadWithError error: Error) | ||
|
||
/** | ||
Triggered when the total number of notifications or notifications with required actions updates | ||
- Parameters: | ||
- notificationBanner: The notification banner component that changed | ||
- total: The total number of notifications in the banner | ||
- actionRequired: The number of notifications that require user action | ||
*/ | ||
func notificationBanner(_ notificationBanner: NotificationBannerViewController, | ||
didChangeWithTotal total: Int, | ||
andActionRequired actionRequired: Int) | ||
} | ||
|
||
@_spi(DashboardOnly) | ||
@available(iOS 15, *) | ||
public extension NotificationBannerViewControllerDelegate { | ||
// Default implementation to make optional | ||
func notificationBanner(_ notificationBanner: NotificationBannerViewController, | ||
didFailLoadWithError error: Error) { } | ||
|
||
func notificationBanner(_ notificationBanner: NotificationBannerViewController, | ||
didChangeWithTotal total: Int, | ||
andActionRequired actionRequired: Int) { } | ||
} |
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
19 changes: 19 additions & 0 deletions
19
.../StripeConnect/Source/Internal/Webview/MessageHandlers/OnNotificationsChangeHandler.swift
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,19 @@ | ||
// | ||
// OnNotificationsChangeHandler.swift | ||
// StripeConnect | ||
// | ||
// Created by Mel Ludowise on 9/25/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class OnNotificationsChangeHandler: OnSetterFunctionCalledMessageHandler.Handler { | ||
struct Values: Codable, Equatable { | ||
let total: Int | ||
let actionRequired: Int | ||
} | ||
|
||
init(didReceiveMessage: @escaping (Values) -> Void) { | ||
super.init(setter: "setOnNotificationsChange", didReceiveMessage: didReceiveMessage) | ||
} | ||
} |