import 'package:flutter/material.dart'; import '../../data/models/route_model.dart'; import '../../data/models/stop_model.dart'; import '../../../../services/api/route_api_service.dart'; class RouteProvider extends ChangeNotifier { final RouteApiService _apiService = RouteApiService(); List _routes = []; RouteModel? _currentRoute; bool _isLoading = false; String? _error; List get routes => _routes; RouteModel? get currentRoute => _currentRoute; bool get isLoading => _isLoading; String? get error => _error; Future loadRoutes(String driverId) async { _isLoading = true; _error = null; notifyListeners(); try { _routes = await _apiService.getDriverRoutes(driverId); _isLoading = false; notifyListeners(); } catch (e) { _error = e.toString(); _isLoading = false; notifyListeners(); } } Future loadRoute(String routeId) async { _isLoading = true; _error = null; notifyListeners(); try { _currentRoute = await _apiService.getRouteById(routeId); _isLoading = false; notifyListeners(); } catch (e) { _error = e.toString(); _isLoading = false; notifyListeners(); } } void setCurrentRoute(RouteModel route) { _currentRoute = route; notifyListeners(); } Future startRoute(String routeId) async { final success = await _apiService.updateRouteStatus( routeId, RouteStatus.inProgress, ); if (success && _currentRoute != null) { _currentRoute = _currentRoute!.copyWith( status: RouteStatus.inProgress, startTime: DateTime.now(), ); notifyListeners(); } return success; } Future completeRoute(String routeId) async { final success = await _apiService.updateRouteStatus( routeId, RouteStatus.completed, ); if (success && _currentRoute != null) { _currentRoute = _currentRoute!.copyWith( status: RouteStatus.completed, endTime: DateTime.now(), ); notifyListeners(); } return success; } Future updateStopStatus( String routeId, String stopId, StopStatus status, { String? signature, String? photo, String? notes, }) async { final success = await _apiService.updateStopStatus( routeId, stopId, status, signature: signature, photo: photo, notes: notes, ); if (success && _currentRoute != null) { final updatedStops = _currentRoute!.stops.map((stop) { if (stop.id == stopId) { return stop.copyWith( status: status, completedTime: status == StopStatus.completed ? DateTime.now() : null, ); } return stop; }).toList(); _currentRoute = _currentRoute!.copyWith(stops: updatedStops); notifyListeners(); } return success; } void clearError() { _error = null; notifyListeners(); } }