diff options
author | Marvin Borner | 2018-07-20 18:14:18 +0200 |
---|---|---|
committer | Marvin Borner | 2018-07-20 18:14:18 +0200 |
commit | 4263997c3e419ef1cf7447d447c5582a322acf74 (patch) | |
tree | d6b5a7c836ba74d1a57f3e6b1a8f372064864c13 /lib/auth.dart | |
parent | 18b8bd888a13c6dadfb7961cc88214332b6f5d38 (diff) |
Began basic login screen feature (mvp architecture)
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)); + } +} |