185 lines
5.6 KiB
Dart
185 lines
5.6 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
||
|
|
import '../models/service_model.dart';
|
||
|
|
import '../services/wortis_api_service.dart';
|
||
|
|
|
||
|
|
class ServicesController extends ChangeNotifier {
|
||
|
|
final WortisApiService _apiService = WortisApiService();
|
||
|
|
|
||
|
|
List<WortisService> _allServices = [];
|
||
|
|
Map<String, List<WortisService>> _servicesBySector = {};
|
||
|
|
bool _isLoading = false;
|
||
|
|
String? _error;
|
||
|
|
String _searchQuery = '';
|
||
|
|
String _selectedSector = 'Tous';
|
||
|
|
|
||
|
|
// Ordre de priorité des secteurs (index 0 = plus haute priorité)
|
||
|
|
static const List<String> _sectorPriority = [
|
||
|
|
'Service Public',
|
||
|
|
// Ajoutez d'autres secteurs selon vos besoins
|
||
|
|
];
|
||
|
|
|
||
|
|
// Getters
|
||
|
|
List<WortisService> get allServices => _allServices;
|
||
|
|
Map<String, List<WortisService>> get servicesBySector => _servicesBySector;
|
||
|
|
bool get isLoading => _isLoading;
|
||
|
|
String? get error => _error;
|
||
|
|
String get searchQuery => _searchQuery;
|
||
|
|
String get selectedSector => _selectedSector;
|
||
|
|
|
||
|
|
List<String> get sectors => ['Tous', ..._servicesBySector.keys];
|
||
|
|
|
||
|
|
// Méthode pour obtenir la priorité d'un secteur
|
||
|
|
int _getSectorPriority(String sector) {
|
||
|
|
final normalizedSector = sector.toLowerCase();
|
||
|
|
for (int i = 0; i < _sectorPriority.length; i++) {
|
||
|
|
if (_sectorPriority[i].toLowerCase() == normalizedSector) {
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return _sectorPriority
|
||
|
|
.length; // Secteurs non listés ont la priorité la plus basse
|
||
|
|
}
|
||
|
|
|
||
|
|
// Méthode pour trier les services par secteur prioritaire et rang
|
||
|
|
List<WortisService> _sortServicesByPriority(List<WortisService> services) {
|
||
|
|
final List<WortisService> sortedServices = List.from(services);
|
||
|
|
|
||
|
|
sortedServices.sort((a, b) {
|
||
|
|
// D'abord, comparer par priorité de secteur
|
||
|
|
final priorityA = _getSectorPriority(a.secteurActivite);
|
||
|
|
final priorityB = _getSectorPriority(b.secteurActivite);
|
||
|
|
|
||
|
|
if (priorityA != priorityB) {
|
||
|
|
return priorityA.compareTo(priorityB);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Si même secteur, trier par rang
|
||
|
|
if (a.rang != null && b.rang != null) {
|
||
|
|
return a.rang!.compareTo(b.rang!);
|
||
|
|
}
|
||
|
|
// Si seulement le premier a un rang, il passe en premier
|
||
|
|
else if (a.rang != null && b.rang == null) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
// Si seulement le deuxième a un rang, il passe en premier
|
||
|
|
else if (a.rang == null && b.rang != null) {
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
// Si aucun n'a de rang, trier par nom alphabétiquement
|
||
|
|
else {
|
||
|
|
return a.name.compareTo(b.name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
return sortedServices;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Services filtrés selon la recherche et le secteur avec tri par priorité
|
||
|
|
List<WortisService> get filteredServices {
|
||
|
|
List<WortisService> services;
|
||
|
|
|
||
|
|
if (_selectedSector == 'Tous') {
|
||
|
|
services = _allServices;
|
||
|
|
} else {
|
||
|
|
services = _servicesBySector[_selectedSector] ?? [];
|
||
|
|
}
|
||
|
|
|
||
|
|
if (_searchQuery.isNotEmpty) {
|
||
|
|
services =
|
||
|
|
services.where((service) {
|
||
|
|
final name = service.name.toLowerCase();
|
||
|
|
final description = service.description.toLowerCase();
|
||
|
|
final sector = service.secteurActivite.toLowerCase();
|
||
|
|
final query = _searchQuery.toLowerCase();
|
||
|
|
|
||
|
|
return name.contains(query) ||
|
||
|
|
description.contains(query) ||
|
||
|
|
sector.contains(query);
|
||
|
|
}).toList();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Appliquer le tri par priorité de secteur et rang
|
||
|
|
return _sortServicesByPriority(services);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Charger les services depuis l'API
|
||
|
|
Future<void> loadServices() async {
|
||
|
|
_isLoading = true;
|
||
|
|
_error = null;
|
||
|
|
notifyListeners();
|
||
|
|
|
||
|
|
try {
|
||
|
|
_allServices = await _apiService.getServices();
|
||
|
|
|
||
|
|
// Trier tous les services par priorité dès le chargement
|
||
|
|
_allServices = _sortServicesByPriority(_allServices);
|
||
|
|
|
||
|
|
_servicesBySector = _apiService.groupServicesBySector(_allServices);
|
||
|
|
|
||
|
|
// Trier les services dans chaque secteur
|
||
|
|
_servicesBySector.forEach((key, value) {
|
||
|
|
_servicesBySector[key] = _sortServicesByPriority(value);
|
||
|
|
});
|
||
|
|
|
||
|
|
_error = null;
|
||
|
|
} catch (e) {
|
||
|
|
_error = e.toString();
|
||
|
|
_allServices = [];
|
||
|
|
_servicesBySector = {};
|
||
|
|
} finally {
|
||
|
|
_isLoading = false;
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Rafraîchir les services
|
||
|
|
Future<void> refreshServices() async {
|
||
|
|
await loadServices();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Mettre à jour la recherche
|
||
|
|
void updateSearchQuery(String query) {
|
||
|
|
_searchQuery = query;
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Changer le secteur sélectionné
|
||
|
|
void selectSector(String sector) {
|
||
|
|
_selectedSector = sector;
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Vider la recherche
|
||
|
|
void clearSearch() {
|
||
|
|
_searchQuery = '';
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Obtenir un service par ID
|
||
|
|
WortisService? getServiceById(String id) {
|
||
|
|
try {
|
||
|
|
return _allServices.firstWhere((service) => service.id == id);
|
||
|
|
} catch (e) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Obtenir les services actifs uniquement avec tri par priorité
|
||
|
|
List<WortisService> get activeServices {
|
||
|
|
final active = _allServices.where((service) => service.status).toList();
|
||
|
|
return _sortServicesByPriority(active);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Obtenir le nombre de services par secteur
|
||
|
|
Map<String, int> get servicesCountBySector {
|
||
|
|
Map<String, int> count = {};
|
||
|
|
for (var entry in _servicesBySector.entries) {
|
||
|
|
count[entry.key] = entry.value.length;
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Méthode pour modifier l'ordre de priorité des secteurs si nécessaire
|
||
|
|
static List<String> get sectorPriority => List.from(_sectorPriority);
|
||
|
|
}
|