Skip to content

Commit

Permalink
Remove state to track realtime and rest instance creation, and just c…
Browse files Browse the repository at this point in the history
…reate them automatically at launch
  • Loading branch information
ben-xD committed Nov 4, 2021
1 parent 6171f2f commit a7859c2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 68 deletions.
68 changes: 5 additions & 63 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class _MyAppState extends State<MyApp> {
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;
Expand Down Expand Up @@ -83,7 +81,7 @@ class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
initPlatformState();
asyncInitState();
}

@override
Expand All @@ -100,9 +98,7 @@ class _MyAppState extends State<MyApp> {
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
print('initPlatformState()');

Future<void> asyncInitState() async {
String platformVersion;
String ablyVersion;

Expand Down Expand Up @@ -136,13 +132,12 @@ class _MyAppState extends State<MyApp> {
_platformVersion = platformVersion;
_ablyVersion = ablyVersion;
});

createAblyRealtime();
await createAblyRest();
}

Future<void> createAblyRest() async {
setState(() {
_restCreationState = OpState.inProgress;
});

final clientOptions = ably.ClientOptions.fromKey(_apiKey)
..logLevel = ably.LogLevel.verbose
..logHandler = ({msg, exception}) {
Expand All @@ -154,16 +149,12 @@ class _MyAppState extends State<MyApp> {
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';
Expand All @@ -186,10 +177,6 @@ class _MyAppState extends State<MyApp> {
}

void createAblyRealtime() {
setState(() {
_realtimeCreationState = OpState.inProgress;
});

final clientOptions = ably.ClientOptions.fromKey(_apiKey)
..clientId = Constants.clientId
..logLevel = ably.LogLevel.verbose
Expand All @@ -206,13 +193,9 @@ class _MyAppState extends State<MyApp> {
_pushNotificationService.setRealtimeClient(realtime);
setState(() {
_realtime = realtime;
_realtimeCreationState = OpState.succeeded;
});
} on Exception catch (error) {
print('Error creating Ably Realtime: $error');
setState(() {
_realtimeCreationState = OpState.failed;
});
rethrow;
}
}
Expand Down Expand Up @@ -275,39 +258,6 @@ class _MyAppState extends State<MyApp> {
}
}

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,
Expand Down Expand Up @@ -745,11 +695,6 @@ class _MyAppState extends State<MyApp> {
'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(
Expand Down Expand Up @@ -829,9 +774,6 @@ class _MyAppState extends State<MyApp> {
'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'
Expand Down
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a7859c2

Please sign in to comment.