Skip to content

Commit

Permalink
feat(hide feature): sets up error handling PE-5309
Browse files Browse the repository at this point in the history
  • Loading branch information
matibat committed Jan 5, 2024
1 parent 340bca1 commit 2fe2208
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 24 deletions.
34 changes: 22 additions & 12 deletions lib/blocs/hide/hide_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class HideBloc extends Bloc<HideEvent, HideState> {
on<ConfirmUploadEvent>(_onConfirmUploadEvent);
on<SelectUploadMethodEvent>(_onSelectUploadMethodEvent);
on<RefreshTurboBalanceEvent>(_refreshTurboBalance);
on<ErrorEvent>(_onErrorEvent);
}

bool get _useTurboUpload =>
Expand Down Expand Up @@ -199,18 +200,10 @@ class HideBloc extends Bloc<HideEvent, HideState> {
));
}

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(
Expand Down Expand Up @@ -428,6 +421,13 @@ class HideBloc extends Bloc<HideEvent, HideState> {
);
}

void _onErrorEvent(
ErrorEvent event,
Emitter<HideState> emit,
) {
emit(FailureHideState(hideAction: event.hideAction));
}

void _computeIsFreeThanksToTurbo() {
final allowedDataItemSizeForTurbo = _appConfig.allowedDataItemSizeForTurbo;
final forceNoFreeThanksToTurbo = _appConfig.forceNoFreeThanksToTurbo;
Expand Down Expand Up @@ -563,4 +563,14 @@ class HideBloc extends Bloc<HideEvent, HideState> {
totalSize: _totalSize,
);
}

@override
void onError(Object error, StackTrace stackTrace) {
add(ErrorEvent(
error: error,
stackTrace: stackTrace,
hideAction: state.hideAction,
));
super.onError(error, stackTrace);
}
}
19 changes: 19 additions & 0 deletions lib/blocs/hide/hide_event.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -82,3 +83,21 @@ class RefreshTurboBalanceEvent extends HideEvent {
@override
List<Object> 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<Object> get props => [
error,
stackTrace,
];
}
11 changes: 4 additions & 7 deletions lib/blocs/hide/hide_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class UploadingHideState extends HideState {
});

@override
List<Object?> get props => [];
List<Object> get props => [];
}

class PreparingAndSigningHideState extends HideState {
Expand All @@ -33,7 +33,7 @@ class PreparingAndSigningHideState extends HideState {
});

@override
List<Object?> get props => [];
List<Object> get props => [];
}

class ConfirmingHideState extends HideState {
Expand Down Expand Up @@ -127,19 +127,16 @@ class SuccessHideState extends HideState {
});

@override
List<Object?> get props => [];
List<Object> get props => [];
}

class FailureHideState extends HideState {
final String message;

const FailureHideState({
required this.message,
required super.hideAction,
});

@override
List<Object?> get props => [message];
List<Object> get props => [];
}

enum HideAction {
Expand Down
47 changes: 42 additions & 5 deletions lib/components/hide_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -82,7 +119,7 @@ class HideDialog extends StatelessWidget {
action: () {
Navigator.of(context).pop();
},
title: appLocalizationsOf(context).cancel,
title: appLocalizationsOf(context).close,
),
];
} else {
Expand Down

0 comments on commit 2fe2208

Please sign in to comment.