From beb10057107c1685004b7effa020f9777b6d75bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 13 Jun 2024 15:42:39 +0200 Subject: [PATCH] Refactor routing methods to Navi helper class --- flutter/lib/components/layout/app_bar.dart | 3 ++- flutter/lib/pages/account/account.dart | 3 ++- flutter/lib/pages/account/login.dart | 3 ++- flutter/lib/pages/debug/debug_persistence.dart | 7 ++++--- flutter/lib/pages/debug/debug_persistence_hive.dart | 3 ++- flutter/lib/pages/debug/debug_requests.dart | 5 +++-- flutter/lib/pages/message_list/message_list.dart | 3 ++- flutter/lib/utils/navi.dart | 12 +++++++++++- 8 files changed, 28 insertions(+), 11 deletions(-) diff --git a/flutter/lib/components/layout/app_bar.dart b/flutter/lib/components/layout/app_bar.dart index 93d3e95..3f6e4d2 100644 --- a/flutter/lib/components/layout/app_bar.dart +++ b/flutter/lib/components/layout/app_bar.dart @@ -3,6 +3,7 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; import 'package:simplecloudnotifier/pages/debug/debug_main.dart'; import 'package:simplecloudnotifier/state/app_theme.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; class SCNAppBar extends StatelessWidget implements PreferredSizeWidget { const SCNAppBar({ @@ -43,7 +44,7 @@ class SCNAppBar extends StatelessWidget implements PreferredSizeWidget { icon: const Icon(FontAwesomeIcons.solidSpiderBlackWidow), tooltip: 'Debug', onPressed: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => DebugMainPage())); + Navi.push(context, () => DebugMainPage()); }, )); } else { diff --git a/flutter/lib/pages/account/account.dart b/flutter/lib/pages/account/account.dart index 72bd378..9864ae9 100644 --- a/flutter/lib/pages/account/account.dart +++ b/flutter/lib/pages/account/account.dart @@ -10,6 +10,7 @@ import 'package:simplecloudnotifier/pages/account/login.dart'; import 'package:simplecloudnotifier/state/application_log.dart'; import 'package:simplecloudnotifier/state/globals.dart'; import 'package:simplecloudnotifier/state/app_auth.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; import 'package:simplecloudnotifier/utils/toaster.dart'; import 'package:simplecloudnotifier/utils/ui.dart'; import 'package:uuid/uuid.dart'; @@ -157,7 +158,7 @@ class _AccountRootPageState extends State { text: 'Use existing account', onPressed: () { if (loading) return; - Navigator.push(context, MaterialPageRoute(builder: (context) => AccountLoginPage())); + Navi.push(context, () => AccountLoginPage()); }, tonal: true, big: true, diff --git a/flutter/lib/pages/account/login.dart b/flutter/lib/pages/account/login.dart index 2711ce0..9d9f76d 100644 --- a/flutter/lib/pages/account/login.dart +++ b/flutter/lib/pages/account/login.dart @@ -8,6 +8,7 @@ import 'package:simplecloudnotifier/state/application_log.dart'; import 'package:simplecloudnotifier/state/globals.dart'; import 'package:simplecloudnotifier/state/app_auth.dart'; import 'package:simplecloudnotifier/state/token_source.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; import 'package:simplecloudnotifier/utils/toaster.dart'; import 'package:simplecloudnotifier/utils/ui.dart'; @@ -154,7 +155,7 @@ class _AccountLoginPageState extends State { await acc.save(); Toaster.success("Login", "Successfully logged in"); - Navigator.popUntil(context, (route) => route.isFirst); + Navi.popToRoot(context); } catch (exc, trace) { ApplicationLog.error('Failed to verify token: ' + exc.toString(), trace: trace); Toaster.error("Error", 'Failed to verify token'); diff --git a/flutter/lib/pages/debug/debug_persistence.dart b/flutter/lib/pages/debug/debug_persistence.dart index cd3ebeb..3808ee2 100644 --- a/flutter/lib/pages/debug/debug_persistence.dart +++ b/flutter/lib/pages/debug/debug_persistence.dart @@ -5,6 +5,7 @@ import 'package:simplecloudnotifier/pages/debug/debug_persistence_hive.dart'; import 'package:simplecloudnotifier/pages/debug/debug_persistence_sharedprefs.dart'; import 'package:simplecloudnotifier/state/application_log.dart'; import 'package:simplecloudnotifier/state/request_log.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; class DebugPersistencePage extends StatefulWidget { @override @@ -33,7 +34,7 @@ class _DebugPersistencePageState extends State { padding: const EdgeInsets.all(8.0), child: GestureDetector( onTap: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => DebugSharedPrefPage(sharedPref: prefs!))); + Navi.push(context, () => DebugSharedPrefPage(sharedPref: prefs!)); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, @@ -51,7 +52,7 @@ class _DebugPersistencePageState extends State { padding: const EdgeInsets.all(8.0), child: GestureDetector( onTap: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => DebugHiveBoxPage(boxName: 'scn-requests', box: Hive.box('scn-requests')))); + Navi.push(context, () => DebugHiveBoxPage(boxName: 'scn-requests', box: Hive.box('scn-requests'))); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, @@ -69,7 +70,7 @@ class _DebugPersistencePageState extends State { padding: const EdgeInsets.all(8.0), child: GestureDetector( onTap: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => DebugHiveBoxPage(boxName: 'scn-requests', box: Hive.box('scn-logs')))); + Navi.push(context, () => DebugHiveBoxPage(boxName: 'scn-requests', box: Hive.box('scn-logs'))); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, diff --git a/flutter/lib/pages/debug/debug_persistence_hive.dart b/flutter/lib/pages/debug/debug_persistence_hive.dart index c58a709..12aefd2 100644 --- a/flutter/lib/pages/debug/debug_persistence_hive.dart +++ b/flutter/lib/pages/debug/debug_persistence_hive.dart @@ -3,6 +3,7 @@ import 'package:hive_flutter/hive_flutter.dart'; import 'package:simplecloudnotifier/components/layout/scaffold.dart'; import 'package:simplecloudnotifier/pages/debug/debug_persistence_hiveentry.dart'; import 'package:simplecloudnotifier/state/interfaces.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; class DebugHiveBoxPage extends StatelessWidget { final String boxName; @@ -21,7 +22,7 @@ class DebugHiveBoxPage extends StatelessWidget { itemBuilder: (context, listIndex) { return GestureDetector( onTap: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => DebugHiveEntryPage(value: box.getAt(listIndex)!))); + Navi.push(context, () => DebugHiveEntryPage(value: box.getAt(listIndex)!)); }, child: ListTile( title: Text(box.getAt(listIndex).toString(), style: TextStyle(fontWeight: FontWeight.bold)), diff --git a/flutter/lib/pages/debug/debug_requests.dart b/flutter/lib/pages/debug/debug_requests.dart index 27331e2..c0b2bc5 100644 --- a/flutter/lib/pages/debug/debug_requests.dart +++ b/flutter/lib/pages/debug/debug_requests.dart @@ -3,6 +3,7 @@ import 'package:hive_flutter/hive_flutter.dart'; import 'package:intl/intl.dart'; import 'package:simplecloudnotifier/pages/debug/debug_request_view.dart'; import 'package:simplecloudnotifier/state/request_log.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; class DebugRequestsPage extends StatefulWidget { @override @@ -40,7 +41,7 @@ class _DebugRequestsPageState extends State { return Padding( padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 2.0), child: GestureDetector( - onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => DebugRequestViewPage(request: req))), + onTap: () => Navi.push(context, () => DebugRequestViewPage(request: req)), child: ListTile( tileColor: Theme.of(context).colorScheme.errorContainer, textColor: Theme.of(context).colorScheme.onErrorContainer, @@ -76,7 +77,7 @@ class _DebugRequestsPageState extends State { return Padding( padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 2.0), child: GestureDetector( - onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => DebugRequestViewPage(request: req))), + onTap: () => Navi.push(context, () => DebugRequestViewPage(request: req)), child: ListTile( title: Row( children: [ diff --git a/flutter/lib/pages/message_list/message_list.dart b/flutter/lib/pages/message_list/message_list.dart index 3281e7e..d5b0527 100644 --- a/flutter/lib/pages/message_list/message_list.dart +++ b/flutter/lib/pages/message_list/message_list.dart @@ -8,6 +8,7 @@ import 'package:simplecloudnotifier/pages/message_view/message_view.dart'; import 'package:simplecloudnotifier/state/application_log.dart'; import 'package:simplecloudnotifier/state/app_auth.dart'; import 'package:simplecloudnotifier/pages/message_list/message_list_item.dart'; +import 'package:simplecloudnotifier/utils/navi.dart'; class MessageListPage extends StatefulWidget { const MessageListPage({super.key}); @@ -83,7 +84,7 @@ class _MessageListPageState extends State { message: item, allChannels: _channels ?? {}, onPressed: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => MessageViewPage(message: item))); + Navi.push(context, () => MessageViewPage(message: item)); }, ), ), diff --git a/flutter/lib/utils/navi.dart b/flutter/lib/utils/navi.dart index 6b6d16c..f250af1 100644 --- a/flutter/lib/utils/navi.dart +++ b/flutter/lib/utils/navi.dart @@ -1 +1,11 @@ -class Navi {} +import 'package:flutter/material.dart'; + +class Navi { + static void push(BuildContext context, T Function() builder) { + Navigator.push(context, MaterialPageRoute(builder: (context) => builder())); + } + + static void popToRoot(BuildContext context) { + Navigator.popUntil(context, (route) => route.isFirst); + } +}