Livraison d ela gestion des opérations v0.4.0
This commit is contained in:
@@ -307,6 +307,69 @@ class ApiService {
|
||||
}
|
||||
}
|
||||
|
||||
// Export Excel d'une opération
|
||||
Future<void> downloadOperationExcel(int operationId, String fileName) async {
|
||||
try {
|
||||
debugPrint('📊 Téléchargement Excel pour opération $operationId');
|
||||
|
||||
final response = await _dio.get(
|
||||
'/operations/$operationId/export/excel',
|
||||
options: Options(
|
||||
responseType: ResponseType.bytes, // Important pour les fichiers binaires
|
||||
headers: {
|
||||
'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
debugPrint('✅ Fichier Excel reçu (${response.data.length} bytes)');
|
||||
|
||||
if (kIsWeb) {
|
||||
// Pour le web : déclencher le téléchargement via le navigateur
|
||||
_downloadFileWeb(response.data, fileName);
|
||||
} else {
|
||||
// Pour mobile : sauvegarder dans le dossier de téléchargements
|
||||
await _downloadFileMobile(response.data, fileName);
|
||||
}
|
||||
|
||||
debugPrint('✅ Export Excel terminé: $fileName');
|
||||
} else {
|
||||
throw ApiException('Erreur lors du téléchargement: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw ApiException.fromDioException(e);
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException('Erreur inattendue lors de l\'export Excel', originalError: e);
|
||||
}
|
||||
}
|
||||
|
||||
// Téléchargement pour le web
|
||||
void _downloadFileWeb(List<int> bytes, String fileName) {
|
||||
final blob = html.Blob([bytes]);
|
||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||
|
||||
final anchor = html.AnchorElement(href: url)
|
||||
..setAttribute('download', fileName)
|
||||
..click();
|
||||
|
||||
html.Url.revokeObjectUrl(url);
|
||||
debugPrint('🌐 Téléchargement web déclenché: $fileName');
|
||||
}
|
||||
|
||||
// Téléchargement pour mobile
|
||||
Future<void> _downloadFileMobile(List<int> bytes, String fileName) async {
|
||||
try {
|
||||
// Pour mobile, on pourrait utiliser path_provider pour obtenir le dossier de téléchargements
|
||||
// et file_picker ou similar pour sauvegarder le fichier
|
||||
// Pour l'instant, on lance juste une exception informative
|
||||
throw const ApiException('Téléchargement mobile non implémenté. Utilisez la version web.');
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Méthode de nettoyage pour les tests
|
||||
static void reset() {
|
||||
_instance = null;
|
||||
|
||||
Reference in New Issue
Block a user