feat: enhance routing and state management in Dashbot components

This commit is contained in:
Udhay-Adithya
2025-09-21 02:14:38 +05:30
parent 671e4218cc
commit f8d54da074
6 changed files with 36 additions and 30 deletions

View File

@@ -9,17 +9,27 @@ import 'package:flutter/material.dart';
Route<dynamic>? generateRoute(RouteSettings settings) { Route<dynamic>? generateRoute(RouteSettings settings) {
switch (settings.name) { switch (settings.name) {
case (DashbotRoutes.dashbotHome): case (DashbotRoutes.dashbotHome):
return MaterialPageRoute(builder: (context) => DashbotHomePage()); return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotHome),
builder: (context) => DashbotHomePage(),
);
case (DashbotRoutes.dashbotDefault): case (DashbotRoutes.dashbotDefault):
return MaterialPageRoute(builder: (context) => DashbotDefaultPage()); return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotDefault),
builder: (context) => DashbotDefaultPage(),
);
case (DashbotRoutes.dashbotChat): case (DashbotRoutes.dashbotChat):
final arg = settings.arguments; final arg = settings.arguments;
ChatMessageType? initialTask; ChatMessageType? initialTask;
if (arg is ChatMessageType) initialTask = arg; if (arg is ChatMessageType) initialTask = arg;
return MaterialPageRoute( return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotChat),
builder: (context) => ChatScreen(initialTask: initialTask), builder: (context) => ChatScreen(initialTask: initialTask),
); );
default: default:
return MaterialPageRoute(builder: (context) => DashbotDefaultPage()); return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotDefault),
builder: (context) => DashbotDefaultPage(),
);
} }
} }

View File

@@ -6,11 +6,7 @@ import '../providers/dashbot_window_notifier.dart';
/// Optionally pass provider overrides (e.g., dashbotRequestContextProvider) /// Optionally pass provider overrides (e.g., dashbotRequestContextProvider)
/// so the host app can feed live context into Dashbot. /// so the host app can feed live context into Dashbot.
void showDashbotWindow( void showDashbotWindow(BuildContext context, WidgetRef ref) {
BuildContext context,
WidgetRef ref, {
List<Override>? overrides,
}) {
final isDashbotActive = ref.read(dashbotWindowNotifierProvider).isActive; final isDashbotActive = ref.read(dashbotWindowNotifierProvider).isActive;
final isDashbotPopped = ref.read(dashbotWindowNotifierProvider).isPopped; final isDashbotPopped = ref.read(dashbotWindowNotifierProvider).isPopped;
final windowNotifier = ref.read(dashbotWindowNotifierProvider.notifier); final windowNotifier = ref.read(dashbotWindowNotifierProvider.notifier);
@@ -21,7 +17,6 @@ void showDashbotWindow(
entry = OverlayEntry( entry = OverlayEntry(
builder: (context) => ProviderScope( builder: (context) => ProviderScope(
overrides: overrides ?? const [],
child: DashbotWindow( child: DashbotWindow(
onClose: () { onClose: () {
entry?.remove(); entry?.remove();

View File

@@ -37,11 +37,14 @@ class DashbotWindow extends ConsumerWidget {
ref.listen( ref.listen(
selectedRequestModelProvider, selectedRequestModelProvider,
(current, next) { (prev, next) {
if (next?.responseStatus != null) { if (prev?.id == next?.id) return;
_dashbotNavigatorKey.currentState?.pushNamed( final initial = _dashbotNavigatorKey.currentState?.widget.initialRoute;
DashbotRoutes.dashbotHome, 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);
} }
}, },
); );

View File

@@ -30,20 +30,23 @@ class _DashbotTabState extends ConsumerState<DashbotTab>
ref.listen( ref.listen(
selectedRequestModelProvider, selectedRequestModelProvider,
(prev, next) { (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); _navKey.currentState?.pushNamed(DashbotRoutes.dashbotHome);
} }
}, },
); );
return WillPopScope( return PopScope(
onWillPop: () async { canPop: true,
onPopInvokedWithResult: (didPop, _) {
if (didPop) return;
final canPop = _navKey.currentState?.canPop() ?? false; final canPop = _navKey.currentState?.canPop() ?? false;
if (canPop) { if (canPop) {
_navKey.currentState?.pop(); _navKey.currentState?.pop();
return false;
} }
return true;
}, },
child: Padding( child: Padding(
padding: kP10, padding: kP10,

View File

@@ -34,14 +34,6 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// ref.listen(
// selectedRequestModelProvider,
// (current, next) {
// if (current?.id != next?.id) {
// Navigator.pop(context);
// }
// },
// );
return Scaffold( return Scaffold(
body: Column( body: Column(
children: [ children: [

View File

@@ -21,7 +21,11 @@ class DashbotHomePage extends ConsumerStatefulWidget {
class _DashbotHomePageState extends ConsumerState<DashbotHomePage> { class _DashbotHomePageState extends ConsumerState<DashbotHomePage> {
@override @override
Widget build(BuildContext context) { 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( // ref.listen(
// selectedRequestModelProvider, // selectedRequestModelProvider,
@@ -120,8 +124,7 @@ class _DashbotHomePageState extends ConsumerState<DashbotHomePage> {
); );
}, },
), ),
if (currentRequest?.httpResponseModel?.statusCode != null && if (hasOkResponse) ...[
currentRequest?.httpResponseModel?.statusCode == 200) ...[
HomeScreenTaskButton( HomeScreenTaskButton(
label: "🛠️ Generate Tool", label: "🛠️ Generate Tool",
onPressed: () { onPressed: () {