fix: critical bugs preventing location reporting to server

- Send config to native before starting tracking (SharedPreferences sync)
- Register ConnectivityReceiver for network state detection
- Fix Settings number fields using TextEditingControllers
- Cancel stream subscription on widget dispose (memory leak)
- Replace WorkManager with AlarmManager for heartbeat
- Add log cleanup for logs older than 24 hours
- Change HTTP method from GET to POST

These fixes resolve the 'device always offline' issue where:
1. Config was not being sent to native service
2. ConnectivityReceiver was never registered
3. Settings number fields were not saving
4. Heartbeat never fired due to WorkManager process isolation
5. Server expected POST not GET
This commit is contained in:
fiatcode 2026-04-30 15:38:24 +07:00
parent 3ae8bf00c1
commit bbd51d1c35
No known key found for this signature in database
10 changed files with 1200 additions and 167 deletions

View file

@ -12,22 +12,29 @@ class LocationBridge {
static Stream<Map<String, dynamic>>? _locationStream;
static Future<bool> startTracking() async {
static Future<bool> startTracking({Map<String, dynamic>? config}) async {
debugPrint('LocationBridge.startTracking called with config: $config');
try {
if (config != null) {
await _methodChannel.invokeMethod('updateConfig', config);
}
final result = await _methodChannel.invokeMethod<bool>('startTracking');
debugPrint('LocationBridge.startTracking result: $result');
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Failed to start tracking: ${e.message}');
debugPrint('LocationBridge.startTracking failed: ${e.message}');
return false;
}
}
static Future<bool> stopTracking() async {
debugPrint('LocationBridge.stopTracking called');
try {
final result = await _methodChannel.invokeMethod<bool>('stopTracking');
debugPrint('LocationBridge.stopTracking result: $result');
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Failed to stop tracking: ${e.message}');
debugPrint('LocationBridge.stopTracking failed: ${e.message}');
return false;
}
}
@ -46,11 +53,13 @@ class LocationBridge {
}
static Future<Map<String, dynamic>?> getStatus() async {
debugPrint('LocationBridge.getStatus called');
try {
final result = await _methodChannel.invokeMethod<Map>('getStatus');
debugPrint('LocationBridge.getStatus result: $result');
return result?.cast<String, dynamic>();
} on PlatformException catch (e) {
debugPrint('Failed to get status: ${e.message}');
debugPrint('LocationBridge.getStatus failed: ${e.message}');
return null;
}
}
@ -71,4 +80,15 @@ class LocationBridge {
);
return _locationStream!;
}
static Future<List<Map<String, dynamic>>> getLogs({int limit = 100}) async {
try {
final result = await _methodChannel.invokeMethod<List>('getLogs', limit);
return result?.map((e) => Map<String, dynamic>.from(e as Map)).toList() ??
[];
} on PlatformException catch (e) {
debugPrint('Failed to get logs: ${e.message}');
return [];
}
}
}