diff options
Diffstat (limited to 'lib/auth.dart')
-rw-r--r-- | lib/auth.dart | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/auth.dart b/lib/auth.dart new file mode 100644 index 0000000..6ee8a0b --- /dev/null +++ b/lib/auth.dart @@ -0,0 +1,43 @@ +import 'package:beam_messenger/data/database_helper.dart'; + +enum AuthState { LOGGED_IN, LOGGED_OUT } + +abstract class AuthStateListener { + void onAuthStateChanged(AuthState state); +} + +// A naive implementation of Observer/Subscriber Pattern. Will do for now. +class AuthStateProvider { + static final AuthStateProvider _instance = new AuthStateProvider.internal(); + + List<AuthStateListener> _subscribers; + + factory AuthStateProvider() => _instance; + AuthStateProvider.internal() { + _subscribers = new List<AuthStateListener>(); + initState(); + } + + void initState() async { + var db = new DatabaseHelper(); + var isLoggedIn = await db.isLoggedIn(); + if (isLoggedIn) + notify(AuthState.LOGGED_IN); + else + notify(AuthState.LOGGED_OUT); + } + + void subscribe(AuthStateListener listener) { + _subscribers.add(listener); + } + + void dispose(AuthStateListener listener) { + for (var l in _subscribers) { + if (l == listener) _subscribers.remove(l); + } + } + + void notify(AuthState state) { + _subscribers.forEach((AuthStateListener s) => s.onAuthStateChanged(state)); + } +} |