Skip to content

Commit

Permalink
feat!: use plugin options for optional plugin parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Nika Hassani committed Apr 23, 2024
1 parent 5d080ce commit ff38d1d
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 129 deletions.
8 changes: 6 additions & 2 deletions canaries/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ Future<void> main() async {
AmplifyAuthCognito(),
AmplifyStorageS3(),
AmplifyAnalyticsPinpoint(),
AmplifyAPI(modelProvider: ModelProvider.instance),
AmplifyDataStore(modelProvider: ModelProvider.instance),
AmplifyAPI(
options: APIPluginOptions(modelProvider: ModelProvider.instance),
),
AmplifyDataStore(
modelProvider: ModelProvider.instance,
),
]);
await Amplify.configure(amplifyconfig);
safePrint('Successfully configured Amplify.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';

part 'pinpoint_config.g.dart';
Expand Down Expand Up @@ -36,9 +35,7 @@ class PinpointPluginConfig
const PinpointPluginConfig({
required this.pinpointAnalytics,
required this.pinpointTargeting,
int autoFlushEventsInterval = 30,
}) : _autoFlushEventsInterval = autoFlushEventsInterval;

});
factory PinpointPluginConfig.fromJson(Map<String, Object?> json) =>
_$PinpointPluginConfigFromJson(json);

Expand All @@ -48,16 +45,8 @@ class PinpointPluginConfig
final PinpointAnalytics pinpointAnalytics;
final PinpointTargeting pinpointTargeting;

final int _autoFlushEventsInterval;

/// The duration in seconds between flushing analytics events to Pinpoint.
@_DurationConverter()
Duration get autoFlushEventsInterval =>
Duration(seconds: _autoFlushEventsInterval);

@override
List<Object?> get props =>
[pinpointAnalytics, pinpointTargeting, autoFlushEventsInterval];
List<Object?> get props => [pinpointAnalytics, pinpointTargeting];

