Skip to content

Commit

Permalink
feat: create ios addressheet implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Dec 22, 2024
1 parent a276f4d commit 9b7b321
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/stripe/lib/src/widgets/adresssheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
130 changes: 130 additions & 0 deletions packages/stripe_ios/ios/Classes/AddressSheetFactory.swift
Original file line number Diff line number Diff line change
@@ -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<AnyHashable, Any>?) {
channel.invokeMethod("onSubmitAction", arguments: addressData)
}

public func onCancelAction(errorData: Dictionary<AnyHashable, Any>?) {
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)


}
}
5 changes: 5 additions & 0 deletions packages/stripe_ios/ios/Classes/StripePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 46 in packages/stripe_ios/ios/Classes/StripePlugin.swift

View workflow job for this annotation

GitHub Actions / Typo CI

Addressheet

"Addressheet" is a typo. Did you mean "Addressee"?
let addressSheetFactory = AddressSheetViewFactory(messenger: registrar.messenger(), delegate: instance)
registrar.register(addressSheetFactory, withId: "flutter.stripe/address_sheet")

}

init(channel : FlutterMethodChannel) {
Expand Down

0 comments on commit 9b7b321

Please sign in to comment.