Fix cancel button not working while waiting for server response

This commit is contained in:
Ming Ming 2022-12-13 23:44:14 +08:00
parent d51724a16b
commit bb7919ff15

View file

@ -15,9 +15,15 @@ abstract class AppPasswordExchangeBlocEvent {
const AppPasswordExchangeBlocEvent(); const AppPasswordExchangeBlocEvent();
} }
/// Group of events that are handled sequentially
abstract class AppPasswordExchangeBlocEventGroup1
extends AppPasswordExchangeBlocEvent {
const AppPasswordExchangeBlocEventGroup1();
}
@toString @toString
class AppPasswordExchangeBlocInitiateLogin class AppPasswordExchangeBlocInitiateLogin
extends AppPasswordExchangeBlocEvent { extends AppPasswordExchangeBlocEventGroup1 {
const AppPasswordExchangeBlocInitiateLogin(this.uri); const AppPasswordExchangeBlocInitiateLogin(this.uri);
@override @override
@ -27,7 +33,7 @@ class AppPasswordExchangeBlocInitiateLogin
} }
@toString @toString
class AppPasswordExchangeBlocPoll extends AppPasswordExchangeBlocEvent { class AppPasswordExchangeBlocPoll extends AppPasswordExchangeBlocEventGroup1 {
const AppPasswordExchangeBlocPoll(this.pollOptions); const AppPasswordExchangeBlocPoll(this.pollOptions);
@override @override
@ -46,7 +52,7 @@ class AppPasswordExchangeBlocCancel extends AppPasswordExchangeBlocEvent {
@toString @toString
class _AppPasswordExchangeBlocAppPwReceived class _AppPasswordExchangeBlocAppPwReceived
extends AppPasswordExchangeBlocEvent { extends AppPasswordExchangeBlocEventGroup1 {
const _AppPasswordExchangeBlocAppPwReceived(this.appPasswordResponse); const _AppPasswordExchangeBlocAppPwReceived(this.appPasswordResponse);
@override @override
@ -56,7 +62,8 @@ class _AppPasswordExchangeBlocAppPwReceived
} }
@toString @toString
class _AppPasswordExchangeBlocAppPwFailed extends AppPasswordExchangeBlocEvent { class _AppPasswordExchangeBlocAppPwFailed
extends AppPasswordExchangeBlocEventGroup1 {
const _AppPasswordExchangeBlocAppPwFailed(this.exception); const _AppPasswordExchangeBlocAppPwFailed(this.exception);
@override @override
@ -131,14 +138,21 @@ class AppPasswordExchangeBlocResult extends AppPasswordExchangeBlocState {
class AppPasswordExchangeBloc class AppPasswordExchangeBloc
extends Bloc<AppPasswordExchangeBlocEvent, AppPasswordExchangeBlocState> { extends Bloc<AppPasswordExchangeBlocEvent, AppPasswordExchangeBlocState> {
AppPasswordExchangeBloc() : super(const AppPasswordExchangeBlocInit()) { AppPasswordExchangeBloc() : super(const AppPasswordExchangeBlocInit()) {
on<AppPasswordExchangeBlocEvent>(_onEvent); on<AppPasswordExchangeBlocEventGroup1>(_onEventGroup1);
on<AppPasswordExchangeBlocCancel>(_onEventCancel);
} }
Future<void> _onEvent(AppPasswordExchangeBlocEvent event, @override
Future<void> close() {
_pollPasswordSubscription?.cancel();
return super.close();
}
Future<void> _onEventGroup1(AppPasswordExchangeBlocEvent event,
Emitter<AppPasswordExchangeBlocState> emit) async { Emitter<AppPasswordExchangeBlocState> emit) async {
_log.info("[_onEvent] $event"); _log.info("[_onEventGroup1] $event");
if (_isCanceled) { if (_isCanceled) {
_log.fine("[_onEvent] canceled = true, ignore event"); _log.fine("[_onEventGroup1] canceled = true, ignore event");
return; return;
} }
if (event is AppPasswordExchangeBlocInitiateLogin) { if (event is AppPasswordExchangeBlocInitiateLogin) {
@ -149,8 +163,6 @@ class AppPasswordExchangeBloc
await _onEventAppPasswordReceived(event, emit); await _onEventAppPasswordReceived(event, emit);
} else if (event is _AppPasswordExchangeBlocAppPwFailed) { } else if (event is _AppPasswordExchangeBlocAppPwFailed) {
await _onEventAppPasswordFailure(event, emit); await _onEventAppPasswordFailure(event, emit);
} else if (event is AppPasswordExchangeBlocCancel) {
await _onEventCancel(event, emit);
} }
} }
@ -244,12 +256,6 @@ class AppPasswordExchangeBloc
emit(const AppPasswordExchangeBlocResult(null)); emit(const AppPasswordExchangeBlocResult(null));
} }
@override
Future<void> close() {
_pollPasswordSubscription?.cancel();
return super.close();
}
static final _log = static final _log =
Logger("bloc.app_password_exchange.AppPasswordExchangeBloc"); Logger("bloc.app_password_exchange.AppPasswordExchangeBloc");