mirror of
https://github.com/foss42/apidash.git
synced 2025-12-03 03:17:00 +08:00
feat: implement reactive switching in dashbot home and default page
This commit is contained in:
@@ -35,16 +35,29 @@ class DashbotWindow extends ConsumerWidget {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Listen to changes in response status and navigate accordingly
|
||||||
ref.listen(
|
ref.listen(
|
||||||
selectedRequestModelProvider,
|
selectedRequestModelProvider
|
||||||
(prev, next) {
|
.select((request) => request?.httpResponseModel?.statusCode != null),
|
||||||
if (prev?.id == next?.id) return;
|
(prev, hasResponse) {
|
||||||
final initial = _dashbotNavigatorKey.currentState?.widget.initialRoute;
|
if (prev == hasResponse) return;
|
||||||
final atRoot = _dashbotNavigatorKey.currentState?.canPop() == false;
|
|
||||||
// Only push when navigator started on Default and is still at root
|
final currentRoute =
|
||||||
if (initial == DashbotRoutes.dashbotDefault && atRoot) {
|
_dashbotNavigatorKey.currentState?.widget.initialRoute;
|
||||||
_dashbotNavigatorKey.currentState
|
final canPop = _dashbotNavigatorKey.currentState?.canPop() ?? false;
|
||||||
?.pushNamed(DashbotRoutes.dashbotHome);
|
|
||||||
|
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(
|
Expanded(
|
||||||
child: Navigator(
|
child: Navigator(
|
||||||
key: _dashbotNavigatorKey,
|
key: _dashbotNavigatorKey,
|
||||||
initialRoute: currentRequest?.responseStatus == null
|
initialRoute: (currentRequest
|
||||||
? DashbotRoutes.dashbotDefault
|
?.httpResponseModel?.statusCode !=
|
||||||
: DashbotRoutes.dashbotHome,
|
null)
|
||||||
|
? DashbotRoutes.dashbotHome
|
||||||
|
: DashbotRoutes.dashbotDefault,
|
||||||
onGenerateRoute: generateRoute,
|
onGenerateRoute: generateRoute,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -27,15 +27,27 @@ class _DashbotTabState extends ConsumerState<DashbotTab>
|
|||||||
super.build(context);
|
super.build(context);
|
||||||
final currentRequest = ref.watch(selectedRequestModelProvider);
|
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(
|
ref.listen(
|
||||||
selectedRequestModelProvider,
|
selectedRequestModelProvider.select((request) =>
|
||||||
(prev, next) {
|
request?.httpResponseModel?.statusCode != null ||
|
||||||
if (prev?.id == next?.id) return;
|
request?.responseStatus != null),
|
||||||
final initial = _navKey.currentState?.widget.initialRoute;
|
(prev, hasResponse) {
|
||||||
final atRoot = _navKey.currentState?.canPop() == false;
|
if (prev == hasResponse) return;
|
||||||
if (initial == DashbotRoutes.dashbotDefault && atRoot) {
|
|
||||||
_navKey.currentState?.pushNamed(DashbotRoutes.dashbotHome);
|
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<DashbotTab>
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: Navigator(
|
child: Navigator(
|
||||||
key: _navKey,
|
key: _navKey,
|
||||||
initialRoute: currentRequest?.responseStatus == null
|
initialRoute:
|
||||||
? DashbotRoutes.dashbotDefault
|
(currentRequest?.httpResponseModel?.statusCode != null ||
|
||||||
: DashbotRoutes.dashbotHome,
|
currentRequest?.responseStatus != null)
|
||||||
|
? DashbotRoutes.dashbotHome
|
||||||
|
: DashbotRoutes.dashbotDefault,
|
||||||
onGenerateRoute: generateRoute,
|
onGenerateRoute: generateRoute,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user