Skip to content

Commit

Permalink
chore: add exception handling in reset settings & remove snackbar
Browse files Browse the repository at this point in the history
  • Loading branch information
sashatalalasha committed Dec 20, 2023
1 parent 0aa3353 commit 1d7965f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
34 changes: 23 additions & 11 deletions flutter-ory-network/lib/blocs/settings/settings_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,29 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
}

_onResetSettings(ResetSettings event, Emitter<SettingsState> emit) async {
if (state.settingsFlow != null) {
emit(state.copyWith(isLoading: true));
final settings =
await repository.getSettingsFlow(flowId: state.settingsFlow!.id);
List<Condition> updatedConditions = List.from(state.conditions);
updatedConditions
.removeWhere((element) => element is SessionRefreshRequested);
emit(state.copyWith(
settingsFlow: settings,
conditions: updatedConditions,
isLoading: false));
try {
if (state.settingsFlow != null) {
emit(state.copyWith(isLoading: true));
final settings =
await repository.getSettingsFlow(flowId: state.settingsFlow!.id);
List<Condition> updatedConditions = List.from(state.conditions);
updatedConditions
.removeWhere((element) => element is SessionRefreshRequested);
emit(state.copyWith(
settingsFlow: settings,
conditions: updatedConditions,
isLoading: false));
}
} on UnauthorizedException catch (_) {
// change auth status as the user is not authenticated
authBloc.add(ChangeAuthStatus(status: AuthStatus.unauthenticated));
} on FlowExpiredException catch (e) {
// get new settings flow
add(GetSettingsFlow(flowId: e.flowId));
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}

Expand Down
15 changes: 12 additions & 3 deletions flutter-ory-network/lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ class SettingsForm extends StatelessWidget {
listenWhen: (SettingsState previous, SettingsState current) =>
// listen to changes only when current
// state is not loading (e.g. updating settings),
// previous and current messages differ
// previous state was loading,
// there is a new message,
// and session refresh is not required
!current.isLoading &&
current.settingsFlow?.ui.messages !=
previous.settingsFlow?.ui.messages &&
previous.isLoading &&
current.settingsFlow?.ui.messages != null &&
!isSessionRefreshRequired(current.conditions),
listener: (BuildContext context, SettingsState state) {
if (state.settingsFlow!.ui.messages != null) {
Expand All @@ -153,6 +154,14 @@ class SettingsForm extends StatelessWidget {
content: Text(state.message!),
));
}
}),
// remove snackbars when loading
BlocListener(
bloc: settingsBloc,
listener: (BuildContext context, SettingsState state) {
if (state.isLoading) {
ScaffoldMessenger.of(context).clearSnackBars();
}
})
],
child: BlocBuilder<SettingsBloc, SettingsState>(
Expand Down

0 comments on commit 1d7965f

Please sign in to comment.