Skip to content

Commit

Permalink
Merge pull request #192 from brandon-mork/main
Browse files Browse the repository at this point in the history
Issue #191 Add "retry" action text to "lost connection" toast
  • Loading branch information
Tarmslitaren authored Oct 25, 2023
2 parents 4982647 + 3145c85 commit 5f55e65
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 28 deletions.
19 changes: 11 additions & 8 deletions frosthaven_assistant/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# frosthaven_assistant

A new Flutter project.
README for developers contributing to frosthaven_assistant.

## Getting Started

This project is a starting point for a Flutter application.
For first time flutter developers, follow the [Flutter Installation Steps](https://docs.flutter.dev/get-started/install).

A few resources to get you started if this is your first Flutter project:
To see if there are any dependencies you need to install to complete setup, run `flutter doctor` from the terminal.

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
Run the app with `flutter run`.

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
## Running Unit Tests

frosthaven_assistant's unit tests rely on [mockito](https://github.com/dart-lang/mockito).

Before running unit tests, run `dart run build_runner build` to generate mockito's `.mocks.dart` files that the unit tests rely on. If encountering errors upon executing build_runner, try running `dart pub upgrade` and rerunning.

Once `.mocks.dart` files are generated, run unit tests with `flutter test`.
64 changes: 45 additions & 19 deletions frosthaven_assistant/lib/Resource/ui_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void openDialogAtPosition(
]));
}

//used to get transpaarant background when dragging in reorderable widgets
//used to get transparent background when dragging in re-orderable widgets
Widget defaultBuildDraggableFeedback(
BuildContext context, BoxConstraints constraints, Widget child) {
return Transform(
Expand Down Expand Up @@ -201,18 +201,20 @@ bool hasGHVersion(String name) {
return true;
}

const TextStyle toastTextStyle = TextStyle(fontFamily: "markazi", fontSize: 28);
createToastContent(BuildContext context, String text) {
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: Text(text, style: toastTextStyle),
);
}

showToast(BuildContext context, String text) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: Text(
text,
style: const TextStyle(fontFamily: "markazi", fontSize: 28),
),
),
backgroundColor: Colors.teal,
content: createToastContent(context, text),
));
}

Expand All @@ -221,16 +223,40 @@ showToastSticky(BuildContext context, String text) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(
duration: const Duration(days: 1),
content: GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: Text(
text,
style: const TextStyle(fontFamily: "markazi", fontSize: 28),
),
),
backgroundColor: Colors.teal,
content: createToastContent(context, text),
))
.closed
.then((value) {
if (getIt<GameState>().toastMessage.value == text) {
GameMethods.setToastMessage("");
}
});
}

showErrorToastStickyWithRetry(
BuildContext context, String text, Function() retry) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(
duration: const Duration(days: 1),
backgroundColor: Colors.redAccent,
content: Row(
children: [
Expanded(child: createToastContent(context, text)),
TextButton(
onPressed: () {
retry();
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: const Text("RETRY",
style: TextStyle(
fontFamily: "markazi",
fontSize: 28,
color: Colors.white,
fontWeight: FontWeight.bold))),
],
),
))
.closed
.then((value) {
Expand Down
15 changes: 14 additions & 1 deletion frosthaven_assistant/lib/services/network/network_ui.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:frosthaven_assistant/Resource/settings.dart';
import 'package:frosthaven_assistant/services/network/client.dart';

import '../../Resource/ui_utils.dart';
import '../service_locator.dart';
Expand Down Expand Up @@ -30,7 +32,18 @@ class NetworkUIState extends State<NetworkUI> {
if (message.toLowerCase().contains("error") ||
message.toLowerCase().contains("disconnected") ||
message.toLowerCase().contains("lost")) {
showToastSticky(context, getIt<Network>().networkMessage.value);
showErrorToastStickyWithRetry(
context, getIt<Network>().networkMessage.value, () {
Settings settings = getIt<Settings>();
if (settings.client.value != ClientState.connected &&
settings.lastKnownConnection != "") {
settings.client.value = ClientState.connecting;
getIt<Client>()
.connect(settings.lastKnownConnection)
.then((value) => null);
settings.saveToDisk();
}
});
} else {
showToast(context, getIt<Network>().networkMessage.value);
}
Expand Down

0 comments on commit 5f55e65

Please sign in to comment.