-
Notifications
You must be signed in to change notification settings - Fork 13
Refactored AdyenCardComponent for improved readability and structure #458
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
Changes from 1 commit
280dd66
fe3c7c8
8f22a74
27ad87e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,18 +10,8 @@ import 'package:flutter/foundation.dart'; | |
| import 'package:flutter/gestures.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| class AdyenCardComponent extends StatelessWidget { | ||
| final CardComponentConfiguration configuration; | ||
| final Map<String, dynamic> paymentMethod; | ||
| final Checkout checkout; | ||
| final Future<void> Function(PaymentResult) onPaymentResult; | ||
| final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers; | ||
| final _isStoredPaymentMethodIndicator = | ||
| Constants.isStoredPaymentMethodIndicator; | ||
| final SdkVersionNumberProvider _sdkVersionNumberProvider = | ||
| SdkVersionNumberProvider.instance; | ||
|
|
||
| AdyenCardComponent({ | ||
| class AdyenCardComponent extends StatefulWidget { | ||
| const AdyenCardComponent({ | ||
RahmiTufanoglu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super.key, | ||
| required this.configuration, | ||
| required this.paymentMethod, | ||
|
|
@@ -30,79 +20,73 @@ class AdyenCardComponent extends StatelessWidget { | |
| this.gestureRecognizers, | ||
| }); | ||
|
|
||
| final CardComponentConfiguration configuration; | ||
| final Map<String, dynamic> paymentMethod; | ||
| final Checkout checkout; | ||
| final Future<void> Function(PaymentResult) onPaymentResult; | ||
| final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers; | ||
|
|
||
| @override | ||
| State<AdyenCardComponent> createState() => _AdyenCardComponentState(); | ||
| } | ||
|
|
||
| class _AdyenCardComponentState extends State<AdyenCardComponent> { | ||
| final _isStoredPaymentMethodIndicator = Constants.isStoredPaymentMethodIndicator; | ||
|
|
||
| final SdkVersionNumberProvider _sdkVersionNumberProvider = SdkVersionNumberProvider.instance; | ||
|
|
||
| late final Future<String> _sdkVersionNumber; | ||
RahmiTufanoglu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| _sdkVersionNumber = _sdkVersionNumberProvider.getSdkVersionNumber(); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return FutureBuilder( | ||
| future: _sdkVersionNumberProvider.getSdkVersionNumber(), | ||
| return FutureBuilder<String>( | ||
| future: _sdkVersionNumber, | ||
| builder: (BuildContext context, AsyncSnapshot<String> snapshot) { | ||
| if (snapshot.data != null) { | ||
| final sdkVersionNumber = snapshot.data ?? ""; | ||
| return switch (checkout) { | ||
| SessionCheckout() => _buildCardSessionFlowWidget(sdkVersionNumber), | ||
| AdvancedCheckout it => | ||
| _buildCardAdvancedFlowWidget(sdkVersionNumber, it), | ||
| return switch (widget.checkout) { | ||
| SessionCheckout() => _CardSessionFlowWidget( | ||
| configuration: widget.configuration, | ||
| paymentMethod: widget.paymentMethod, | ||
| checkout: widget.checkout, | ||
| onPaymentResult: widget.onPaymentResult, | ||
| sdkVersionNumber: sdkVersionNumber, | ||
| initialHeight: _determineInitialHeight(widget.configuration.cardConfiguration), | ||
| isStoredPaymentMethod: widget.paymentMethod.containsKey(_isStoredPaymentMethodIndicator), | ||
| ), | ||
| AdvancedCheckout it => _CardAdvancedFlowWidget( | ||
| configuration: widget.configuration, | ||
| paymentMethod: widget.paymentMethod, | ||
| advancedCheckout: it, | ||
| onPaymentResult: widget.onPaymentResult, | ||
| sdkVersionNumber: sdkVersionNumber, | ||
| encodedPaymentMethod: json.encode(widget.paymentMethod), | ||
| gestureRecognizers: widget.gestureRecognizers, | ||
| initialHeight: _determineInitialHeight(widget.configuration.cardConfiguration), | ||
| isStoredPaymentMethod: widget.paymentMethod.containsKey(_isStoredPaymentMethodIndicator), | ||
| ), | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve type safety and reduce redundancy, consider capturing the |
||
| } else { | ||
| return Container( | ||
| height: _determineInitialHeight(configuration.cardConfiguration), | ||
| height: _determineInitialHeight(widget.configuration.cardConfiguration), | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| CardSessionComponent _buildCardSessionFlowWidget(String sdkVersionNumber) { | ||
| final SessionCheckout sessionCheckout = checkout as SessionCheckout; | ||
| final String encodedPaymentMethod = json.encode(paymentMethod); | ||
| final double initialHeight = | ||
| _determineInitialHeight(configuration.cardConfiguration); | ||
| final bool isStoredPaymentMethod = | ||
| paymentMethod.containsKey(_isStoredPaymentMethodIndicator); | ||
|
|
||
| return CardSessionComponent( | ||
| cardComponentConfiguration: configuration.toDTO(sdkVersionNumber), | ||
| paymentMethod: encodedPaymentMethod, | ||
| session: sessionCheckout.toDTO(), | ||
| onPaymentResult: onPaymentResult, | ||
| initialViewHeight: initialHeight, | ||
| isStoredPaymentMethod: isStoredPaymentMethod, | ||
| onBinLookup: configuration.cardConfiguration.onBinLookup, | ||
| onBinValue: configuration.cardConfiguration.onBinValue, | ||
| ); | ||
| } | ||
|
|
||
| CardAdvancedComponent _buildCardAdvancedFlowWidget( | ||
| String sdkVersionNumber, | ||
| Checkout advancedCheckout, | ||
| ) { | ||
| final initialHeight = | ||
| _determineInitialHeight(configuration.cardConfiguration); | ||
| final String encodedPaymentMethod = json.encode(paymentMethod); | ||
| final bool isStoredPaymentMethod = | ||
| paymentMethod.containsKey(_isStoredPaymentMethodIndicator); | ||
|
|
||
| return CardAdvancedComponent( | ||
| cardComponentConfiguration: configuration.toDTO(sdkVersionNumber), | ||
| paymentMethod: encodedPaymentMethod, | ||
| advancedCheckout: advancedCheckout, | ||
| onPaymentResult: onPaymentResult, | ||
| initialViewHeight: initialHeight, | ||
| isStoredPaymentMethod: isStoredPaymentMethod, | ||
| gestureRecognizers: gestureRecognizers, | ||
| onBinLookup: configuration.cardConfiguration.onBinLookup, | ||
| onBinValue: configuration.cardConfiguration.onBinValue, | ||
| ); | ||
| } | ||
|
|
||
| double _determineInitialHeight(CardConfiguration cardConfiguration) { | ||
| switch (defaultTargetPlatform) { | ||
| case TargetPlatform.android: | ||
| return _determineInitialAndroidViewHeight(cardConfiguration); | ||
| case TargetPlatform.iOS: | ||
| return _determineInitialIosViewHeight(cardConfiguration); | ||
| default: | ||
| throw UnsupportedError('Unsupported platform view'); | ||
| } | ||
| return switch (defaultTargetPlatform) { | ||
| TargetPlatform.android => _determineInitialAndroidViewHeight(cardConfiguration), | ||
| TargetPlatform.iOS => _determineInitialIosViewHeight(cardConfiguration), | ||
| _ => throw UnsupportedError('Unsupported platform view'), | ||
| }; | ||
| } | ||
|
|
||
| double _determineInitialAndroidViewHeight( | ||
|
|
@@ -164,3 +148,79 @@ class AdyenCardComponent extends StatelessWidget { | |
| return iosViewHeight; | ||
| } | ||
| } | ||
|
|
||
| class _CardSessionFlowWidget extends StatelessWidget { | ||
| const _CardSessionFlowWidget({ | ||
| required this.configuration, | ||
| required this.paymentMethod, | ||
| required this.checkout, | ||
| required this.onPaymentResult, | ||
| required this.sdkVersionNumber, | ||
| required this.initialHeight, | ||
| required this.isStoredPaymentMethod, | ||
| }); | ||
|
|
||
| final CardComponentConfiguration configuration; | ||
| final Map<String, dynamic> paymentMethod; | ||
| final Checkout checkout; | ||
| final Future<void> Function(PaymentResult) onPaymentResult; | ||
| final String sdkVersionNumber; | ||
| final double initialHeight; | ||
| final bool isStoredPaymentMethod; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final encodedPaymentMethod = json.encode(paymentMethod); | ||
| final sessionCheckout = checkout as SessionCheckout; | ||
|
|
||
| return CardSessionComponent( | ||
| cardComponentConfiguration: configuration.toDTO(sdkVersionNumber), | ||
| paymentMethod: encodedPaymentMethod, | ||
| session: sessionCheckout.toDTO(), | ||
| onPaymentResult: onPaymentResult, | ||
| initialViewHeight: initialHeight, | ||
| isStoredPaymentMethod: isStoredPaymentMethod, | ||
| onBinLookup: configuration.cardConfiguration.onBinLookup, | ||
| onBinValue: configuration.cardConfiguration.onBinValue, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class _CardAdvancedFlowWidget extends StatelessWidget { | ||
| const _CardAdvancedFlowWidget({ | ||
| required this.configuration, | ||
| required this.paymentMethod, | ||
| required this.onPaymentResult, | ||
| required this.sdkVersionNumber, | ||
| required this.encodedPaymentMethod, | ||
| required this.advancedCheckout, | ||
| required this.gestureRecognizers, | ||
| required this.initialHeight, | ||
| required this.isStoredPaymentMethod, | ||
| }); | ||
|
|
||
| final CardComponentConfiguration configuration; | ||
| final Map<String, dynamic> paymentMethod; | ||
| final AdvancedCheckout advancedCheckout; | ||
| final Future<void> Function(PaymentResult) onPaymentResult; | ||
| final String sdkVersionNumber; | ||
| final String encodedPaymentMethod; | ||
| final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers; | ||
| final double initialHeight; | ||
| final bool isStoredPaymentMethod; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return CardAdvancedComponent( | ||
| cardComponentConfiguration: configuration.toDTO(sdkVersionNumber), | ||
| paymentMethod: encodedPaymentMethod, | ||
| advancedCheckout: advancedCheckout, | ||
| onPaymentResult: onPaymentResult, | ||
| initialViewHeight: initialHeight, | ||
| isStoredPaymentMethod: isStoredPaymentMethod, | ||
| gestureRecognizers: gestureRecognizers, | ||
| onBinLookup: configuration.cardConfiguration.onBinLookup, | ||
| onBinValue: configuration.cardConfiguration.onBinValue, | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.