Skip to content

Commit

Permalink
Fix crash when handling app link while add torrent dialog is displayed.
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ray committed Dec 15, 2024
1 parent 398f4ea commit d5e9aa4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
3 changes: 1 addition & 2 deletions app/lib/dialogs/add_torrent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class AddTorrentDialog extends StatefulWidget {
State<AddTorrentDialog> createState() => _AddTorrentDialogState();
}

final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

class _AddTorrentDialogState extends State<AddTorrentDialog> {
late TextEditingController _torrentLinkController;
String? _filename;
String? pickedDownloadDir;
String _torrentLink = ''; // Track a state to trigger updates
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
void initState() {
Expand Down
54 changes: 35 additions & 19 deletions app/lib/navigation/app_shell_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AppShellRoute extends StatefulWidget {

class _AppShellRouteState extends State<AppShellRoute> {
late AppLinks _appLinks;
bool triggeredTermsOfUseDialog = false;
bool isTermsOfUseDialogDisplayed = false;

@override
void initState() {
Expand All @@ -41,27 +41,35 @@ class _AppShellRouteState extends State<AppShellRoute> {
}

_openAddTorrentDialog(String? initialMagnetLink, String? initialContentPath) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog(
context: context,
builder: (BuildContext context) {
return AddTorrentDialog(
initialMagnetLink: initialMagnetLink,
initialContentPath: initialContentPath,
);
});
});
if (Navigator.canPop(context)) {
// Pop current dialog, if any
Navigator.pop(context);
}

showDialog(
context: context,
builder: (BuildContext context) {
return AddTorrentDialog(
initialMagnetLink: initialMagnetLink,
initialContentPath: initialContentPath,
);
});

if (isTermsOfUseDialogDisplayed) {
// This will re-trigger the terms of use dialog, if needed.
// This dialog should be displayed above.
setState(() {
isTermsOfUseDialogDisplayed = false;
});
}
}

@override
Widget build(BuildContext context) {
var settingsLoaded = context.select((AppModel app) => app.loaded);
var termsOfUseAccepted =
context.select((AppModel app) => app.termsOfUseAccepted);
_openTermsOfUseDialog(AppModel appModel) {
var termsOfUseAccepted = appModel.termsOfUseAccepted;

if (settingsLoaded && !termsOfUseAccepted && !triggeredTermsOfUseDialog) {
if (!isTermsOfUseDialogDisplayed && !termsOfUseAccepted) {
// Avoid calling the dialog multiple times
triggeredTermsOfUseDialog = true;
isTermsOfUseDialogDisplayed = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog(
context: context,
Expand All @@ -71,7 +79,15 @@ class _AppShellRouteState extends State<AppShellRoute> {
});
});
}
}

return Navigation(child: widget.child);
@override
Widget build(BuildContext context) {
return Consumer<AppModel>(builder: (context, appModel, child) {
if (appModel.loaded) {
_openTermsOfUseDialog(appModel);
}
return Navigation(child: widget.child);
});
}
}

0 comments on commit d5e9aa4

Please sign in to comment.