feat: create a dedicated provider for dashbot navigation

This commit is contained in:
Udhay-Adithya
2025-09-27 17:36:02 +05:30
parent ec02eb640f
commit b840ba44de
7 changed files with 141 additions and 99 deletions

View File

@@ -0,0 +1,63 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
import '../routes/dashbot_routes.dart';
import '../utils/dashbot_route_utils.dart';
/// A Notifier that exposes the current Dashbot active route.
///
/// Behavior:
/// - Default state is computed from the currently selected request using
/// [computeDashbotBaseRoute].
/// - Automatically updates when the selected request changes, *unless* the
/// route has been manually set to Chat (Chat acts as a user override).
/// - Public method [goToChat] pins the route to Chat.
/// - Public method [resetToBaseRoute] recalculates from the current request
/// (used after clearing chat, etc.).
class DashbotActiveRouteNotifier extends Notifier<String> {
bool _chatPinned = false;
@override
String build() {
// Watch current request for automatic base route computation.
final req = ref.watch(selectedRequestModelProvider);
ref.keepAlive();
// If chat is pinned we always stay on chat regardless of request changes.
if (_chatPinned) {
return DashbotRoutes.dashbotChat;
}
// Otherwise compute the base route from the current request.
return computeDashbotBaseRoute(req);
}
void goToChat() {
if (state == DashbotRoutes.dashbotChat) return;
_chatPinned = true;
state = DashbotRoutes.dashbotChat;
}
void resetToBaseRoute() {
_chatPinned = false;
final req = ref.read(selectedRequestModelProvider);
final target = computeDashbotBaseRoute(req);
state = target;
}
/// Force set a specific route (used rarely; prefers semantic helpers).
void setRoute(String route) {
if (route == DashbotRoutes.dashbotChat) {
goToChat();
return;
}
_chatPinned = false;
state = route;
}
}
final dashbotActiveRouteProvider =
NotifierProvider<DashbotActiveRouteNotifier, String>(
() => DashbotActiveRouteNotifier(),
name: 'dashbotActiveRouteProvider',
);

View File

@@ -0,0 +1,22 @@
import 'package:apidash/models/models.dart';
import '../routes/dashbot_routes.dart';
/// Computes the base Dashbot route for a given request based on whether a
/// response exists.
/// - Returns [DashbotRoutes.dashbotHome] if the request has a response (either
/// statusCode or responseStatus present).
/// - Otherwise returns [DashbotRoutes.dashbotDefault].
String computeDashbotBaseRoute(RequestModel? req) {
final hasResponse = (req?.httpResponseModel?.statusCode != null) ||
(req?.responseStatus != null);
return hasResponse ? DashbotRoutes.dashbotHome : DashbotRoutes.dashbotDefault;
}
/// Returns true if the route that should be shown for [req] differs from the
/// currently active [currentRoute].
/// This helper is pure and does not perform any side effects.
bool needsDashbotRouteChange(RequestModel? req, String currentRoute) {
final target = computeDashbotBaseRoute(req);
return target != currentRoute;
}