f1e88b1ac3
- Add shared_preferences for persisting server URL - Add SettingsService and PlatformService - Add server URL input field on non-web platforms - Make ApiConfig baseUrl configurable at runtime - Add Android location permissions (ACCESS_FINE/COURSE_LOCATION, INTERNET) - Request location permission on login and map init - Fix geo_id type: use String instead of int (UUID format) - Align share_service with API spec: remove unique_id, use share_id only - Fix watch endpoint response: last_update instead of created_at - Add error handling with SnackBars for geo operations - Wrap login screen in SingleChildScrollView for keyboard handling - Update map tile layer with userAgentPackageName for OSM
57 lines
1.8 KiB
Dart
57 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '../config/api.dart';
|
|
|
|
class ShareService {
|
|
final http.Client _client;
|
|
|
|
ShareService({http.Client? client}) : _client = client ?? http.Client();
|
|
|
|
Future<Map<String, dynamic>> createShare(String token, double x, double y) async {
|
|
final response = await _client.post(
|
|
Uri.parse(ApiConfig.shareUrl),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer $token',
|
|
},
|
|
body: jsonEncode({'x': x, 'y': y}),
|
|
);
|
|
|
|
if (response.statusCode == 201) {
|
|
try {
|
|
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
|
return {
|
|
'geo_id': data['geo_id']?.toString() ?? '',
|
|
'share_id': data['share_id']?.toString() ?? '',
|
|
};
|
|
} catch (e) {
|
|
throw Exception('Failed to parse response: $e | Body: ${response.body.substring(0, response.body.length > 200 ? 200 : response.body.length)}');
|
|
}
|
|
} else {
|
|
final errorBody = response.body.length > 200 ? response.body.substring(0, 200) : response.body;
|
|
throw Exception('Failed to create share link: ${response.statusCode} - $errorBody');
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getPositionByShareId(String token, String shareId) async {
|
|
final response = await _client.get(
|
|
Uri.parse('${ApiConfig.watchUrl}?share_id=$shareId'),
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
|
return {
|
|
'x': data['x'],
|
|
'y': data['y'],
|
|
'last_update': data['last_update']?.toString() ?? '',
|
|
'expires_at': data['expires_at']?.toString() ?? '',
|
|
};
|
|
} else {
|
|
throw Exception('Share link not found or no position available');
|
|
}
|
|
}
|
|
}
|