formz icon indicating copy to clipboard operation
formz copied to clipboard

Add `isInProgressOrSuccess` to `FormzSubmissionStatusX` extension

Open maRci002 opened this issue 1 year ago • 1 comments

Use case

In many cases this is the flow in cubit / bloc when operating with form inputs:

Future<void> submit() async {
  if (state.status.isInProgress || state.status.isSuccess || !state.isValid) {
    return;
  }

  emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
  try {
    await repository.doSomething();
    emit(state.copyWith(status: FormzSubmissionStatus.success));
  } catch (e) {
    emit(state.copyWith(failure: '$e', status: FormzSubmissionStatus.failure));
  }
}

And this happens on UI:

final isInProgress = context.select<MyCubit, bool>(
  (cubit) => cubit.state.status.isInProgress || cubit.state.status.isSuccess,
);

return ElevatedButton(
  onPressed: isInProgress ? null : () => context.read<MyCubit>().submit(),
  child: const Text('Submit'),
);

Proposal

Instead of cubit.state.status.isInProgress || cubit.state.status.isSuccess it would be much easier to write cubit.state.status.isInProgressOrSuccess.

Including myself I only check FormzSubmissionStatusX.isInProgress which can lead to unexpetcted behaviour since in case of succes emit(state.copyWith(status: FormzSubmissionStatus.success)); the ElevatedButton will be clickable which can happen since flutter animates the screens in and out or worse when CircularProgressIndicator is used in case of isInProgress which will be visible for some frames. (Usually when staus becomes succes then form page is closed and still visible while page fades away)

When developer tries to write isI or progr then IDE's autocomplete will show isInProgressOrSuccess along side isInProgress and they will read the docs or just read the changelog.

Note: this proposal should be aplied to 0.5.0-dev.1 I hope it will make to stable version since #48 refactor is awesome and logical.

maRci002 avatar Sep 21 '22 14:09 maRci002

the ElevatedButton will be clickable which can happen since flutter animates the screens in and out

This might not be true: flutter/flutter#4770 However ElevatedButton will be showed as enabled instead of disabled while changing pages or CircularProgressIndicator will be on screen.

maRci002 avatar Sep 21 '22 14:09 maRci002