Tracking now works via timed interval
This commit is contained in:
@@ -6,6 +6,7 @@ import 'package:fforte/services/notification_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class TrackingService {
|
||||
static final TrackingService _instance = TrackingService._internal();
|
||||
@@ -14,8 +15,9 @@ class TrackingService {
|
||||
|
||||
List<LatLng> pathList = [];
|
||||
List<double> accuracyList = [];
|
||||
StreamSubscription<Position>? positionStream;
|
||||
Timer? _positionTimer;
|
||||
bool isTracking = false;
|
||||
BuildContext? _lastContext;
|
||||
final _positionController = StreamController<Position>.broadcast();
|
||||
final _statsController = StreamController<TrackingStats>.broadcast();
|
||||
|
||||
@@ -44,34 +46,49 @@ class TrackingService {
|
||||
Future<void> startTracking(BuildContext context) async {
|
||||
if (isTracking) return;
|
||||
|
||||
_lastContext = context;
|
||||
await NotificationService().initNotification();
|
||||
NotificationService().showNotification(
|
||||
title: AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
);
|
||||
|
||||
positionStream = Geolocator.getPositionStream(
|
||||
locationSettings: AndroidSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
distanceFilter: 0,
|
||||
foregroundNotificationConfig: ForegroundNotificationConfig(
|
||||
notificationTitle: AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||
notificationText: "",
|
||||
),
|
||||
),
|
||||
).listen((Position? position) {
|
||||
if (position != null) {
|
||||
// Get tracking interval from settings
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final intervalSeconds = prefs.getInt('trackingInterval') ?? 60;
|
||||
|
||||
// Create a timer that triggers position updates
|
||||
_positionTimer = Timer.periodic(Duration(seconds: intervalSeconds), (_) async {
|
||||
try {
|
||||
final Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high,
|
||||
);
|
||||
|
||||
pathList.add(LatLng(position.latitude, position.longitude));
|
||||
accuracyList.add(position.accuracy);
|
||||
currentAccuracy = position.accuracy;
|
||||
_positionController.add(position);
|
||||
_updateStats();
|
||||
} catch (e) {
|
||||
NotificationService().deleteNotification();
|
||||
NotificationService().showNotification(title: "ERROR: $e");
|
||||
}
|
||||
});
|
||||
|
||||
positionStream!.onError((e) {
|
||||
// Get initial position immediately
|
||||
try {
|
||||
final Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high,
|
||||
);
|
||||
|
||||
pathList.add(LatLng(position.latitude, position.longitude));
|
||||
accuracyList.add(position.accuracy);
|
||||
currentAccuracy = position.accuracy;
|
||||
_positionController.add(position);
|
||||
_updateStats();
|
||||
} catch (e) {
|
||||
NotificationService().deleteNotification();
|
||||
NotificationService().showNotification(title: "ERROR: $e");
|
||||
});
|
||||
}
|
||||
|
||||
isTracking = true;
|
||||
}
|
||||
@@ -131,21 +148,24 @@ class TrackingService {
|
||||
}
|
||||
|
||||
void pauseTracking() {
|
||||
positionStream?.pause();
|
||||
_positionTimer?.cancel();
|
||||
isTracking = false;
|
||||
}
|
||||
|
||||
void resumeTracking() {
|
||||
positionStream?.resume();
|
||||
if (!isTracking && _lastContext != null) {
|
||||
startTracking(_lastContext!);
|
||||
}
|
||||
isTracking = true;
|
||||
}
|
||||
|
||||
void stopTracking() {
|
||||
positionStream?.cancel();
|
||||
_positionTimer?.cancel();
|
||||
NotificationService().deleteNotification();
|
||||
isTracking = false;
|
||||
accuracyList.clear();
|
||||
currentAccuracy = null;
|
||||
_lastContext = null;
|
||||
}
|
||||
|
||||
void clearPath() {
|
||||
|
||||
Reference in New Issue
Block a user