From a7859c230b3b2a1e4f5ec7f129eef8cbadff6aaa Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 4 Nov 2021 15:34:46 +0000 Subject: [PATCH] Remove state to track realtime and rest instance creation, and just create them automatically at launch --- example/lib/main.dart | 68 ++++--------------------------------------- example/pubspec.lock | 10 +++---- 2 files changed, 10 insertions(+), 68 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ce8d66afa..8e266cd80 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -32,8 +32,6 @@ class _MyAppState extends State { String _ablyVersion = 'Unknown'; final String _apiKey = const String.fromEnvironment(Constants.ablyApiKey); - OpState _realtimeCreationState = OpState.notStarted; - OpState _restCreationState = OpState.notStarted; ably.Realtime? _realtime; ably.Rest? _rest; ably.ConnectionState? _realtimeConnectionState; @@ -83,7 +81,7 @@ class _MyAppState extends State { @override void initState() { super.initState(); - initPlatformState(); + asyncInitState(); } @override @@ -100,9 +98,7 @@ class _MyAppState extends State { } // Platform messages are asynchronous, so we initialize in an async method. - Future initPlatformState() async { - print('initPlatformState()'); - + Future asyncInitState() async { String platformVersion; String ablyVersion; @@ -136,13 +132,12 @@ class _MyAppState extends State { _platformVersion = platformVersion; _ablyVersion = ablyVersion; }); + + createAblyRealtime(); + await createAblyRest(); } Future createAblyRest() async { - setState(() { - _restCreationState = OpState.inProgress; - }); - final clientOptions = ably.ClientOptions.fromKey(_apiKey) ..logLevel = ably.LogLevel.verbose ..logHandler = ({msg, exception}) { @@ -154,16 +149,12 @@ class _MyAppState extends State { rest = ably.Rest(options: clientOptions); } on Exception catch (error) { print('Error creating Ably Rest: $error'); - setState(() { - _restCreationState = OpState.failed; - }); rethrow; } _pushNotificationService.setRestClient(rest); setState(() { _rest = rest; - _restCreationState = OpState.succeeded; }); const name = 'Hello'; @@ -186,10 +177,6 @@ class _MyAppState extends State { } void createAblyRealtime() { - setState(() { - _realtimeCreationState = OpState.inProgress; - }); - final clientOptions = ably.ClientOptions.fromKey(_apiKey) ..clientId = Constants.clientId ..logLevel = ably.LogLevel.verbose @@ -206,13 +193,9 @@ class _MyAppState extends State { _pushNotificationService.setRealtimeClient(realtime); setState(() { _realtime = realtime; - _realtimeCreationState = OpState.succeeded; }); } on Exception catch (error) { print('Error creating Ably Realtime: $error'); - setState(() { - _realtimeCreationState = OpState.failed; - }); rethrow; } } @@ -275,39 +258,6 @@ class _MyAppState extends State { } } - static Widget button( - final OpState state, - void Function() action, - String actionDescription, - String operatingDescription, - String doneDescription, - ) => - FlatButton( - onPressed: (state == OpState.notStarted || state == OpState.failed) - ? action - : null, - color: opStateColor(state), - disabledColor: opStateColor(state), - child: Text( - opStateDescription( - state, - actionDescription, - operatingDescription, - doneDescription, - ), - ), - ); - - Widget createRestButton() => button(_restCreationState, createAblyRest, - 'Create Ably Rest', 'Create Ably Rest', 'Ably Rest Created'); - - Widget createRealtimeButton() => button( - _realtimeCreationState, - createAblyRealtime, - 'Create Ably Realtime', - 'Creating Ably Realtime', - 'Ably Realtime Created'); - Widget createRTConnectButton() => FlatButton( padding: EdgeInsets.zero, onPressed: (_realtime == null) ? null : _realtime!.connect, @@ -745,11 +695,6 @@ class _MyAppState extends State { 'Realtime', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), - createRealtimeButton(), - Text( - 'Realtime:' - ' ${_realtime?.toString() ?? 'Realtime not created yet.'}', - ), Text('Connection State: $_realtimeConnectionState'), Text('Channel State: $_realtimeChannelState'), Row( @@ -829,9 +774,6 @@ class _MyAppState extends State { 'Rest', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), - createRestButton(), - Text('Rest: ' - '${_rest?.toString() ?? 'Ably Rest not created yet.'}'), sendRestMessage(), Text( 'Rest: press this button to publish a new message with' diff --git a/example/pubspec.lock b/example/pubspec.lock index 3bd30a74c..d6f74da4a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,14 +7,14 @@ packages: path: ".." relative: true source: path - version: "1.2.2" + version: "1.2.3" async: dependency: "direct main" description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.6.1" + version: "2.8.1" boolean_selector: dependency: transitive description: @@ -35,7 +35,7 @@ packages: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.1" clock: dependency: transitive description: @@ -204,7 +204,7 @@ packages: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.7.0" path: dependency: transitive description: @@ -300,7 +300,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.3.0" + version: "0.4.2" timezone: dependency: transitive description: