feat(core): Merge connect and connectAwaitable

This commit is contained in:
PapaTutuWawa 2023-03-11 18:54:36 +01:00
parent 25c778965c
commit 574fdfecaa
2 changed files with 16 additions and 6 deletions

View File

@ -117,19 +117,19 @@ class _MyHomePageState extends State<MyHomePage> {
allowPlainAuth: true, allowPlainAuth: true,
), ),
); );
final result = await connection.connectAwaitable(); final result = await connection.connect(waitUntilLogin: true);
setState(() { setState(() {
connected = result.success; connected = result.isType<bool>() && result.get<bool>();
loading = false; loading = false;
}); });
if (result.error != null) { if (result.isType<XmppConnectionError>()) {
logger.severe(result.error); logger.severe(result.get<XmppConnectionError>());
if (context.mounted) { if (context.mounted) {
showDialog( showDialog(
context: context, context: context,
builder: (_) => AlertDialog( builder: (_) => AlertDialog(
title: const Text('Error'), title: const Text('Error'),
content: Text(result.error.toString()), content: Text(result.get<XmppConnectionError>().toString()),
), ),
); );
} }

View File

@ -458,7 +458,14 @@ class XmppConnection {
// The error is recoverable // The error is recoverable
await _setConnectionState(XmppConnectionState.notConnected); await _setConnectionState(XmppConnectionState.notConnected);
if (await _reconnectionPolicy.getShouldReconnect()) {
await _reconnectionPolicy.onFailure(); await _reconnectionPolicy.onFailure();
} else {
_log.info(
'Not passing connection failure to reconnection policy as it indicates that we should not reconnect',
);
}
} }
/// Called whenever the socket creates an event /// Called whenever the socket creates an event
@ -1170,6 +1177,8 @@ class XmppConnection {
_enableReconnectOnSuccess = enableReconnectOnSuccess; _enableReconnectOnSuccess = enableReconnectOnSuccess;
if (shouldReconnect) { if (shouldReconnect) {
await _reconnectionPolicy.setShouldReconnect(true); await _reconnectionPolicy.setShouldReconnect(true);
} else {
await _reconnectionPolicy.setShouldReconnect(false);
} }
await _reconnectionPolicy.reset(); await _reconnectionPolicy.reset();
@ -1259,6 +1268,7 @@ class XmppConnection {
lastResource: lastResource, lastResource: lastResource,
shouldReconnect: shouldReconnect ?? !waitUntilLogin, shouldReconnect: shouldReconnect ?? !waitUntilLogin,
waitForConnection: waitForConnection, waitForConnection: waitForConnection,
waitUntilLogin: waitUntilLogin,
enableReconnectOnSuccess: enableReconnectOnSuccess, enableReconnectOnSuccess: enableReconnectOnSuccess,
); );
if (waitUntilLogin) { if (waitUntilLogin) {