PinpointPluginConfig copyWith({
PinpointAnalytics? pinpointAnalytics,
Expand All @@ -67,8 +56,6 @@ class PinpointPluginConfig
return PinpointPluginConfig(
pinpointAnalytics: pinpointAnalytics ?? this.pinpointAnalytics,
pinpointTargeting: pinpointTargeting ?? this.pinpointTargeting,
autoFlushEventsInterval:
autoFlushEventsInterval ?? _autoFlushEventsInterval,
);
}

Expand Down Expand Up @@ -132,13 +119,3 @@ class PinpointTargeting with AWSEquatable<PinpointTargeting>, AWSSerializable {
@override
Map<String, Object?> toJson() => _$PinpointTargetingToJson(this);
}

class _DurationConverter implements JsonConverter<Duration, int> {
const _DurationConverter();

@override
Duration fromJson(int json) => Duration(seconds: json);

@override
int toJson(Duration object) => object.inSeconds;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/amplify_core/test/config/cli_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const expected = {
region: REGION,
),
pinpointTargeting: PinpointTargeting(region: REGION),
autoFlushEventsInterval: ANALYTICS_FLUSH_INTERVAL,
),
},
),
Expand Down
15 changes: 5 additions & 10 deletions packages/amplify_core/test/config/testdata/cli_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ const _v4analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -227,8 +226,7 @@ const _v5analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -415,8 +413,7 @@ const _v6analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -603,8 +600,7 @@ const _v7analytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down Expand Up @@ -791,8 +787,7 @@ const _vlatestanalytics = '''
},
"pinpointTargeting": {
"region": "$REGION"
},
"autoFlushEventsInterval": $ANALYTICS_FLUSH_INTERVAL
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const child_process = require('child_process');
const REGION = '$REGION';
const API_KEY = '$API_KEY';
const ANALYTICS_APP_ID = '$ANALYTICS_APP_ID';
const ANALYTICS_FLUSH_INTERVAL = '$ANALYTICS_FLUSH_INTERVAL';
const GRAPHQL_ENDPOINT = '$GRAPHQL_ENDPOINT';
const REST_ENDPOINT = '$REST_ENDPOINT';
const BUCKET = '$BUCKET';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const API_KEY = 'MY_KEY';

/// Analytics values
const ANALYTICS_APP_ID = 'analytics_app_id';
const ANALYTICS_FLUSH_INTERVAL = 10;

/// API values
const GRAPHQL_TYPE = 'GraphQL';
Expand Down
6 changes: 4 additions & 2 deletions packages/amplify_datastore/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class _MyAppState extends State<MyApp> {
try {
datastorePlugin = AmplifyDataStore(
modelProvider: ModelProvider.instance,
errorHandler: ((error) =>
{print("Custom ErrorHandler received: " + error.toString())}),
options: DataStorePluginOptions(
errorHandler: ((error) =>
{print("Custom ErrorHandler received: " + error.toString())}),
),
);
await Amplify.addPlugin(datastorePlugin);

Expand Down
71 changes: 48 additions & 23 deletions packages/amplify_datastore/lib/amplify_datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,19 @@ export 'package:amplify_core/src/types/datastore/datastore_types.dart'
class AmplifyDataStore extends DataStorePluginInterface
with AWSDebuggable, AmplifyLoggerMixin {
/// Constructs an AmplifyDataStore plugin with mandatory [modelProvider]
/// and optional datastore configuration properties including
///
/// [syncExpressions]: list of sync expressions to filter datastore sync against
///
/// [syncInterval]: datastore syncing interval (in seconds)
///
/// [syncMaxRecords]: max number of records to sync
///
/// [syncPageSize]: page size to sync
/// and an optional datastore plugin options [options]
AmplifyDataStore({
required ModelProviderInterface modelProvider,
Function(AmplifyException)? errorHandler,
DataStoreConflictHandler? conflictHandler,
List<DataStoreSyncExpression> syncExpressions = const [],
int? syncInterval,
int? syncMaxRecords,
int? syncPageSize,
AuthModeStrategy authModeStrategy = AuthModeStrategy.defaultStrategy,
DataStorePluginOptions options = const DataStorePluginOptions(),
}) : super(
modelProvider: modelProvider,
errorHandler: errorHandler,
conflictHandler: conflictHandler,
syncExpressions: syncExpressions,
syncInterval: syncInterval,
syncMaxRecords: syncMaxRecords,
syncPageSize: syncPageSize,
authModeStrategy: authModeStrategy,
errorHandler: options.errorHandler,
conflictHandler: options.conflictHandler,
syncExpressions: options.syncExpressions,
syncInterval: options.syncInterval,
syncMaxRecords: options.syncMaxRecords,
syncPageSize: options.syncPageSize,
authModeStrategy: options.authModeStrategy,
);

/// Internal use constructor
Expand Down Expand Up @@ -326,3 +312,42 @@ class _NativeAmplifyApi
@override
String get runtimeTypeName => '_NativeAmplifyApi';
}

/// {@template amplify_datastore.datastore_plugin_options}
/// The plugin options for the Amplify DataStore plugin.
/// {@endtemplate}
class DataStorePluginOptions {
/// {@macro amplify_datastore.datastore_plugin_options}
const DataStorePluginOptions({
this.errorHandler,
this.conflictHandler,
this.syncExpressions = const [],
this.syncInterval,
this.syncMaxRecords,
this.syncPageSize,
this.authModeStrategy = AuthModeStrategy.defaultStrategy,
});

/// The custom error handler function that receives an [AmplifyException]
/// object when DataStore encounters an unhandled error.
final Function(AmplifyException)? errorHandler;

/// The custom conflict handler function that receives an [ConflictData]
/// object when DataStore encounters a data conflict.
final DataStoreConflictHandler? conflictHandler;

/// The list of sync expressions to filter datastore sync.
final List<DataStoreSyncExpression> syncExpressions;

/// The syncing interval in seconds.
final int? syncInterval;

/// The max number of records to sync.
final int? syncMaxRecords;

/// The page size to sync.
final int? syncPageSize;

/// The strategy for authorizing an API call.
final AuthModeStrategy authModeStrategy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void main() {
);
AmplifyDataStore dataStore = AmplifyDataStore(
modelProvider: ModelProvider.instance,
errorHandler: (exception) => {receivedException = exception});
options: DataStorePluginOptions(
errorHandler: (exception) => {receivedException = exception}));
return dataStore.configureDataStore();
});

Expand Down
15 changes: 8 additions & 7 deletions packages/amplify_datastore/test/amplify_datastore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ void main() {

AmplifyDataStore dataStore = AmplifyDataStore(
modelProvider: ModelProvider.instance,
syncExpressions: [
DataStoreSyncExpression(Blog.classType, () => Blog.NAME.eq('foo')),
DataStoreSyncExpression(Post.classType, () => Post.TITLE.eq('bar'))
],
syncInterval: mockSyncInterval,
syncMaxRecords: mockSyncMaxRecords,
syncPageSize: mockSyncPagesize);
options: DataStorePluginOptions(
syncExpressions: [
DataStoreSyncExpression(Blog.classType, () => Blog.NAME.eq('foo')),
DataStoreSyncExpression(Post.classType, () => Post.TITLE.eq('bar'))
],
syncInterval: mockSyncInterval,
syncMaxRecords: mockSyncMaxRecords,
syncPageSize: mockSyncPagesize));

final binding = TestWidgetsFlutterBinding.ensureInitialized();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class _MyAppState extends State<MyApp> {
AmplifyAnalyticsPinpoint(
// ignore: invalid_use_of_visible_for_testing_member
secureStorageFactory: storageFactory,
options: const AnalyticsPinpointPluginOptions(
autoFlushEventsInterval: Duration(seconds: 10),
),
),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AmplifyAnalyticsPinpoint extends AmplifyAnalyticsPinpointDart {
AmplifyAnalyticsPinpoint({
@visibleForTesting AppLifecycleProvider? appLifecycleProvider,
@visibleForTesting SecureStorageFactory? secureStorageFactory,
super.options,
}) : super(
pathProvider: FlutterPathProvider(),
legacyNativeDataProvider: FlutterLegacyNativeDataProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
DeviceContextInfoProvider? deviceContextInfoProvider,
AppLifecycleProvider? appLifecycleProvider,
SecureStorageFactory? secureStorageFactory,
AnalyticsPinpointPluginOptions options =
const AnalyticsPinpointPluginOptions(),
}) : _pathProvider = pathProvider,
_legacyNativeDataProvider = legacyNativeDataProvider,
_deviceContextInfoProvider = deviceContextInfoProvider,
_appLifecycleProvider = appLifecycleProvider,
_secureStorageFactory =
secureStorageFactory ?? AmplifySecureStorageWorker.factoryFrom();
secureStorageFactory ?? AmplifySecureStorageWorker.factoryFrom(),
_options = options;

void _ensureConfigured() {
if (!_isConfigured) {
Expand Down Expand Up @@ -73,6 +76,7 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
final SecureStorageFactory _secureStorageFactory;
final DeviceContextInfoProvider? _deviceContextInfoProvider;
final LegacyNativeDataProvider? _legacyNativeDataProvider;
final AnalyticsPinpointPluginOptions _options;

static final _logger = AmplifyLogger.category(Category.analytics);

Expand Down Expand Up @@ -157,7 +161,7 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
},
);

final autoFlushEventsInterval = pinpointConfig.autoFlushEventsInterval;
final autoFlushEventsInterval = _options.autoFlushEventsInterval;

if (autoFlushEventsInterval.isNegative) {
throw ConfigurationError(
Expand Down Expand Up @@ -261,3 +265,16 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
await _eventClient.close();
}
}

/// {@template amplify_analytics_pinpoint_dart.analytics_pinpoint_plugin_options}
/// The plugin options for the Amplify Analytics Pinpoint plugin
/// {@endtemplate}
class AnalyticsPinpointPluginOptions {
/// {@macro amplify_analytics_pinpoint_dart.analytics_pinpoint_plugin_options}
const AnalyticsPinpointPluginOptions({
this.autoFlushEventsInterval = const Duration(seconds: 30),
});

/// The duration in seconds between flushing analytics events to Pinpoint.
final Duration autoFlushEventsInterval;
}
Loading

0 comments on commit ff38d1d

Please sign in to comment.