From e3fc474c070852ac39a74152afd953cbdc3f6e1b Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Fri, 26 Sep 2025 00:08:04 +0530 Subject: [PATCH] feat: implement reactive switching in dashbot home and default page --- lib/dashbot/dashbot_dashboard.dart | 39 +++++++++++++++++++++--------- lib/dashbot/dashbot_tab.dart | 36 ++++++++++++++++++--------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/lib/dashbot/dashbot_dashboard.dart b/lib/dashbot/dashbot_dashboard.dart index 276512ee..e4a42228 100644 --- a/lib/dashbot/dashbot_dashboard.dart +++ b/lib/dashbot/dashbot_dashboard.dart @@ -35,16 +35,29 @@ class DashbotWindow extends ConsumerWidget { }, ); + // Listen to changes in response status and navigate accordingly ref.listen( - selectedRequestModelProvider, - (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); + selectedRequestModelProvider + .select((request) => request?.httpResponseModel?.statusCode != null), + (prev, hasResponse) { + if (prev == hasResponse) return; + + final currentRoute = + _dashbotNavigatorKey.currentState?.widget.initialRoute; + final canPop = _dashbotNavigatorKey.currentState?.canPop() ?? false; + + if (hasResponse) { + // Response available - navigate to home if not already there + if (currentRoute == DashbotRoutes.dashbotDefault && !canPop) { + _dashbotNavigatorKey.currentState + ?.pushNamed(DashbotRoutes.dashbotHome); + } + } else { + // No response - navigate back to default if we're in home + if (canPop) { + _dashbotNavigatorKey.currentState + ?.popUntil((route) => route.isFirst); + } } }, ); @@ -161,9 +174,11 @@ class DashbotWindow extends ConsumerWidget { Expanded( child: Navigator( key: _dashbotNavigatorKey, - initialRoute: currentRequest?.responseStatus == null - ? DashbotRoutes.dashbotDefault - : DashbotRoutes.dashbotHome, + initialRoute: (currentRequest + ?.httpResponseModel?.statusCode != + null) + ? DashbotRoutes.dashbotHome + : DashbotRoutes.dashbotDefault, onGenerateRoute: generateRoute, ), ), diff --git a/lib/dashbot/dashbot_tab.dart b/lib/dashbot/dashbot_tab.dart index f5c8346e..d7e8b832 100644 --- a/lib/dashbot/dashbot_tab.dart +++ b/lib/dashbot/dashbot_tab.dart @@ -27,15 +27,27 @@ class _DashbotTabState extends ConsumerState super.build(context); final currentRequest = ref.watch(selectedRequestModelProvider); - // If a response arrives while user is on default, navigate to home. + // Listen to changes in response status and navigate accordingly ref.listen( - selectedRequestModelProvider, - (prev, next) { - 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); + selectedRequestModelProvider.select((request) => + request?.httpResponseModel?.statusCode != null || + request?.responseStatus != null), + (prev, hasResponse) { + if (prev == hasResponse) return; + + final currentRoute = _navKey.currentState?.widget.initialRoute; + final canPop = _navKey.currentState?.canPop() ?? false; + + if (hasResponse) { + // Response available - navigate to home if not already there + if (currentRoute == DashbotRoutes.dashbotDefault && !canPop) { + _navKey.currentState?.pushNamed(DashbotRoutes.dashbotHome); + } + } else { + // No response - navigate back to default if we're in home + if (canPop) { + _navKey.currentState?.popUntil((route) => route.isFirst); + } } }, ); @@ -107,9 +119,11 @@ class _DashbotTabState extends ConsumerState Expanded( child: Navigator( key: _navKey, - initialRoute: currentRequest?.responseStatus == null - ? DashbotRoutes.dashbotDefault - : DashbotRoutes.dashbotHome, + initialRoute: + (currentRequest?.httpResponseModel?.statusCode != null || + currentRequest?.responseStatus != null) + ? DashbotRoutes.dashbotHome + : DashbotRoutes.dashbotDefault, onGenerateRoute: generateRoute, ), ),