Skip to content

Commit

Permalink
feat: Use exception dialog when websocket connection closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Mar 19, 2024
1 parent bf182c2 commit 1a727f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
41 changes: 9 additions & 32 deletions wrestling_scoreboard_client/lib/view/screens/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wrestling_scoreboard_client/provider/network_provider.dart';
import 'package:wrestling_scoreboard_client/services/network/remote/web_socket.dart';
import 'package:wrestling_scoreboard_client/view/screens/home/clubs_view.dart';
import 'package:wrestling_scoreboard_client/view/screens/home/competitions_view.dart';
import 'package:wrestling_scoreboard_client/view/screens/home/leagues_view.dart';
import 'package:wrestling_scoreboard_client/services/network/remote/web_socket.dart';
import 'package:wrestling_scoreboard_client/view/widgets/dialogs.dart';

class Home extends ConsumerStatefulWidget {
const Home({super.key});
Expand All @@ -19,44 +20,20 @@ class HomeState extends ConsumerState<Home> {
void initState() {
super.initState();
ref.read(dataManagerNotifierProvider).then((dataManager) {
void showDisconnectedAlert([Object? exception]) {
final localizations = AppLocalizations.of(context)!;
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
SelectableText(localizations.noWebSocketConnection),
// TODO: add show more button
// if (exception != null)
// SelectableText(exception.toString(), style: TextStyle(color: Theme.of(context).colorScheme.error)),
],
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(localizations.cancel),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
dataManager.webSocketManager.onWebSocketConnection.sink.add(WebSocketConnectionState.connecting);
},
child: Text(localizations.retry),
),
],
),
);
void onRetry() {
Navigator.of(context).pop();
dataManager.webSocketManager.onWebSocketConnection.sink.add(WebSocketConnectionState.connecting);
}

WidgetsBinding.instance.addPostFrameCallback((_) {
dataManager.webSocketManager.onWebSocketConnection.stream.distinct().listen((connectionState) {
if (mounted && connectionState == WebSocketConnectionState.disconnected) {
showDisconnectedAlert();
final localizations = AppLocalizations.of(context)!;
showExceptionDialog(
context: context, exception: localizations.noWebSocketConnection, stackTrace: null, onRetry: onRetry);
}
}, onError: (e, [trace]) {
showDisconnectedAlert(e);
showExceptionDialog(exception: e, context: context, stackTrace: trace, onRetry: onRetry);
});
});
});
Expand Down
8 changes: 5 additions & 3 deletions wrestling_scoreboard_client/lib/view/widgets/dialogs.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:wrestling_scoreboard_client/services/network/remote/rest.dart';
import 'package:wrestling_scoreboard_client/view/utils.dart';
import 'package:wrestling_scoreboard_client/view/widgets/duration_picker.dart';
import 'package:wrestling_scoreboard_client/view/widgets/exception.dart';
Expand Down Expand Up @@ -82,10 +81,13 @@ Future<T?> showOkCanelDialog<T>({
}

Future<void> showExceptionDialog(
{required BuildContext context, required Object exception, required StackTrace? stackTrace}) async {
{required BuildContext context,
required Object exception,
required StackTrace? stackTrace,
Function()? onRetry}) async {
await showOkDialog(
context: context,
child: ExceptionInfo(exception, stackTrace: stackTrace),
child: ExceptionInfo(exception, stackTrace: stackTrace, onRetry: onRetry),
);
}

Expand Down

0 comments on commit 1a727f2

Please sign in to comment.