Add Android support with configurable server URL and location permissions
- 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
This commit is contained in:
@@ -4,7 +4,7 @@ import '../config/api.dart';
|
||||
|
||||
class AuthService {
|
||||
final http.Client _client;
|
||||
|
||||
|
||||
AuthService({http.Client? client}) : _client = client ?? http.Client();
|
||||
|
||||
Future<String> login(String login, String password) async {
|
||||
@@ -13,7 +13,7 @@ class AuthService {
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'login': login, 'password': password}),
|
||||
);
|
||||
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return response.body;
|
||||
} else {
|
||||
@@ -21,15 +21,23 @@ class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> register(String login, String password) async {
|
||||
Future<void> register(
|
||||
String login,
|
||||
String password,
|
||||
String secretKeyHash,
|
||||
) async {
|
||||
final response = await _client.post(
|
||||
Uri.parse(ApiConfig.regUrl),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'login': login, 'password': password}),
|
||||
body: jsonEncode({
|
||||
'login': login,
|
||||
'password': password,
|
||||
'secret_key_hash': secretKeyHash,
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
if (response.statusCode != 201) {
|
||||
throw Exception('Registration failed');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ class GeoService {
|
||||
|
||||
GeoService({http.Client? client}) : _client = client ?? http.Client();
|
||||
|
||||
Future<void> updatePosition(String token, int id, double x, double y) async {
|
||||
Future<void> updatePosition(String token, String id, double x, double y) async {
|
||||
final response = await _client.put(
|
||||
Uri.parse('${ApiConfig.geoUrl}?id=$id'),
|
||||
headers: {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'dart:io' show Platform;
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
|
||||
class PlatformService {
|
||||
bool get isWeb => kIsWeb;
|
||||
bool get isNative => !kIsWeb;
|
||||
String get platformName => isWeb ? 'web' : Platform.isAndroid ? 'android' : Platform.isIOS ? 'ios' : Platform.isWindows ? 'windows' : Platform.isMacOS ? 'macos' : Platform.isLinux ? 'linux' : 'unknown';
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class SettingsService {
|
||||
static const String _baseUrlKey = 'server_base_url';
|
||||
static const String _defaultBaseUrl = 'https://family-safety.onrender.com/api';
|
||||
|
||||
String _baseUrl = _defaultBaseUrl;
|
||||
bool _initialized = false;
|
||||
|
||||
String get baseUrl => _baseUrl;
|
||||
|
||||
Future<void> initialize() async {
|
||||
if (_initialized) return;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_baseUrl = prefs.getString(_baseUrlKey) ?? _defaultBaseUrl;
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
Future<void> setBaseUrl(String url) async {
|
||||
_baseUrl = url;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_baseUrlKey, url);
|
||||
}
|
||||
|
||||
void resetToDefault() {
|
||||
_baseUrl = _defaultBaseUrl;
|
||||
}
|
||||
}
|
||||
@@ -18,35 +18,18 @@ class ShareService {
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
final data = jsonDecode(response.body);
|
||||
return {
|
||||
'geo_id': data['geo_id'],
|
||||
'share_id': data['share_id'],
|
||||
};
|
||||
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 {
|
||||
throw Exception('Failed to create share link');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getPosition(String token, String uniqueId) async {
|
||||
final response = await _client.get(
|
||||
Uri.parse('${ApiConfig.watchUrl}?unique_id=$uniqueId'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
return {
|
||||
'id': data['id'],
|
||||
'x': data['x'],
|
||||
'y': data['y'],
|
||||
'created_at': data['created_at'],
|
||||
'expires_at': data['expires_at'],
|
||||
};
|
||||
} else {
|
||||
throw Exception('Share link not found or no position available');
|
||||
final errorBody = response.body.length > 200 ? response.body.substring(0, 200) : response.body;
|
||||
throw Exception('Failed to create share link: ${response.statusCode} - $errorBody');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,15 +42,15 @@ class ShareService {
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
return {
|
||||
'x': data['x'],
|
||||
'y': data['y'],
|
||||
'created_at': data['created_at'],
|
||||
'expires_at': data['expires_at'],
|
||||
'last_update': data['last_update']?.toString() ?? '',
|
||||
'expires_at': data['expires_at']?.toString() ?? '',
|
||||
};
|
||||
} else {
|
||||
throw Exception('Share link not found or no position available');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user