Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
122 changes: 52 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,71 @@ 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() => CardSessionComponent(
cardComponentConfiguration: widget.configuration.toDTO(sdkVersionNumber),
session: (widget.checkout as SessionCheckout).toDTO(),
paymentMethod: json.encode(widget.paymentMethod),
onPaymentResult: widget.onPaymentResult,
initialViewHeight: _determineInitialHeight(widget.configuration.cardConfiguration),
isStoredPaymentMethod: widget.paymentMethod.containsKey(_isStoredPaymentMethodIndicator),
gestureRecognizers: widget.gestureRecognizers,
),
AdvancedCheckout it => CardAdvancedComponent(
cardComponentConfiguration: widget.configuration.toDTO(sdkVersionNumber),
paymentMethod: json.encode(widget.paymentMethod),
onPaymentResult: widget.onPaymentResult,
advancedCheckout: it,
initialViewHeight: _determineInitialHeight(widget.configuration.cardConfiguration),
isStoredPaymentMethod: widget.paymentMethod.containsKey(_isStoredPaymentMethodIndicator),
gestureRecognizers: widget.gestureRecognizers,
),
};
} 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
3 changes: 1 addition & 2 deletions lib/src/components/card/base_card_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,13 @@ class _BaseCardComponentState extends State<BaseCardComponent> {

@override
void initState() {
super.initState();
_cardWidget = _buildCardWidget();
_componentCommunicationStream = _componentFlutterApi
.componentCommunicationStream.stream
.where((communicationModel) =>
communicationModel.componentId == widget.componentId)
.listen(_onComponentCommunication);

super.initState();
}

@override
Expand Down
14 changes: 4 additions & 10 deletions lib/src/components/card/card_component_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,15 @@ class CardComponentContainer extends StatelessWidget {
if (viewportHeight == null)
SizedBox(
height: initialViewPortHeight,
child: _buildLoadingWidget(),
child: switch (defaultTargetPlatform) {
TargetPlatform.android => const Center(child: CircularProgressIndicator()),
_ => const Center(child: SizedBox.shrink()),
},
)
],
);
}

Widget _buildLoadingWidget() {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return const Center(child: CircularProgressIndicator());
default:
return const Center(child: SizedBox.shrink());
}
}

double _determineHeight(int? height) {
return height == null ? initialViewPortHeight : height + bottomSpacing;
}
Expand Down