Skip to content

Commit

Permalink
wip: async & decoding subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Equartey committed Mar 27, 2024
1 parent f053eaa commit 6c9d523
Show file tree
Hide file tree
Showing 12 changed files with 720 additions and 50 deletions.

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

40 changes: 37 additions & 3 deletions packages/amplify_datastore/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ library sample_app;

import 'dart:async';

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_datastore/amplify_datastore.dart';
import 'package:amplify_datastore_example/amplifyconfiguration.dart';
// Uncomment the below line to enable online sync
// import 'package:amplify_api/amplify_api.dart';

Expand All @@ -20,6 +22,7 @@ part 'queries_display_widgets.dart';
part 'save_model_widgets.dart';

void main() {
// AWSLogger().logLevel = LogLevel.verbose;
runApp(MyApp());
}

Expand Down Expand Up @@ -76,20 +79,22 @@ class _MyAppState extends State<MyApp> {
errorHandler: ((error) =>
{print("Custom ErrorHandler received: " + error.toString())}),
);
await Amplify.addPlugin(
AmplifyAPI(modelProvider: ModelProvider.instance));
await Amplify.addPlugin(datastorePlugin);

// Configure

// Uncomment the below lines to enable online sync.
// await Amplify.addPlugin(AmplifyAPI());
// await Amplify.configure(amplifyconfig);
await Amplify.configure(amplifyconfig);

// Remove this line when using the lines above for online sync
await Amplify.configure("{}");
// await Amplify.configure("{}");
} on AmplifyAlreadyConfiguredException {
print(
'Amplify was already configured. Looks like app restarted on android.');
}
Amplify.DataStore.start();
listenToHub();

Amplify.DataStore.observeQuery(
Expand Down Expand Up @@ -255,6 +260,26 @@ class _MyAppState extends State<MyApp> {
});
}

void subExample() {
final req = ModelSubscriptions.onCreate(
Blog.classType,
authorizationMode: APIAuthorizationType.apiKey,
);
final sub = Amplify.API.subscribe(req);

sub.listen((event) {
print(event);
});
}

void start() {
Amplify.DataStore.start();
}

void stop() {
Amplify.DataStore.stop();
}

@override
Widget build(BuildContext context) {
executeAfterBuild();
Expand Down Expand Up @@ -311,6 +336,15 @@ class _MyAppState extends State<MyApp> {

Padding(padding: EdgeInsets.all(10.0)),

Row(
children: [
ElevatedButton(onPressed: start, child: const Text('Start')),
ElevatedButton(onPressed: stop, child: const Text('Stop')),
ElevatedButton(
onPressed: subExample, child: const Text('Subscribe')),
],
),

// Row for query buttons
displayQueryButtons(
_isAmplifyConfigured, _queriesToView, updateQueriesToView),
Expand Down
Loading

0 comments on commit 6c9d523

Please sign in to comment.