Tracking now works via timed interval
This commit is contained in:
@@ -22,7 +22,7 @@ class Tracking extends StatefulWidget {
|
|||||||
|
|
||||||
class _TrackingState extends State<Tracking> {
|
class _TrackingState extends State<Tracking> {
|
||||||
final TrackingService _trackingService = TrackingService();
|
final TrackingService _trackingService = TrackingService();
|
||||||
LocationMarkerPosition? locationMarkerPosition;
|
Position? currentPosition;
|
||||||
MapController mapController = MapController();
|
MapController mapController = MapController();
|
||||||
StreamSubscription? _positionSubscription;
|
StreamSubscription? _positionSubscription;
|
||||||
StreamSubscription? _statsSubscription;
|
StreamSubscription? _statsSubscription;
|
||||||
@@ -48,11 +48,7 @@ class _TrackingState extends State<Tracking> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
locationMarkerPosition = LocationMarkerPosition(
|
currentPosition = widget.startPosition;
|
||||||
latitude: widget.startPosition.latitude,
|
|
||||||
longitude: widget.startPosition.longitude,
|
|
||||||
accuracy: widget.startPosition.accuracy,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Initialisiere die Statistiken sofort
|
// Initialisiere die Statistiken sofort
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -62,11 +58,7 @@ class _TrackingState extends State<Tracking> {
|
|||||||
|
|
||||||
_positionSubscription = _trackingService.positionStream$.listen((position) {
|
_positionSubscription = _trackingService.positionStream$.listen((position) {
|
||||||
setState(() {
|
setState(() {
|
||||||
locationMarkerPosition = LocationMarkerPosition(
|
currentPosition = position;
|
||||||
latitude: position.latitude,
|
|
||||||
longitude: position.longitude,
|
|
||||||
accuracy: position.accuracy,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -163,11 +155,7 @@ class _TrackingState extends State<Tracking> {
|
|||||||
if (_trackingService.isTracking) {
|
if (_trackingService.isTracking) {
|
||||||
_trackingService.pauseTracking();
|
_trackingService.pauseTracking();
|
||||||
} else {
|
} else {
|
||||||
if (_trackingService.positionStream == null) {
|
|
||||||
_trackingService.startTracking(context);
|
_trackingService.startTracking(context);
|
||||||
} else {
|
|
||||||
_trackingService.resumeTracking();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -181,8 +169,8 @@ class _TrackingState extends State<Tracking> {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
mapController.move(
|
mapController.move(
|
||||||
LatLng(
|
LatLng(
|
||||||
locationMarkerPosition!.latitude,
|
currentPosition!.latitude,
|
||||||
locationMarkerPosition!.longitude,
|
currentPosition!.longitude,
|
||||||
),
|
),
|
||||||
16,
|
16,
|
||||||
);
|
);
|
||||||
@@ -218,6 +206,31 @@ class _TrackingState extends State<Tracking> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
if (currentPosition != null)
|
||||||
|
CircleLayer(
|
||||||
|
circles: [
|
||||||
|
CircleMarker(
|
||||||
|
point: LatLng(
|
||||||
|
currentPosition!.latitude,
|
||||||
|
currentPosition!.longitude,
|
||||||
|
),
|
||||||
|
radius: currentPosition!.accuracy,
|
||||||
|
color: Colors.blue.withOpacity(0.2),
|
||||||
|
borderColor: Colors.blue,
|
||||||
|
borderStrokeWidth: 2,
|
||||||
|
),
|
||||||
|
CircleMarker(
|
||||||
|
point: LatLng(
|
||||||
|
currentPosition!.latitude,
|
||||||
|
currentPosition!.longitude,
|
||||||
|
),
|
||||||
|
radius: 5,
|
||||||
|
color: Colors.blue,
|
||||||
|
borderColor: Colors.white,
|
||||||
|
borderStrokeWidth: 2,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
CurrentLocationLayer(),
|
CurrentLocationLayer(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -10,36 +10,75 @@ class Settings extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _SettingsState extends State<Settings> {
|
class _SettingsState extends State<Settings> {
|
||||||
|
int _trackingInterval = 60;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_loadSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadSettings() async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
setState(() {
|
||||||
|
_trackingInterval = prefs.getInt('trackingInterval') ?? 60;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _saveTrackingInterval(int value) async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
await prefs.setInt('trackingInterval', value);
|
||||||
|
setState(() {
|
||||||
|
_trackingInterval = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Future<String> _getSaveDir() async {
|
Future<String> _getSaveDir() async {
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
final String saveDir = prefs.getString('saveDir') ?? "";
|
final String saveDir = prefs.getString('saveDir') ?? "";
|
||||||
|
|
||||||
return saveDir;
|
return saveDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings),),
|
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings),),
|
||||||
body: Center(
|
body: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(AppLocalizations.of(context)!.filelocation, style: const TextStyle(fontSize: 20),),
|
Text(AppLocalizations.of(context)!.filelocation, style: const TextStyle(fontSize: 20),),
|
||||||
FutureBuilder(future: _getSaveDir(), builder: (context, snapshot) {
|
FutureBuilder(
|
||||||
|
future: _getSaveDir(),
|
||||||
|
builder: (context, snapshot) {
|
||||||
if (snapshot.connectionState == ConnectionState.done) {
|
if (snapshot.connectionState == ConnectionState.done) {
|
||||||
return Text(snapshot.data ?? "");
|
return Text(snapshot.data ?? "");
|
||||||
} else {
|
} else {
|
||||||
return const CircularProgressIndicator();
|
return const CircularProgressIndicator();
|
||||||
}
|
}
|
||||||
}),
|
}
|
||||||
ElevatedButton(onPressed: () {
|
),
|
||||||
|
ElevatedButton(
|
||||||
}, child: Text(AppLocalizations.of(context)!.open))
|
onPressed: () {},
|
||||||
|
child: Text(AppLocalizations.of(context)!.open)
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
const Text(
|
||||||
|
'Tracking Interval (Sekunden)',
|
||||||
|
style: TextStyle(fontSize: 20),
|
||||||
|
),
|
||||||
|
Slider(
|
||||||
|
value: _trackingInterval.toDouble(),
|
||||||
|
min: 10,
|
||||||
|
max: 300,
|
||||||
|
divisions: 29,
|
||||||
|
label: _trackingInterval.toString(),
|
||||||
|
onChanged: (double value) {
|
||||||
|
_saveTrackingInterval(value.round());
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Text('Aktuelles Intervall: $_trackingInterval Sekunden'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import 'package:fforte/services/notification_service.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
import 'package:latlong2/latlong.dart';
|
import 'package:latlong2/latlong.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class TrackingService {
|
class TrackingService {
|
||||||
static final TrackingService _instance = TrackingService._internal();
|
static final TrackingService _instance = TrackingService._internal();
|
||||||
@@ -14,8 +15,9 @@ class TrackingService {
|
|||||||
|
|
||||||
List<LatLng> pathList = [];
|
List<LatLng> pathList = [];
|
||||||
List<double> accuracyList = [];
|
List<double> accuracyList = [];
|
||||||
StreamSubscription<Position>? positionStream;
|
Timer? _positionTimer;
|
||||||
bool isTracking = false;
|
bool isTracking = false;
|
||||||
|
BuildContext? _lastContext;
|
||||||
final _positionController = StreamController<Position>.broadcast();
|
final _positionController = StreamController<Position>.broadcast();
|
||||||
final _statsController = StreamController<TrackingStats>.broadcast();
|
final _statsController = StreamController<TrackingStats>.broadcast();
|
||||||
|
|
||||||
@@ -44,34 +46,49 @@ class TrackingService {
|
|||||||
Future<void> startTracking(BuildContext context) async {
|
Future<void> startTracking(BuildContext context) async {
|
||||||
if (isTracking) return;
|
if (isTracking) return;
|
||||||
|
|
||||||
|
_lastContext = context;
|
||||||
await NotificationService().initNotification();
|
await NotificationService().initNotification();
|
||||||
NotificationService().showNotification(
|
NotificationService().showNotification(
|
||||||
title: AppLocalizations.of(context)!.trackingRunningInBackground,
|
title: AppLocalizations.of(context)!.trackingRunningInBackground,
|
||||||
);
|
);
|
||||||
|
|
||||||
positionStream = Geolocator.getPositionStream(
|
// Get tracking interval from settings
|
||||||
locationSettings: AndroidSettings(
|
final prefs = await SharedPreferences.getInstance();
|
||||||
accuracy: LocationAccuracy.high,
|
final intervalSeconds = prefs.getInt('trackingInterval') ?? 60;
|
||||||
distanceFilter: 0,
|
|
||||||
foregroundNotificationConfig: ForegroundNotificationConfig(
|
// Create a timer that triggers position updates
|
||||||
notificationTitle: AppLocalizations.of(context)!.trackingRunningInBackground,
|
_positionTimer = Timer.periodic(Duration(seconds: intervalSeconds), (_) async {
|
||||||
notificationText: "",
|
try {
|
||||||
),
|
final Position position = await Geolocator.getCurrentPosition(
|
||||||
),
|
desiredAccuracy: LocationAccuracy.high,
|
||||||
).listen((Position? position) {
|
);
|
||||||
if (position != null) {
|
|
||||||
pathList.add(LatLng(position.latitude, position.longitude));
|
pathList.add(LatLng(position.latitude, position.longitude));
|
||||||
accuracyList.add(position.accuracy);
|
accuracyList.add(position.accuracy);
|
||||||
currentAccuracy = position.accuracy;
|
currentAccuracy = position.accuracy;
|
||||||
_positionController.add(position);
|
_positionController.add(position);
|
||||||
_updateStats();
|
_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().deleteNotification();
|
||||||
NotificationService().showNotification(title: "ERROR: $e");
|
NotificationService().showNotification(title: "ERROR: $e");
|
||||||
});
|
}
|
||||||
|
|
||||||
isTracking = true;
|
isTracking = true;
|
||||||
}
|
}
|
||||||
@@ -131,21 +148,24 @@ class TrackingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pauseTracking() {
|
void pauseTracking() {
|
||||||
positionStream?.pause();
|
_positionTimer?.cancel();
|
||||||
isTracking = false;
|
isTracking = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resumeTracking() {
|
void resumeTracking() {
|
||||||
positionStream?.resume();
|
if (!isTracking && _lastContext != null) {
|
||||||
|
startTracking(_lastContext!);
|
||||||
|
}
|
||||||
isTracking = true;
|
isTracking = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopTracking() {
|
void stopTracking() {
|
||||||
positionStream?.cancel();
|
_positionTimer?.cancel();
|
||||||
NotificationService().deleteNotification();
|
NotificationService().deleteNotification();
|
||||||
isTracking = false;
|
isTracking = false;
|
||||||
accuracyList.clear();
|
accuracyList.clear();
|
||||||
currentAccuracy = null;
|
currentAccuracy = null;
|
||||||
|
_lastContext = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearPath() {
|
void clearPath() {
|
||||||
|
|||||||
Reference in New Issue
Block a user