Skip to content

Commit

Permalink
feat: Handle error message on displays
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Feb 25, 2024
1 parent 5821c96 commit 8cd154e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ class RestException implements Exception {
String message;

RestException(this.message);

@override
String toString() {
return 'RestException: $message';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:wrestling_scoreboard_client/view/screens/display/common.dart';
import 'package:wrestling_scoreboard_client/view/screens/overview/team_match/team_match_bout_overview.dart';
import 'package:wrestling_scoreboard_client/view/screens/overview/team_match/team_match_overview.dart';
import 'package:wrestling_scoreboard_client/view/widgets/consumer.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dialogs.dart';
import 'package:wrestling_scoreboard_client/view/widgets/loading_builder.dart';
import 'package:wrestling_scoreboard_client/view/widgets/scaled_text.dart';
import 'package:wrestling_scoreboard_client/view/widgets/themed.dart';
Expand Down Expand Up @@ -229,16 +230,29 @@ class BoutState extends ConsumerState<BoutScreen> {
}

void handleAction(BoutScreenActionIntent intent) async {
intent.handle(
(await ref.read(dataManagerNotifierProvider)),
stopwatch,
widget.bouts,
getActions,
widget.boutIndex,
doAction,
context: context,
navigateToBoutByIndex: saveAndNavigateToBoutByIndex,
);
final tmpContext = context;
final dataManager = await ref.read(dataManagerNotifierProvider);
if (tmpContext.mounted) {
try {
await intent.handle(
dataManager,
stopwatch,
widget.bouts,
getActions,
widget.boutIndex,
doAction,
context: tmpContext,
navigateToBoutByIndex: saveAndNavigateToBoutByIndex,
);
} on Exception catch (e) {
if (context.mounted) {
await showExceptionDialog(
context: tmpContext,
exception: e,
);
}
}
}
}

Future<List<BoutAction>> getActions() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:go_router/go_router.dart';
import 'package:wrestling_scoreboard_client/provider/network_provider.dart';
import 'package:wrestling_scoreboard_client/services/audio/audio.dart';
import 'package:wrestling_scoreboard_client/services/network/data_manager.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dialogs.dart';
import 'package:wrestling_scoreboard_common/common.dart';

enum BoutScreenActions {
Expand Down Expand Up @@ -293,9 +294,17 @@ class BoutActionHandler extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
Future<void> handleIntent(BoutScreenActionIntent intent, {BuildContext? context}) async {
await intent.handle(
await ref.read(dataManagerNotifierProvider), stopwatch, bouts, getActions, boutIndex, doAction,
context: context, navigateToBoutByIndex: navigateToBoutByIndex);
final dataManager = await ref.read(dataManagerNotifierProvider);
if (context != null && context.mounted) {
try {
await intent.handle(dataManager, stopwatch, bouts, getActions, boutIndex, doAction,
context: context, navigateToBoutByIndex: navigateToBoutByIndex);
} on Exception catch (e) {
if (context.mounted) {
await showExceptionDialog(context: context, exception: e);
}
}
}
}

const redOneIntent = BoutScreenActionIntent.redOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:wrestling_scoreboard_client/provider/network_provider.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dropdown.dart';
import 'package:wrestling_scoreboard_client/view/widgets/edit.dart';
import 'package:wrestling_scoreboard_client/view/widgets/font.dart';
import 'package:wrestling_scoreboard_client/view/widgets/ok_dialog.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dialogs.dart';
import 'package:wrestling_scoreboard_common/common.dart';

// TODO: dynamically add or remove participants without weight class
Expand Down Expand Up @@ -88,7 +88,7 @@ class LineupEditState extends ConsumerState<LineupEdit> {
onSubmit: () async {
final hasConfirmed = await showDialog(
context: context,
builder: (context) => OkDialog(
builder: (context) => OkCancelDialog(
getResult: () => true,
child: Text(localizations.warningBoutGenerate),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wrestling_scoreboard_client/provider/local_preferences.dart';
import 'package:wrestling_scoreboard_client/provider/local_preferences_provider.dart';
import 'package:wrestling_scoreboard_client/view/widgets/loading_builder.dart';
import 'package:wrestling_scoreboard_client/view/widgets/ok_dialog.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dialogs.dart';
import 'package:wrestling_scoreboard_client/view/widgets/responsive_container.dart';
import 'package:wrestling_scoreboard_client/utils/asset.dart';
import 'package:wrestling_scoreboard_client/utils/environment.dart';
Expand Down Expand Up @@ -280,7 +280,7 @@ class TextInputDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
String? result;
return OkDialog(
return OkCancelDialog(
child: TextFormField(
initialValue: initialValue,
onChanged: (value) => result = value,
Expand Down Expand Up @@ -316,7 +316,7 @@ class _RadioDialogState<T> extends State<RadioDialog<T>> {

@override
Widget build(BuildContext context) {
return OkDialog<T?>(
return OkCancelDialog<T?>(
child: ListView.builder(
key: Key(result.toString()),
shrinkWrap: true,
Expand Down
72 changes: 72 additions & 0 deletions wrestling_scoreboard_client/lib/view/widgets/dialogs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:wrestling_scoreboard_client/services/network/remote/rest.dart';

class OkDialog extends StatelessWidget {
final Widget child;

const OkDialog({required this.child, super.key});

@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return AlertDialog(
content: SizedBox(
width: 300,
child: child,
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(localizations.ok),
),
],
);
}
}

class OkCancelDialog<T extends Object?> extends StatelessWidget {
final Widget child;
final T Function() getResult;

const OkCancelDialog({required this.child, required this.getResult, super.key});

@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return AlertDialog(
content: SizedBox(
width: 300,
child: child,
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(localizations.cancel),
),
TextButton(
onPressed: () => Navigator.pop(context, getResult()),
child: Text(localizations.ok),
),
],
);
}
}

Future<void> showExceptionDialog({required BuildContext context, required Exception exception}) async {
final localizations = AppLocalizations.of(context)!;
await showDialog(
context: context,
builder: (context) => OkDialog(
child: Column(
children: [
if (exception is RestException) SelectableText(localizations.invalidParameterException),
SelectableText(
exception.toString(),
style: TextStyle(color: Theme.of(context).disabledColor, fontSize: 10),
),
],
),
),
);
}
25 changes: 2 additions & 23 deletions wrestling_scoreboard_client/lib/view/widgets/edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:developer' as developer;

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dialogs.dart';
import 'package:wrestling_scoreboard_client/view/widgets/responsive_container.dart';
import 'package:wrestling_scoreboard_client/services/network/remote/rest.dart';

Expand Down Expand Up @@ -103,34 +104,12 @@ class EditAction extends StatelessWidget {
}

void _handleSubmit(BuildContext context) async {
final localizations = AppLocalizations.of(context)!;
try {
await onSubmit();
} on RestException catch (e) {
developer.log(e.message);
if (context.mounted) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Column(
children: [
Text(localizations.invalidParameterException),
Text(
e.message,
style: TextStyle(color: Theme.of(context).disabledColor, fontSize: 10),
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(localizations.ok),
),
],
),
);
await showExceptionDialog(context: context, exception: e);
}
}
}
Expand Down
32 changes: 0 additions & 32 deletions wrestling_scoreboard_client/lib/view/widgets/ok_dialog.dart

This file was deleted.

0 comments on commit 8cd154e

Please sign in to comment.