From 02d81caa7d6d14928ecb7471311ce3f067bcb134 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:59:45 +0100 Subject: [PATCH] wait for the film to edited --- lib/screens/film_edit/bloc_film_edit.dart | 30 ++-- lib/screens/film_edit/screen_film_edit.dart | 145 +++++++++++--------- lib/screens/film_edit/state_film_edit.dart | 19 ++- 3 files changed, 112 insertions(+), 82 deletions(-) diff --git a/lib/screens/film_edit/bloc_film_edit.dart b/lib/screens/film_edit/bloc_film_edit.dart index f956e71..fb41886 100644 --- a/lib/screens/film_edit/bloc_film_edit.dart +++ b/lib/screens/film_edit/bloc_film_edit.dart @@ -45,18 +45,18 @@ class FilmEditBloc extends Bloc { ), ) { on( - (event, emit) { + (event, emit) async { switch (event) { case final FilmEditNameChangedEvent e: - _onNameChanged(e, emit); + await _onNameChanged(e, emit); case final FilmEditIsoChangedEvent e: - _onIsoChanged(e, emit); + await _onIsoChanged(e, emit); case final FilmEditExpChangedEvent e: - _onExpChanged(e, emit); + await _onExpChanged(e, emit); case FilmEditSaveEvent(): - _onSave(event, emit); + await _onSave(event, emit); case FilmEditDeleteEvent(): - _onDelete(event, emit); + await _onDelete(event, emit); } }, ); @@ -65,10 +65,8 @@ class FilmEditBloc extends Bloc { Future _onNameChanged(FilmEditNameChangedEvent event, Emitter emit) async { _newFilm = _newFilm.copyWith(name: event.name); emit( - FilmEditState( + state.copyWith( name: event.name, - isoValue: state.isoValue, - exponent: state.exponent, canSave: _canSave(event.name, state.exponent), ), ); @@ -77,10 +75,8 @@ class FilmEditBloc extends Bloc { Future _onIsoChanged(FilmEditIsoChangedEvent event, Emitter emit) async { _newFilm = _newFilm.copyWith(iso: event.iso.value); emit( - FilmEditState( - name: state.name, + state.copyWith( isoValue: event.iso, - exponent: state.exponent, canSave: _canSave(state.name, state.exponent), ), ); @@ -101,8 +97,9 @@ class FilmEditBloc extends Bloc { } Future _onSave(FilmEditSaveEvent _, Emitter emit) async { + emit(state.copyWith(isLoading: true)); if (_isEdit) { - filmsProvider.updateCustomFilm( + await filmsProvider.updateCustomFilm( FilmExponential( id: _originalFilm.id, name: state.name, @@ -111,7 +108,7 @@ class FilmEditBloc extends Bloc { ), ); } else { - filmsProvider.addCustomFilm( + await filmsProvider.addCustomFilm( FilmExponential( id: const Uuid().v1(), name: state.name, @@ -120,10 +117,13 @@ class FilmEditBloc extends Bloc { ), ); } + emit(state.copyWith(isLoading: false)); } Future _onDelete(FilmEditDeleteEvent _, Emitter emit) async { - filmsProvider.deleteCustomFilm(_originalFilm); + emit(state.copyWith(isLoading: true)); + await filmsProvider.deleteCustomFilm(_originalFilm); + emit(state.copyWith(isLoading: false)); } bool _canSave(String name, double? exponent) { diff --git a/lib/screens/film_edit/screen_film_edit.dart b/lib/screens/film_edit/screen_film_edit.dart index 7692f9c..dbd6b80 100644 --- a/lib/screens/film_edit/screen_film_edit.dart +++ b/lib/screens/film_edit/screen_film_edit.dart @@ -25,79 +25,94 @@ class FilmEditScreen extends StatefulWidget { class _FilmEditScreenState extends State { @override Widget build(BuildContext context) { - return SliverScreen( - title: Text(widget.isEdit ? S.of(context).editFilmTitle : S.of(context).addFilmTitle), - appBarActions: [ - BlocBuilder( - buildWhen: (previous, current) => previous.canSave != current.canSave, - builder: (context, state) => IconButton( - onPressed: state.canSave - ? () { - context.read().add(const FilmEditSaveEvent()); - Navigator.of(context).pop(); - } - : null, - icon: const Icon(Icons.save), - ), - ), - if (widget.isEdit) - IconButton( - onPressed: () { - context.read().add(const FilmEditDeleteEvent()); - Navigator.of(context).pop(); - }, - icon: const Icon(Icons.delete), - ), - ], - slivers: [ - SliverToBoxAdapter( - child: Padding( - padding: const EdgeInsets.fromLTRB( - Dimens.paddingM, - 0, - Dimens.paddingM, - Dimens.paddingM, + return BlocConsumer( + listenWhen: (previous, current) => previous.isLoading != current.isLoading, + listener: (context, state) { + if (state.isLoading) { + FocusScope.of(context).unfocus(); + } else { + Navigator.of(context).pop(); + } + }, + buildWhen: (previous, current) => previous.isLoading != current.isLoading, + builder: (context, state) => IgnorePointer( + ignoring: state.isLoading, + child: SliverScreen( + title: Text(widget.isEdit ? S.of(context).editFilmTitle : S.of(context).addFilmTitle), + appBarActions: [ + BlocBuilder( + buildWhen: (previous, current) => previous.canSave != current.canSave, + builder: (context, state) => IconButton( + onPressed: state.canSave + ? () { + context.read().add(const FilmEditSaveEvent()); + } + : null, + icon: const Icon(Icons.save), + ), ), - child: Card( + if (widget.isEdit) + IconButton( + onPressed: () { + context.read().add(const FilmEditDeleteEvent()); + }, + icon: const Icon(Icons.delete), + ), + ], + slivers: [ + SliverToBoxAdapter( child: Padding( - padding: const EdgeInsets.symmetric(vertical: Dimens.paddingM), - child: Column( - children: [ - BlocBuilder( - buildWhen: (previous, current) => previous.name != current.name, - builder: (context, state) => FilmEditNameField( - name: state.name, - onChanged: (value) { - context.read().add(FilmEditNameChangedEvent(value)); - }, + padding: const EdgeInsets.fromLTRB( + Dimens.paddingM, + 0, + Dimens.paddingM, + Dimens.paddingM, + ), + child: Card( + child: Padding( + padding: const EdgeInsets.symmetric(vertical: Dimens.paddingM), + child: Opacity( + opacity: state.isLoading ? Dimens.disabledOpacity : Dimens.enabledOpacity, + child: Column( + children: [ + BlocBuilder( + buildWhen: (previous, current) => previous.name != current.name, + builder: (context, state) => FilmEditNameField( + name: state.name, + onChanged: (value) { + context.read().add(FilmEditNameChangedEvent(value)); + }, + ), + ), + BlocBuilder( + buildWhen: (previous, current) => previous.isoValue != current.isoValue, + builder: (context, state) => FilmEditIsoPicker( + selected: state.isoValue, + onChanged: (value) { + context.read().add(FilmEditIsoChangedEvent(value)); + }, + ), + ), + BlocBuilder( + buildWhen: (previous, current) => previous.exponent != current.exponent, + builder: (context, state) => FilmEditExponentialFormulaInput( + value: state.exponent, + onChanged: (value) { + context.read().add(FilmEditExpChangedEvent(value)); + }, + ), + ), + ], ), ), - BlocBuilder( - buildWhen: (previous, current) => previous.isoValue != current.isoValue, - builder: (context, state) => FilmEditIsoPicker( - selected: state.isoValue, - onChanged: (value) { - context.read().add(FilmEditIsoChangedEvent(value)); - }, - ), - ), - BlocBuilder( - buildWhen: (previous, current) => previous.exponent != current.exponent, - builder: (context, state) => FilmEditExponentialFormulaInput( - value: state.exponent, - onChanged: (value) { - context.read().add(FilmEditExpChangedEvent(value)); - }, - ), - ), - ], + ), ), ), ), - ), + SliverToBoxAdapter(child: SizedBox(height: MediaQuery.paddingOf(context).bottom)), + ], ), - SliverToBoxAdapter(child: SizedBox(height: MediaQuery.paddingOf(context).bottom)), - ], + ), ); } } diff --git a/lib/screens/film_edit/state_film_edit.dart b/lib/screens/film_edit/state_film_edit.dart index caa25c0..1572931 100644 --- a/lib/screens/film_edit/state_film_edit.dart +++ b/lib/screens/film_edit/state_film_edit.dart @@ -5,13 +5,28 @@ class FilmEditState { final IsoValue isoValue; final double? exponent; final bool canSave; - final FilmExponential? filmToSave; + final bool isLoading; const FilmEditState({ required this.name, required this.isoValue, required this.exponent, required this.canSave, - this.filmToSave, + this.isLoading = false, }); + + FilmEditState copyWith({ + String? name, + IsoValue? isoValue, + double? exponent, + bool? canSave, + bool? isLoading, + }) => + FilmEditState( + name: name ?? this.name, + isoValue: isoValue ?? this.isoValue, + exponent: exponent ?? this.exponent, + canSave: canSave ?? this.canSave, + isLoading: isLoading ?? this.isLoading, + ); }