106 lines
4.2 KiB
Dart
106 lines
4.2 KiB
Dart
// lib/services/marine_api_service.dart
|
|
|
|
import 'dart:convert'; // Added: Necessary for jsonEncode
|
|
import 'package:flutter/foundation.dart'; // Added: Necessary for debugPrint
|
|
import 'package:intl/intl.dart'; // Added: Necessary for DateFormat
|
|
|
|
import 'package:environment_monitoring_app/services/base_api_service.dart';
|
|
import 'package:environment_monitoring_app/services/telegram_service.dart';
|
|
import 'package:environment_monitoring_app/services/server_config_service.dart';
|
|
// Added: Necessary for ApiService.imageBaseUrl, assuming it's defined there.
|
|
// If not, you may need to adjust this.
|
|
import 'package:environment_monitoring_app/services/api_service.dart';
|
|
|
|
class MarineApiService {
|
|
final BaseApiService _baseService;
|
|
final TelegramService _telegramService;
|
|
final ServerConfigService _serverConfigService;
|
|
|
|
MarineApiService(this._baseService, this._telegramService, this._serverConfigService);
|
|
|
|
Future<Map<String, dynamic>> getTarballStations() async {
|
|
final baseUrl = await _serverConfigService.getActiveApiUrl();
|
|
return _baseService.get(baseUrl, 'marine/tarball/stations');
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getManualStations() async {
|
|
final baseUrl = await _serverConfigService.getActiveApiUrl();
|
|
return _baseService.get(baseUrl, 'marine/manual/stations');
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getTarballClassifications() async {
|
|
final baseUrl = await _serverConfigService.getActiveApiUrl();
|
|
return _baseService.get(baseUrl, 'marine/tarball/classifications');
|
|
}
|
|
|
|
// --- ADDED: Method to fetch images (Fixes the issue) ---
|
|
/// Fetches image records for either In-Situ or Tarball sampling.
|
|
Future<Map<String, dynamic>> getManualSamplingImages({
|
|
required int stationId,
|
|
required DateTime samplingDate,
|
|
required String samplingType, // This parameter is now used
|
|
}) async {
|
|
final baseUrl = await _serverConfigService.getActiveApiUrl();
|
|
final String dateStr = DateFormat('yyyy-MM-dd').format(samplingDate);
|
|
|
|
String endpoint;
|
|
// Determine the correct endpoint based on the sampling type
|
|
switch (samplingType) {
|
|
case 'In-Situ Sampling':
|
|
endpoint = 'marine/manual/records-by-station?station_id=$stationId&date=$dateStr';
|
|
break;
|
|
case 'Tarball Sampling':
|
|
// **IMPORTANT**: Please verify this is the correct endpoint for tarball records
|
|
endpoint = 'marine/tarball/records-by-station?station_id=$stationId&date=$dateStr';
|
|
break;
|
|
case 'All Manual Sampling':
|
|
default:
|
|
// 'All' is complex. Defaulting to 'manual' (in-situ) as a fallback.
|
|
endpoint = 'marine/manual/records-by-station?station_id=$stationId&date=$dateStr';
|
|
break;
|
|
}
|
|
|
|
debugPrint("MarineApiService: Calling API endpoint: $endpoint");
|
|
|
|
final response = await _baseService.get(baseUrl, endpoint);
|
|
|
|
// This parsing logic assumes the server nests the list inside {'data': {'data': [...]}}
|
|
// Adjust if your API response is different
|
|
if (response['success'] == true && response['data'] is Map && response['data']['data'] is List) {
|
|
return {
|
|
'success': true,
|
|
'data': response['data']['data'], // Return the inner 'data' list
|
|
'message': response['message'],
|
|
};
|
|
}
|
|
|
|
// Return original response if structure doesn't match
|
|
return response;
|
|
}
|
|
|
|
// --- ADDED: Method to send email request (Fixes the issue) ---
|
|
/// Sends the selected image URLs to the server for emailing.
|
|
Future<Map<String, dynamic>> sendImageRequestEmail({
|
|
required String recipientEmail,
|
|
required List<String> imageUrls,
|
|
required String stationName,
|
|
required String samplingDate,
|
|
}) async {
|
|
final baseUrl = await _serverConfigService.getActiveApiUrl();
|
|
|
|
final Map<String, String> fields = {
|
|
'recipientEmail': recipientEmail,
|
|
'imageUrls': jsonEncode(imageUrls), // Encode list as JSON string
|
|
'stationName': stationName,
|
|
'samplingDate': samplingDate,
|
|
};
|
|
|
|
// Use postMultipart (even with no files) as it's common for this kind of endpoint
|
|
return _baseService.postMultipart(
|
|
baseUrl: baseUrl,
|
|
endpoint: 'marine/images/send-email', // **IMPORTANT**: Verify this endpoint
|
|
fields: fields,
|
|
files: {}, // No files being uploaded, just data
|
|
);
|
|
}
|
|
} |