From 535a3b1d70380d5a536088dacea6c022de290359 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 6 Jul 2021 14:12:10 +0200 Subject: [PATCH] feat: Make it possible to get the current loginState You may have missed the last valid loginState from the stream if you listen too late to it. This makes it possible to get always the current loginState. --- lib/src/client.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 0946b367..445b74b6 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -192,6 +192,13 @@ class Client extends MatrixApi { String _deviceName; /// Returns the current login state. + LoginState get loginState => __loginState; + LoginState __loginState; + set _loginState(LoginState state) { + __loginState = state; + onLoginStateChanged.add(state); + } + bool isLogged() => accessToken != null; /// A list of all rooms the user is participating or invited. @@ -884,7 +891,7 @@ class Client extends MatrixApi { // we aren't logged in encryption?.dispose(); encryption = null; - onLoginStateChanged.add(LoginState.loggedOut); + _loginState = LoginState.loggedOut; Logs().i('User is not logged in.'); _initLock = false; return; @@ -934,8 +941,7 @@ class Client extends MatrixApi { presences.clear(); } _initLock = false; - - onLoginStateChanged.add(LoginState.logged); + _loginState = LoginState.logged; Logs().i( 'Successfully connected as ${userID.localpart} with ${homeserver.toString()}', ); @@ -981,7 +987,7 @@ class Client extends MatrixApi { await databaseDestroyer(this); _database = null; } - onLoginStateChanged.add(LoginState.loggedOut); + _loginState = LoginState.loggedOut; } bool _backgroundSync = true;