diff --git a/lib/blocs/hide/hide_bloc.dart b/lib/blocs/hide/hide_bloc.dart index abef7fe0ff..443a86ed22 100644 --- a/lib/blocs/hide/hide_bloc.dart +++ b/lib/blocs/hide/hide_bloc.dart @@ -72,6 +72,7 @@ class HideBloc extends Bloc { on(_onConfirmUploadEvent); on(_onSelectUploadMethodEvent); on(_refreshTurboBalance); + on(_onErrorEvent); } bool get _useTurboUpload => @@ -199,18 +200,10 @@ class HideBloc extends Bloc { )); } - try { - await _computeCostEstimate(); - await _computeBalanceEstimate(); - _computeIsFreeThanksToTurbo(); - _computeIsSufficientBalance(); - } catch (e) { - emit(const FailureHideState( - hideAction: HideAction.hideFolder, - message: 'Error while computing cost estimate', - )); - return; - } + await _computeCostEstimate(); + await _computeBalanceEstimate(); + _computeIsFreeThanksToTurbo(); + _computeIsSufficientBalance(); emit( ConfirmingHideState( @@ -428,6 +421,13 @@ class HideBloc extends Bloc { ); } + void _onErrorEvent( + ErrorEvent event, + Emitter emit, + ) { + emit(FailureHideState(hideAction: event.hideAction)); + } + void _computeIsFreeThanksToTurbo() { final allowedDataItemSizeForTurbo = _appConfig.allowedDataItemSizeForTurbo; final forceNoFreeThanksToTurbo = _appConfig.forceNoFreeThanksToTurbo; @@ -563,4 +563,14 @@ class HideBloc extends Bloc { totalSize: _totalSize, ); } + + @override + void onError(Object error, StackTrace stackTrace) { + add(ErrorEvent( + error: error, + stackTrace: stackTrace, + hideAction: state.hideAction, + )); + super.onError(error, stackTrace); + } } diff --git a/lib/blocs/hide/hide_event.dart b/lib/blocs/hide/hide_event.dart index ef81ea9633..7e5049a3fe 100644 --- a/lib/blocs/hide/hide_event.dart +++ b/lib/blocs/hide/hide_event.dart @@ -1,3 +1,4 @@ +import 'package:ardrive/blocs/hide/hide_state.dart'; import 'package:ardrive/blocs/upload/upload_cubit.dart'; import 'package:ardrive_utils/ardrive_utils.dart'; import 'package:equatable/equatable.dart'; @@ -82,3 +83,21 @@ class RefreshTurboBalanceEvent extends HideEvent { @override List get props => []; } + +class ErrorEvent extends HideEvent { + final Object error; + final StackTrace stackTrace; + final HideAction hideAction; + + const ErrorEvent({ + required this.error, + required this.stackTrace, + required this.hideAction, + }); + + @override + List get props => [ + error, + stackTrace, + ]; +} diff --git a/lib/blocs/hide/hide_state.dart b/lib/blocs/hide/hide_state.dart index 4cc6e4e476..588a9d9fb4 100644 --- a/lib/blocs/hide/hide_state.dart +++ b/lib/blocs/hide/hide_state.dart @@ -24,7 +24,7 @@ class UploadingHideState extends HideState { }); @override - List get props => []; + List get props => []; } class PreparingAndSigningHideState extends HideState { @@ -33,7 +33,7 @@ class PreparingAndSigningHideState extends HideState { }); @override - List get props => []; + List get props => []; } class ConfirmingHideState extends HideState { @@ -127,19 +127,16 @@ class SuccessHideState extends HideState { }); @override - List get props => []; + List get props => []; } class FailureHideState extends HideState { - final String message; - const FailureHideState({ - required this.message, required super.hideAction, }); @override - List get props => [message]; + List get props => []; } enum HideAction { diff --git a/lib/components/hide_dialog.dart b/lib/components/hide_dialog.dart index 8a45650058..f35d80876a 100644 --- a/lib/components/hide_dialog.dart +++ b/lib/components/hide_dialog.dart @@ -41,15 +41,29 @@ class HideDialog extends StatelessWidget { }, builder: (context, state) { return ArDriveStandardModal( - title: _buildTitle(state.hideAction), - content: _buildContent(), + title: _buildTitle(state), + content: _buildContent(state), actions: _buildActions(context, state), ); }, ); } - String _buildTitle(HideAction hideAction) { + String _buildTitle(HideState state) { + final hideAction = state.hideAction; + if (state is FailureHideState) { + switch (hideAction) { + case HideAction.hideFile: + return 'Failed to hide file'; // TODO: localize + case HideAction.hideFolder: + return 'Failed to hide folder'; // TODO: localize + case HideAction.unhideFile: + return 'Failed to unhide file'; // TODO: localize + case HideAction.unhideFolder: + return 'Failed to unhide folder'; // TODO: localize + } + } + switch (hideAction) { case HideAction.hideFile: return 'Hiding file'; // TODO: localize @@ -62,7 +76,30 @@ class HideDialog extends StatelessWidget { } } - Widget _buildContent() { + Widget _buildContent(HideState state) { + if (state is FailureHideState) { + final hideAction = state.hideAction; + + switch (hideAction) { + case HideAction.hideFile: + return const Text( + 'Failed to hide file, please try again', // TODO: localize + ); + case HideAction.hideFolder: + return const Text( + 'Failed to hide folder, please try again', // TODO: localize + ); + case HideAction.unhideFile: + return const Text( + 'Failed to unhide file, please try again', // TODO: localize + ); + case HideAction.unhideFolder: + return const Text( + 'Failed to unhide folder, please try again', // TODO: localize + ); + } + } + return const Column( children: [ Center( @@ -82,7 +119,7 @@ class HideDialog extends StatelessWidget { action: () { Navigator.of(context).pop(); }, - title: appLocalizationsOf(context).cancel, + title: appLocalizationsOf(context).close, ), ]; } else {