c4d4032b6b
- Add timestamp field to ProcessedGpsData with copyWith support - Add precision switching thresholds: 3.33 m/s (12 km/h) up, 2.22 m/s (8 km/h) down - Add PRECISION_SWITCH_FIRST_FIX_DELAY (5s) to prevent early switching - Add BACKGROUND_GRACE_PERIOD (7s) for lifecycle-aware GPS management - Mark GPS_GRACE_PERIOD as deprecated in favor of BACKGROUND_GRACE_PERIOD - Change GpsService.positionStream to createPositionStream() with dynamic accuracy - Keep deprecated positionStream getter for backward compatibility Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
57 lines
1.8 KiB
Dart
57 lines
1.8 KiB
Dart
import 'dart:async';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'config/gps_constants.dart';
|
|
|
|
class GpsService {
|
|
static double? _lastValidHeading;
|
|
|
|
/// Create a position stream with dynamic accuracy setting
|
|
static Stream<Position> createPositionStream({
|
|
LocationAccuracy accuracy = LocationAccuracy.high,
|
|
}) {
|
|
return Geolocator.getPositionStream(
|
|
locationSettings: LocationSettings(
|
|
accuracy: accuracy,
|
|
distanceFilter: 0,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Deprecated: Use createPositionStream() instead
|
|
@Deprecated('Use createPositionStream(accuracy: LocationAccuracy.bestForNavigation) instead')
|
|
static Stream<Position> get positionStream {
|
|
return createPositionStream(accuracy: LocationAccuracy.bestForNavigation);
|
|
}
|
|
|
|
static Future<bool> requestPermissions() async {
|
|
final permission = await Permission.locationWhenInUse.request();
|
|
return permission.isGranted;
|
|
}
|
|
|
|
static Future<bool> isLocationServiceEnabled() async {
|
|
return await Geolocator.isLocationServiceEnabled();
|
|
}
|
|
|
|
static double getCachedHeading(double heading) {
|
|
if (heading >= GpsConfig.VALID_HEADING_MIN && heading <= GpsConfig.VALID_HEADING_MAX) {
|
|
_lastValidHeading = heading;
|
|
return heading;
|
|
}
|
|
return _lastValidHeading ?? 0.0;
|
|
}
|
|
|
|
static String getCompassDirection(double heading) {
|
|
final cachedHeading = getCachedHeading(heading);
|
|
|
|
const directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
|
|
final index = ((cachedHeading + 22.5) / 45).floor() % 8;
|
|
return directions[index];
|
|
}
|
|
|
|
static String formatHeading(double heading) {
|
|
final cachedHeading = getCachedHeading(heading);
|
|
return '${cachedHeading.round()} ${getCompassDirection(heading)}'; // don't add that degree symbol, this is cooler
|
|
}
|
|
}
|