ionic-planb-logistic-app-fl.../lib/features/routes/presentation/providers/route_provider.dart

131 lines
3.0 KiB
Dart

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<RouteModel> _routes = [];
RouteModel? _currentRoute;
bool _isLoading = false;
String? _error;
List<RouteModel> get routes => _routes;
RouteModel? get currentRoute => _currentRoute;
bool get isLoading => _isLoading;
String? get error => _error;
Future<void> 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<void> 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<bool> 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<bool> 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<bool> 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();
}
}