From 9b7b321a630bd80a5b9c311f7bc4c093944dec81 Mon Sep 17 00:00:00 2001 From: Remon Date: Mon, 16 Dec 2024 17:06:41 +0000 Subject: [PATCH] feat: create ios addressheet implementation --- .../stripe/lib/src/widgets/adresssheet.dart | 4 +- .../ios/Classes/AddressSheetFactory.swift | 130 ++++++++++++++++++ .../stripe_ios/ios/Classes/StripePlugin.swift | 5 + 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 packages/stripe_ios/ios/Classes/AddressSheetFactory.swift diff --git a/packages/stripe/lib/src/widgets/adresssheet.dart b/packages/stripe/lib/src/widgets/adresssheet.dart index 836cf144..932f194e 100644 --- a/packages/stripe/lib/src/widgets/adresssheet.dart +++ b/packages/stripe/lib/src/widgets/adresssheet.dart @@ -89,7 +89,9 @@ class _AddressSheetState extends State<_AddressSheet> { child: defaultTargetPlatform == TargetPlatform.iOS ? UiKitView( viewType: _viewType, - creationParams: const StandardMessageCodec(), + creationParamsCodec: const StandardMessageCodec(), + creationParams: widget.addressSheetParams.toJson(), + onPlatformViewCreated: onPlatformViewCreated, ) : PlatformViewLink( surfaceFactory: (context, controller) { diff --git a/packages/stripe_ios/ios/Classes/AddressSheetFactory.swift b/packages/stripe_ios/ios/Classes/AddressSheetFactory.swift new file mode 100644 index 00000000..f6e8afd8 --- /dev/null +++ b/packages/stripe_ios/ios/Classes/AddressSheetFactory.swift @@ -0,0 +1,130 @@ +// +// AddressSheetFactory.swift +// stripe_ios +// +// Created by Remon on 16/12/2024. +// + +import Foundation + +public class AddressSheetViewFactory: NSObject, FlutterPlatformViewFactory{ + + private var messenger: FlutterBinaryMessenger + private var delegate: ViewManagerDelegate + + init(messenger: FlutterBinaryMessenger, delegate: ViewManagerDelegate) { + self.messenger = messenger + self.delegate = delegate + super.init() + } + + public func create( + withFrame frame: CGRect, + viewIdentifier viewId: Int64, + arguments args: Any? + ) -> FlutterPlatformView { + let view = AddressSheetPlatformView( + frame: frame, + viewIdentifier: viewId, + arguments: args, + binaryMessenger: messenger) + return view + } + + public func createArgsCodec() -> any FlutterMessageCodec & NSObjectProtocol { + return FlutterStandardMessageCodec.sharedInstance() + } +} + +class AddressSheetPlatformView: NSObject, FlutterPlatformView { + let formView: AddressSheetView + + private let channel: FlutterMethodChannel + func view() -> UIView { + return formView + } + + init( frame: CGRect, + viewIdentifier viewId: Int64, + arguments args: Any?, + binaryMessenger messenger: FlutterBinaryMessenger){ + + channel = FlutterMethodChannel(name: "flutter.stripe/address_sheet/\(viewId)", + binaryMessenger: messenger) + + formView = AddressSheetView() + super.init() + channel.setMethodCallHandler(handle) + + formView.onSubmitAction = onCompleteAction + formView.onErrorAction = onCancelAction + + updateProps(args) + + } + + public func onCompleteAction(addressData: Dictionary?) { + channel.invokeMethod("onSubmitAction", arguments: addressData) + } + + public func onCancelAction(errorData: Dictionary?) { + channel.invokeMethod("onErrorAction", arguments: errorData) + } + + public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + switch call.method { + default: + result(FlutterMethodNotImplemented) + } + } + + func updateProps (_ args : Any? ) { + guard let arguments = args as? [String: Any] else { + return + } + + if let presentationStyle = arguments["presentationStyle"] as? String{ + formView.presentationStyle = presentationStyle + } + + if let animationStyle = arguments["animationStyle"] as? String{ + formView.animationStyle = animationStyle + } + + if let visible = arguments["visible"] as? Bool { + formView.visible = visible + } + + if let appearance = arguments["appearance"] as? NSDictionary { + formView.appearance = appearance + } + + if let defaultValues = arguments["defaultValues"] as? NSDictionary { + formView.defaultValues = defaultValues + } + + if let additionalFields = arguments["additionalFields"] as? NSDictionary { + formView.additionalFields = additionalFields + } + + if let allowedCountries = arguments["allowedCountries"] as? [String] { + formView.allowedCountries = allowedCountries + } + + if let autocompleteCountries = arguments["autocompleteCountries"] as? [String] { + formView.autocompleteCountries = autocompleteCountries + } + + if let primaryButtonTitle = arguments["primaryButtonTitle"] as? String { + formView.primaryButtonTitle = primaryButtonTitle + } + + if let sheetTitle = arguments["sheetTitle"] as? String { + formView.sheetTitle = sheetTitle + } + + formView.didSetProps(nil) + + + } +} diff --git a/packages/stripe_ios/ios/Classes/StripePlugin.swift b/packages/stripe_ios/ios/Classes/StripePlugin.swift index e92fc950..f9ccede5 100644 --- a/packages/stripe_ios/ios/Classes/StripePlugin.swift +++ b/packages/stripe_ios/ios/Classes/StripePlugin.swift @@ -42,6 +42,11 @@ class StripePlugin: StripeSdk, FlutterPlugin, ViewManagerDelegate { // Apple Pay Button let applePayFactory = ApplePayButtonViewFactory(messenger: registrar.messenger(),stripeSdk: instance) registrar.register(applePayFactory, withId: "flutter.stripe/apple_pay") + + // Addressheet + let addressSheetFactory = AddressSheetViewFactory(messenger: registrar.messenger(), delegate: instance) + registrar.register(addressSheetFactory, withId: "flutter.stripe/address_sheet") + } init(channel : FlutterMethodChannel) {