Last active
January 7, 2021 17:06
-
-
Save laptopmutia/acb3811a31682cd99a9e4f3d262c226c to your computer and use it in GitHub Desktop.
I'm trying to refactor bloc into cubit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:authentication_repository/authentication_repository.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:equatable/equatable.dart'; | |
import 'package:meta/meta.dart'; | |
import 'package:pedantic/pedantic.dart'; | |
part 'authentication_event.dart'; | |
part 'authentication_state.dart'; | |
class AuthenticationBloc | |
extends Bloc<AuthenticationEvent, AuthenticationState> { | |
AuthenticationBloc({ | |
@required AuthenticationRepository authenticationRepository, | |
}) : assert(authenticationRepository != null), | |
_authenticationRepository = authenticationRepository, | |
super(const AuthenticationState.unknown()) { | |
_userSubscription = _authenticationRepository.user.listen( | |
(user) => add(AuthenticationUserChanged(user)), | |
); | |
} | |
final AuthenticationRepository _authenticationRepository; | |
StreamSubscription<User> _userSubscription; | |
@override | |
Stream<AuthenticationState> mapEventToState( | |
AuthenticationEvent event, | |
) async* { | |
if (event is AuthenticationUserChanged) { | |
yield _mapAuthenticationUserChangedToState(event); | |
} else if (event is AuthenticationLogoutRequested) { | |
unawaited(_authenticationRepository.logOut()); | |
} | |
} | |
@override | |
Future<void> close() { | |
_userSubscription?.cancel(); | |
return super.close(); | |
} | |
AuthenticationState _mapAuthenticationUserChangedToState( | |
AuthenticationUserChanged event, | |
) { | |
return event.user != User.empty | |
? AuthenticationState.authenticated(event.user) | |
: const AuthenticationState.unauthenticated(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
part of 'authentication_bloc.dart'; | |
abstract class AuthenticationEvent extends Equatable { | |
const AuthenticationEvent(); | |
@override | |
List<Object> get props => []; | |
} | |
class AuthenticationUserChanged extends AuthenticationEvent { | |
const AuthenticationUserChanged(this.user); | |
final User user; | |
@override | |
List<Object> get props => [user]; | |
} | |
class AuthenticationLogoutRequested extends AuthenticationEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
part of 'authentication_bloc.dart'; | |
enum AuthenticationStatus { authenticated, unauthenticated, unknown } | |
class AuthenticationState extends Equatable { | |
const AuthenticationState._({ | |
this.status = AuthenticationStatus.unknown, | |
this.user = User.empty, | |
}); | |
const AuthenticationState.unknown() : this._(); | |
const AuthenticationState.authenticated(User user) | |
: this._(status: AuthenticationStatus.authenticated, user: user); | |
const AuthenticationState.unauthenticated() | |
: this._(status: AuthenticationStatus.unauthenticated); | |
final AuthenticationStatus status; | |
final User user; | |
@override | |
List<Object> get props => [status, user]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:authentication_repository/authentication_repository.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:equatable/equatable.dart'; | |
import 'package:meta/meta.dart'; | |
import 'package:pedantic/pedantic.dart'; | |
part 'authentication_state.dart'; | |
class AuthenticationCubit extends Cubit<AuthenticationState> { | |
AuthenticationCubit({ | |
@required AuthenticationRepository authenticationRepository, | |
}) : assert(authenticationRepository != null), | |
_authenticationRepository = authenticationRepository, | |
super(const AuthenticationState.unknown()) { | |
_userSubscription = _authenticationRepository.user.listen( | |
(user) => emit(user != User.empty | |
? AuthenticationState.authenticated(user) | |
: const AuthenticationState.unauthenticated()), | |
); | |
} | |
final AuthenticationRepository _authenticationRepository; | |
StreamSubscription<User> _userSubscription; | |
@override | |
Future<void> close() { | |
_userSubscription?.cancel(); | |
return super.close(); | |
} | |
Future<void> authenticationLogoutRequested() async { | |
emit(const AuthenticationState.unauthenticated()); | |
unawaited(_authenticationRepository.logOut()); | |
} | |
Future<void> authenticationUserChanged(User user) async { | |
user != User.empty | |
? emit(AuthenticationState.authenticated(user)) | |
: emit(const AuthenticationState.unauthenticated()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment