From f8d54da074d0499d3e6975694a55a76683d95295 Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Sun, 21 Sep 2025 02:14:38 +0530 Subject: [PATCH] feat: enhance routing and state management in Dashbot components --- lib/dashbot/core/routes/dashbot_router.dart | 16 +++++++++++++--- lib/dashbot/core/utils/show_dashbot.dart | 7 +------ lib/dashbot/dashbot_dashboard.dart | 13 ++++++++----- lib/dashbot/dashbot_tab.dart | 13 ++++++++----- .../chat/view/pages/dashbot_chat_page.dart | 8 -------- .../features/home/view/pages/home_page.dart | 9 ++++++--- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/lib/dashbot/core/routes/dashbot_router.dart b/lib/dashbot/core/routes/dashbot_router.dart index 66c515dd..858b5172 100644 --- a/lib/dashbot/core/routes/dashbot_router.dart +++ b/lib/dashbot/core/routes/dashbot_router.dart @@ -9,17 +9,27 @@ import 'package:flutter/material.dart'; Route? generateRoute(RouteSettings settings) { switch (settings.name) { case (DashbotRoutes.dashbotHome): - return MaterialPageRoute(builder: (context) => DashbotHomePage()); + return MaterialPageRoute( + settings: const RouteSettings(name: DashbotRoutes.dashbotHome), + builder: (context) => DashbotHomePage(), + ); case (DashbotRoutes.dashbotDefault): - return MaterialPageRoute(builder: (context) => DashbotDefaultPage()); + return MaterialPageRoute( + settings: const RouteSettings(name: DashbotRoutes.dashbotDefault), + builder: (context) => DashbotDefaultPage(), + ); case (DashbotRoutes.dashbotChat): final arg = settings.arguments; ChatMessageType? initialTask; if (arg is ChatMessageType) initialTask = arg; return MaterialPageRoute( + settings: const RouteSettings(name: DashbotRoutes.dashbotChat), builder: (context) => ChatScreen(initialTask: initialTask), ); default: - return MaterialPageRoute(builder: (context) => DashbotDefaultPage()); + return MaterialPageRoute( + settings: const RouteSettings(name: DashbotRoutes.dashbotDefault), + builder: (context) => DashbotDefaultPage(), + ); } } diff --git a/lib/dashbot/core/utils/show_dashbot.dart b/lib/dashbot/core/utils/show_dashbot.dart index 3590afe1..9e034727 100644 --- a/lib/dashbot/core/utils/show_dashbot.dart +++ b/lib/dashbot/core/utils/show_dashbot.dart @@ -6,11 +6,7 @@ import '../providers/dashbot_window_notifier.dart'; /// Optionally pass provider overrides (e.g., dashbotRequestContextProvider) /// so the host app can feed live context into Dashbot. -void showDashbotWindow( - BuildContext context, - WidgetRef ref, { - List? overrides, -}) { +void showDashbotWindow(BuildContext context, WidgetRef ref) { final isDashbotActive = ref.read(dashbotWindowNotifierProvider).isActive; final isDashbotPopped = ref.read(dashbotWindowNotifierProvider).isPopped; final windowNotifier = ref.read(dashbotWindowNotifierProvider.notifier); @@ -21,7 +17,6 @@ void showDashbotWindow( entry = OverlayEntry( builder: (context) => ProviderScope( - overrides: overrides ?? const [], child: DashbotWindow( onClose: () { entry?.remove(); diff --git a/lib/dashbot/dashbot_dashboard.dart b/lib/dashbot/dashbot_dashboard.dart index 66c5472f..e8b13460 100644 --- a/lib/dashbot/dashbot_dashboard.dart +++ b/lib/dashbot/dashbot_dashboard.dart @@ -37,11 +37,14 @@ class DashbotWindow extends ConsumerWidget { ref.listen( selectedRequestModelProvider, - (current, next) { - if (next?.responseStatus != null) { - _dashbotNavigatorKey.currentState?.pushNamed( - DashbotRoutes.dashbotHome, - ); + (prev, next) { + if (prev?.id == next?.id) return; + final initial = _dashbotNavigatorKey.currentState?.widget.initialRoute; + final atRoot = _dashbotNavigatorKey.currentState?.canPop() == false; + // Only push when navigator started on Default and is still at root + if (initial == DashbotRoutes.dashbotDefault && atRoot) { + _dashbotNavigatorKey.currentState + ?.pushNamed(DashbotRoutes.dashbotHome); } }, ); diff --git a/lib/dashbot/dashbot_tab.dart b/lib/dashbot/dashbot_tab.dart index e9fe6ab3..f091667c 100644 --- a/lib/dashbot/dashbot_tab.dart +++ b/lib/dashbot/dashbot_tab.dart @@ -30,20 +30,23 @@ class _DashbotTabState extends ConsumerState ref.listen( selectedRequestModelProvider, (prev, next) { - if (next?.responseStatus != null) { + if (prev?.id == next?.id) return; + final initial = _navKey.currentState?.widget.initialRoute; + final atRoot = _navKey.currentState?.canPop() == false; + if (initial == DashbotRoutes.dashbotDefault && atRoot) { _navKey.currentState?.pushNamed(DashbotRoutes.dashbotHome); } }, ); - return WillPopScope( - onWillPop: () async { + return PopScope( + canPop: true, + onPopInvokedWithResult: (didPop, _) { + if (didPop) return; final canPop = _navKey.currentState?.canPop() ?? false; if (canPop) { _navKey.currentState?.pop(); - return false; } - return true; }, child: Padding( padding: kP10, diff --git a/lib/dashbot/features/chat/view/pages/dashbot_chat_page.dart b/lib/dashbot/features/chat/view/pages/dashbot_chat_page.dart index c3fb9edc..b71e3ea2 100644 --- a/lib/dashbot/features/chat/view/pages/dashbot_chat_page.dart +++ b/lib/dashbot/features/chat/view/pages/dashbot_chat_page.dart @@ -34,14 +34,6 @@ class _ChatScreenState extends ConsumerState { @override Widget build(BuildContext context) { - // ref.listen( - // selectedRequestModelProvider, - // (current, next) { - // if (current?.id != next?.id) { - // Navigator.pop(context); - // } - // }, - // ); return Scaffold( body: Column( children: [ diff --git a/lib/dashbot/features/home/view/pages/home_page.dart b/lib/dashbot/features/home/view/pages/home_page.dart index 50fb1640..0efb6384 100644 --- a/lib/dashbot/features/home/view/pages/home_page.dart +++ b/lib/dashbot/features/home/view/pages/home_page.dart @@ -21,7 +21,11 @@ class DashbotHomePage extends ConsumerStatefulWidget { class _DashbotHomePageState extends ConsumerState { @override Widget build(BuildContext context) { - final currentRequest = ref.watch(selectedRequestModelProvider); + final hasOkResponse = ref.watch( + selectedRequestModelProvider.select((req) => + req?.httpResponseModel?.statusCode != null && + req?.httpResponseModel?.statusCode == 200), + ); // ref.listen( // selectedRequestModelProvider, @@ -120,8 +124,7 @@ class _DashbotHomePageState extends ConsumerState { ); }, ), - if (currentRequest?.httpResponseModel?.statusCode != null && - currentRequest?.httpResponseModel?.statusCode == 200) ...[ + if (hasOkResponse) ...[ HomeScreenTaskButton( label: "🛠️ Generate Tool", onPressed: () {