import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; class LocationBridge { static const MethodChannel _methodChannel = MethodChannel( 'com.traccar.client/tracking', ); static const EventChannel _eventChannel = EventChannel( 'com.traccar.client/location_updates', ); static Stream>? _locationStream; static Future startTracking({Map? config}) async { debugPrint('LocationBridge.startTracking called with config: $config'); try { if (config != null) { await _methodChannel.invokeMethod('updateConfig', config); } final result = await _methodChannel.invokeMethod('startTracking'); debugPrint('LocationBridge.startTracking result: $result'); return result ?? false; } on PlatformException catch (e) { debugPrint('LocationBridge.startTracking failed: ${e.message}'); return false; } } static Future stopTracking() async { debugPrint('LocationBridge.stopTracking called'); try { final result = await _methodChannel.invokeMethod('stopTracking'); debugPrint('LocationBridge.stopTracking result: $result'); return result ?? false; } on PlatformException catch (e) { debugPrint('LocationBridge.stopTracking failed: ${e.message}'); return false; } } static Future updateConfig(Map config) async { try { final result = await _methodChannel.invokeMethod( 'updateConfig', config, ); return result ?? false; } on PlatformException catch (e) { debugPrint('Failed to update config: ${e.message}'); return false; } } static Future?> getStatus() async { debugPrint('LocationBridge.getStatus called'); try { final result = await _methodChannel.invokeMethod('getStatus'); debugPrint('LocationBridge.getStatus result: $result'); return result?.cast(); } on PlatformException catch (e) { debugPrint('LocationBridge.getStatus failed: ${e.message}'); return null; } } static Future reportLocation() async { try { final result = await _methodChannel.invokeMethod('reportLocation'); return result ?? false; } on PlatformException catch (e) { debugPrint('Failed to report location: ${e.message}'); return false; } } static Stream> get locationUpdates { _locationStream ??= _eventChannel.receiveBroadcastStream().map( (event) => Map.from(event as Map), ); return _locationStream!; } static Future>> getLogs({int limit = 100}) async { try { final result = await _methodChannel.invokeMethod('getLogs', limit); return result?.map((e) => Map.from(e as Map)).toList() ?? []; } on PlatformException catch (e) { debugPrint('Failed to get logs: ${e.message}'); return []; } } static Future openBatteryOptimizationSettings() async { try { final result = await _methodChannel.invokeMethod( 'openBatteryOptimizationSettings', ); return result ?? false; } on PlatformException catch (e) { debugPrint('Failed to open battery settings: ${e.message}'); return false; } } static Future isBatteryOptimizationDisabled() async { try { final result = await _methodChannel.invokeMethod( 'isBatteryOptimizationDisabled', ); return result ?? false; } on PlatformException catch (e) { debugPrint('Failed to check battery optimization: ${e.message}'); return false; } } }