Skip to content

Commit

Permalink
feat(stripe_terminal): Added to Terminal.setSimulatorConfiguration
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
BreX900 committed Nov 25, 2023
1 parent 17e6264 commit 816c77a
Show file tree
Hide file tree
Showing 22 changed files with 775 additions and 258 deletions.
10 changes: 6 additions & 4 deletions one_for_all_generator/lib/src/generators/dart_api_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DartApiBuilder extends ApiBuilder {

void _updateHostApiMethod(MethodElement e, MethodBuilder b) {
b
..annotations.add(CodeExpression(Code('override')))
..returns = Reference('${e.returnType}')
..name = e.name
..requiredParameters.addAll(e.parameters.where((e) => !e.isNamed && e.isRequired).map((e) {
Expand All @@ -54,6 +55,7 @@ class DartApiBuilder extends ApiBuilder {

_library.body.add(Class((b) => b
..name = '_\$${codecs.encodeName(element.name)}'
..implements.add(Reference(element.name))
..fields.add(Field((b) => b
..static = true
..modifier = FieldModifier.constant
Expand Down Expand Up @@ -147,8 +149,8 @@ channel.setMethodCallHandler((call) async {

@override
void writeSerializableClass(SerializableClassHandler handler, {bool withName = false}) {
final SerializableClassHandler(:element, :flutterToHost, :hostToFlutter, :children) = handler;
final fields = element.fields.where((e) => !e.isStatic && e.isFinal && !e.hasInitializer);
final SerializableClassHandler(:element, :flutterToHost, :hostToFlutter, :params, :children) =
handler;

final serializedRef = const Reference('List<Object?>');
final deserializedRef = Reference(element.name);
Expand Down Expand Up @@ -184,7 +186,7 @@ channel.setMethodCallHandler((call) async {
..lambda = true
..body = Code('[${[
if (withName) '\'${element.name}\'',
...fields.map((e) {
...params.map((e) {
return codecs.encodeSerialization(e.type, 'deserialized.${e.name}');
}),
].join(',')}]')));
Expand All @@ -198,7 +200,7 @@ channel.setMethodCallHandler((call) async {
..type = serializedRef
..name = 'serialized'))
..lambda = true
..body = Code('${element.name}(${fields.mapIndexed((i, e) {
..body = Code('${element.name}(${params.mapIndexed((i, e) {
return '${e.name}: ${codecs.encodeDeserialization(e.type, 'serialized[$i]')}';
}).join(',')})')));
}
Expand Down
12 changes: 6 additions & 6 deletions one_for_all_generator/lib/src/generators/kotlin_api_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ return suspendCoroutine { continuation ->
@override
void writeSerializableClass(SerializableClassHandler handler, {ClassElement? extend}) {
if (!handler.kotlinGeneration) return;
final SerializableClassHandler(:element, :flutterToHost, :hostToFlutter, :children) = handler;
final fields = element.fields.where((e) => !e.isStatic && e.isFinal && !e.hasInitializer);
final SerializableClassHandler(:element, :flutterToHost, :hostToFlutter, :params, :children) =
handler;

if (children != null) {
_specs.add(KotlinClass(
Expand Down Expand Up @@ -411,10 +411,10 @@ return suspendCoroutine { continuation ->
}

_specs.add(KotlinClass(
modifier: fields.isNotEmpty ? KotlinClassModifier.data : null,
modifier: params.isNotEmpty ? KotlinClassModifier.data : null,
name: codecs.encodeName(element.name),
extend: extend != null ? '${codecs.encodeName(extend.name)}()' : null,
initializers: fields.map((e) {
initializers: params.map((e) {
return KotlinField(
name: _encodeVarName(e.name),
type: codecs.encodeType(e.type),
Expand All @@ -425,7 +425,7 @@ return suspendCoroutine { continuation ->
KotlinMethod(
name: 'serialize',
returns: 'List<Any?>',
body: 'return listOf(\n${fields.map((e) {
body: 'return listOf(\n${params.map((e) {
return ' ${codecs.encodeSerialization(e.type, _encodeVarName(e.name))},\n';
}).join()})',
),
Expand All @@ -443,7 +443,7 @@ return suspendCoroutine { continuation ->
),
],
returns: codecs.encodeType(element.thisType),
body: 'return ${codecs.encodeName(element.name)}(\n${fields.mapIndexed((i, e) {
body: 'return ${codecs.encodeName(element.name)}(\n${params.mapIndexed((i, e) {
return ' ${_encodeVarName(e.name)} = ${codecs.encodeDeserialization(e.type, 'serialized[$i]')},\n';
}).join()})',
),
Expand Down
10 changes: 5 additions & 5 deletions one_for_all_generator/lib/src/generators/swift_api_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ return try await withCheckedThrowingContinuation { continuation in
@override
void writeSerializableClass(SerializableClassHandler handler, {ClassElement? extend}) {
if (!handler.swiftGeneration) return;
final SerializableClassHandler(:element, :flutterToHost, :hostToFlutter, :children) = handler;
final fields = element.fields.where((e) => !e.isStatic && e.isFinal && !e.hasInitializer);
final SerializableClassHandler(:element, :flutterToHost, :hostToFlutter, :params, :children) =
handler;

if (children != null) {
_specs.add(SwiftProtocol(
Expand Down Expand Up @@ -451,7 +451,7 @@ return try await withCheckedThrowingContinuation { continuation in
_specs.add(SwiftStruct(
name: codecs.encodeName(element.name),
implements: [if (extend != null) codecs.encodeName(extend.name)],
fields: fields.map((e) {
fields: params.map((e) {
return SwiftField(
name: _encodeVarName(e.name),
type: codecs.encodeType(e.type),
Expand All @@ -462,7 +462,7 @@ return try await withCheckedThrowingContinuation { continuation in
SwiftMethod(
name: 'serialize',
returns: '[Any?]',
body: 'return [\n${fields.map((e) {
body: 'return [\n${params.map((e) {
return ' ${codecs.encodeSerialization(e.type, _encodeVarName(e.name))},\n';
}).join()}]',
),
Expand All @@ -478,7 +478,7 @@ return try await withCheckedThrowingContinuation { continuation in
),
],
returns: codecs.encodeType(element.thisType),
body: 'return ${codecs.encodeName(element.name)}(\n${fields.mapIndexed((i, e) {
body: 'return ${codecs.encodeName(element.name)}(\n${params.mapIndexed((i, e) {
return ' ${_encodeVarName(e.name)}: ${codecs.encodeDeserialization(e.type, 'serialized[$i]')}';
}).join(',\n')}\n)',
),
Expand Down
26 changes: 25 additions & 1 deletion one_for_all_generator/lib/src/handlers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:equatable/equatable.dart';
import 'package:one_for_all/one_for_all.dart';
import 'package:one_for_all_generator/one_for_all_generator.dart';
Expand Down Expand Up @@ -131,7 +132,7 @@ class SerializableClassHandler extends SerializableHandler<ClassElement> {

final List<SerializableClassHandler>? children;

const SerializableClassHandler({
SerializableClassHandler({
required super.element,
required super.kotlinGeneration,
required super.swiftGeneration,
Expand Down Expand Up @@ -163,6 +164,13 @@ class SerializableClassHandler extends SerializableHandler<ClassElement> {
);
}

late final List<SerializableParamHandler> params = flutterToHost
? element.fields
.where((e) => !e.isStatic && e.isFinal && !e.hasInitializer)
.map(SerializableParamHandler.fromField)
.toList()
: element.unnamedConstructor!.parameters.map(SerializableParamHandler.fromParameter).toList();

@override
SerializableClassHandler apply({
bool flutterToHost = false,
Expand All @@ -181,6 +189,22 @@ class SerializableClassHandler extends SerializableHandler<ClassElement> {
}
}

class SerializableParamHandler {
final DartType type;
final String name;
final String? defaultValueCode;

SerializableParamHandler.fromParameter(ParameterElement element)
: type = element.type,
name = element.name,
defaultValueCode = element.defaultValueCode;

SerializableParamHandler.fromField(FieldElement element)
: type = element.type,
name = element.name,
defaultValueCode = null;
}

class SerializableEnumHandler extends SerializableHandler {
static const _annotationChecker = TypeChecker.fromRuntime(SerializableEnum);

Expand Down
3 changes: 3 additions & 0 deletions stripe_terminal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

## 3.2.0
- feat: Added to `Terminal.setSimulatorConfiguration` method
- chore(android): Replaced `compileSdkVersion` with `compileSdk` and bumped version to `34`
- chore(android): Bumped tools gradle version to `8.1.3`
- feat: Bumped [Android](https://github.com/stripe/stripe-terminal-android/blob/master/CHANGELOG.md#320---2023-11-15)
Expand Down
4 changes: 2 additions & 2 deletions stripe_terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ more simply by supporting streams instead of callbacks for listeners
## Features

All features of android and ios sdk are supported (Also the TapToPay feature)
- [Android sdk](https://github.com/stripe/stripe-terminal-android) version: 3.1.0
- [IOS sdk](https://github.com/stripe/stripe-terminal-ios) version: 3.1.0
- [Android sdk](https://github.com/stripe/stripe-terminal-android) version: 3.2.0
- [IOS sdk](https://github.com/stripe/stripe-terminal-ios) version: 3.2.0

> Offline mode is not supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import mek.stripeterminal.api.RefundApi
import mek.stripeterminal.api.Result
import mek.stripeterminal.api.SetupIntentApi
import mek.stripeterminal.api.SetupIntentUsageApi
import mek.stripeterminal.api.SimulatorConfigurationApi
import mek.stripeterminal.api.TerminalExceptionCodeApi
import mek.stripeterminal.api.TerminalHandlersApi
import mek.stripeterminal.api.TerminalPlatformApi
Expand Down Expand Up @@ -295,6 +296,10 @@ class TerminalPlugin : FlutterPlugin, ActivityAware, TerminalPlatformApi {
},
)
}

override fun onSetSimulatorConfiguration(configuration: SimulatorConfigurationApi) {
Terminal.getInstance().simulatorConfiguration = configuration.toHost()
}
// endregion

// region Taking Payment
Expand Down Expand Up @@ -402,8 +407,8 @@ class TerminalPlugin : FlutterPlugin, ActivityAware, TerminalPlatformApi {
}

override fun onSuccess(paymentIntent: PaymentIntent) {
result.success(paymentIntent.toApi())
paymentIntents.remove(paymentIntent.id)
result.success(paymentIntent.toApi())
}
},
)
Expand Down
Loading

0 comments on commit 816c77a

Please sign in to comment.