Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 130 additions & 70 deletions lib/src/components/card/adyen_card_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
super.key,
required this.configuration,
required this.paymentMethod,
Expand All @@ -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;

@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),
),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve type safety and reduce redundancy, consider capturing the SessionCheckout instance in the switch statement, similar to how it's done for AdvancedCheckout. This allows passing the correctly typed object to _CardSessionFlowWidget, avoiding the need for casting within that widget. Additionally, pre-calculate initialHeight, isStoredPaymentMethod, and encodedPaymentMethod outside the switch to avoid redundant calculations. Finally, the paymentMethod map is passed to _CardAdvancedFlowWidget but is not used. After refactoring, it will also become unused in _CardSessionFlowWidget.

        if (snapshot.hasData) {
          final sdkVersionNumber = snapshot.data!;
          final initialHeight = _determineInitialHeight(widget.configuration.cardConfiguration);
          final isStoredPaymentMethod = widget.paymentMethod.containsKey(_isStoredPaymentMethodIndicator);
          final encodedPaymentMethod = json.encode(widget.paymentMethod);

          return switch (widget.checkout) {
            SessionCheckout session => _CardSessionFlowWidget(
                configuration: widget.configuration,
                sessionCheckout: session,
                onPaymentResult: widget.onPaymentResult,
                sdkVersionNumber: sdkVersionNumber,
                initialHeight: initialHeight,
                isStoredPaymentMethod: isStoredPaymentMethod,
                encodedPaymentMethod: encodedPaymentMethod,
              ),
            AdvancedCheckout it => _CardAdvancedFlowWidget(
                configuration: widget.configuration,
                advancedCheckout: it,
                onPaymentResult: widget.onPaymentResult,
                sdkVersionNumber: sdkVersionNumber,
                encodedPaymentMethod: encodedPaymentMethod,
                gestureRecognizers: widget.gestureRecognizers,
                initialHeight: initialHeight,
                isStoredPaymentMethod: isStoredPaymentMethod,
              ),
          };
        }

} 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(
Expand Down Expand Up @@ -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,
);
}
}