diff --git a/lib/blocs/auth/auth_bloc.dart b/lib/blocs/auth/auth_bloc.dart index 5e68f5858..91c713fec 100644 --- a/lib/blocs/auth/auth_bloc.dart +++ b/lib/blocs/auth/auth_bloc.dart @@ -100,6 +100,9 @@ class AuthBloc extends Bloc with Loggable { } Future onLogout(AuthLogout event, Emitter emit) async { + await _authRepository.logout(); + await _preferenceRepository.updateUnreadCommentsIds([]); + await _sembastRepository.deleteCachedComments(); emit( state.copyWith( user: const User.empty(), @@ -107,9 +110,6 @@ class AuthBloc extends Bloc with Loggable { agreedToEULA: false, ), ); - await _authRepository.logout(); - await _preferenceRepository.updateUnreadCommentsIds([]); - await _sembastRepository.deleteCachedComments(); logInfo('user logged out'); } diff --git a/lib/screens/settings/settings_screen.dart b/lib/screens/settings/settings_screen.dart index 94aaea8b5..7eae92f38 100644 --- a/lib/screens/settings/settings_screen.dart +++ b/lib/screens/settings/settings_screen.dart @@ -73,24 +73,37 @@ class _SettingsViewState extends State return SingleChildScrollView( child: Column( children: [ - ListTile( - leading: Icon( - Icons.person, - color: isLoggedIn - ? Theme.of(context).colorScheme.primary - : null, - ), - title: Text(isLoggedIn ? 'Log Out' : 'Log In'), - subtitle: isLoggedIn - ? Text(context.read().state.username) - : null, - onTap: () { - if (isLoggedIn) { - onLogoutTapped(); - } else { - onLoginTapped(); + BlocListener( + listenWhen: (AuthState previous, AuthState current) => + previous.isLoggedIn && !current.isLoggedIn, + listener: (BuildContext context, AuthState state) { + if (ModalRoute.of(context)?.isCurrent ?? false) { + showSnackBar( + content: + 'Logged out successfully! ${Constants.happyFace}', + ); + HapticFeedbackUtils.success(); } }, + child: ListTile( + leading: Icon( + Icons.person, + color: isLoggedIn + ? Theme.of(context).colorScheme.primary + : null, + ), + title: Text(isLoggedIn ? 'Log Out' : 'Log In'), + subtitle: isLoggedIn + ? Text(context.read().state.username) + : null, + onTap: () { + if (isLoggedIn) { + onLogoutTapped(); + } else { + onLoginTapped(); + } + }, + ), ), const EnterOfflineModeListTile(), const OfflineListTile(), diff --git a/test/blocs/auth/auth_bloc_test.dart b/test/blocs/auth/auth_bloc_test.dart index 02b2c6842..55f0c426a 100644 --- a/test/blocs/auth/auth_bloc_test.dart +++ b/test/blocs/auth/auth_bloc_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:bloc_test/bloc_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hacki/blocs/blocs.dart'; @@ -144,4 +146,126 @@ void main() { }, ); }); + + group('AuthLogout', () { + const AuthState loggedInState = AuthState( + user: tUser, + isLoggedIn: true, + status: Status.success, + agreedToEULA: false, + ); + + setUp(() { + reset(mockAuthRepository); + reset(mockPreferenceRepository); + reset(mockHackerNewsRepository); + reset(mockSembastRepository); + when( + () => mockAuthRepository.loggedIn, + ).thenAnswer((Invocation _) async => true); + when( + () => mockAuthRepository.username, + ).thenAnswer((Invocation _) async => id); + when( + () => mockHackerNewsRepository.fetchUser(id: id), + ).thenAnswer((Invocation _) async => tUser); + }); + + final List> operationStarted = + List>.generate(3, (int _) => Completer()); + final List> operationCompleted = + List>.generate(3, (int _) => Completer()); + + Future waitForOperation(int index) { + operationStarted[index].complete(); + return operationCompleted[index].future; + } + + blocTest( + 'emits logged-out state only after logout and cleanup complete', + build: () { + when( + mockAuthRepository.logout, + ).thenAnswer((Invocation _) => waitForOperation(0)); + when( + () => mockPreferenceRepository.updateUnreadCommentsIds(any()), + ).thenAnswer((Invocation _) => waitForOperation(1)); + when( + mockSembastRepository.deleteCachedComments, + ).thenAnswer((Invocation _) => waitForOperation(2)); + return AuthBloc( + authRepository: mockAuthRepository, + preferenceRepository: mockPreferenceRepository, + hackerNewsRepository: mockHackerNewsRepository, + sembastRepository: mockSembastRepository, + ); + }, + act: (AuthBloc bloc) async { + await bloc.stream.firstWhere((AuthState state) => state.isLoggedIn); + bloc.add(AuthLogout()); + try { + for (int index = 0; index < operationStarted.length; index++) { + await operationStarted[index].future; + expect(bloc.state, loggedInState); + operationCompleted[index].complete(); + } + await bloc.stream.firstWhere((AuthState state) => !state.isLoggedIn); + } finally { + for (final Completer operation in operationCompleted) { + if (!operation.isCompleted) operation.complete(); + } + } + }, + expect: () => [loggedInState, const AuthState.init()], + verify: (AuthBloc _) { + verifyInOrder( Function()>[ + mockAuthRepository.logout, + () => mockPreferenceRepository.updateUnreadCommentsIds([]), + mockSembastRepository.deleteCachedComments, + ]); + }, + ); + + const List operationNames = [ + 'logout', + 'unread comments cleanup', + 'cached comments cleanup', + ]; + for (final int failingOperation in [0, 1, 2]) { + final StateError error = StateError('Logout operation failed'); + + Future completeOperation(int index) async { + if (index == failingOperation) throw error; + } + + blocTest( + 'does not emit logged-out state when ' + '${operationNames[failingOperation]} fails', + build: () { + when( + mockAuthRepository.logout, + ).thenAnswer((Invocation _) => completeOperation(0)); + when( + () => mockPreferenceRepository.updateUnreadCommentsIds(any()), + ).thenAnswer((Invocation _) => completeOperation(1)); + when( + mockSembastRepository.deleteCachedComments, + ).thenAnswer((Invocation _) => completeOperation(2)); + return AuthBloc( + authRepository: mockAuthRepository, + preferenceRepository: mockPreferenceRepository, + hackerNewsRepository: mockHackerNewsRepository, + sembastRepository: mockSembastRepository, + ); + }, + act: (AuthBloc bloc) async { + await bloc.stream.firstWhere((AuthState state) => state.isLoggedIn); + bloc.add(AuthLogout()); + }, + expect: () => [loggedInState], + errors: () => [error], + verify: (AuthBloc bloc) => expect(bloc.state, loggedInState), + ); + } + }); }