tracpulse/lib/bridge/location_bridge.dart

74 lines
2.1 KiB
Dart

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<Map<String, dynamic>>? _locationStream;
static Future<bool> startTracking() async {
try {
final result = await _methodChannel.invokeMethod<bool>('startTracking');
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Failed to start tracking: ${e.message}');
return false;
}
}
static Future<bool> stopTracking() async {
try {
final result = await _methodChannel.invokeMethod<bool>('stopTracking');
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Failed to stop tracking: ${e.message}');
return false;
}
}
static Future<bool> updateConfig(Map<String, dynamic> config) async {
try {
final result = await _methodChannel.invokeMethod<bool>(
'updateConfig',
config,
);
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Failed to update config: ${e.message}');
return false;
}
}
static Future<Map<String, dynamic>?> getStatus() async {
try {
final result = await _methodChannel.invokeMethod<Map>('getStatus');
return result?.cast<String, dynamic>();
} on PlatformException catch (e) {
debugPrint('Failed to get status: ${e.message}');
return null;
}
}
static Future<bool> reportLocation() async {
try {
final result = await _methodChannel.invokeMethod<bool>('reportLocation');
return result ?? false;
} on PlatformException catch (e) {
debugPrint('Failed to report location: ${e.message}');
return false;
}
}
static Stream<Map<String, dynamic>> get locationUpdates {
_locationStream ??= _eventChannel.receiveBroadcastStream().map(
(event) => Map<String, dynamic>.from(event as Map),
);
return _locationStream!;
}
}