Support multiple tracked people with colored markers and watch menu
- Replace single tracking with list of TrackedPerson objects in ShareProvider - Each tracked person gets a unique color from a predefined palette - Add watch menu (bottom sheet) with list of tracked people, coordinates, and remove buttons - Add ability to add new tracked people by Share ID - Render multiple colored markers on the map - Start/stop periodic polling for all tracked people
This commit is contained in:
@@ -1,37 +1,62 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../services/share_service.dart';
|
||||
|
||||
class TrackedPerson {
|
||||
final String shareId;
|
||||
final String name;
|
||||
final Color color;
|
||||
Map<String, dynamic>? position;
|
||||
Timer? timer;
|
||||
|
||||
TrackedPerson({
|
||||
required this.shareId,
|
||||
required this.color,
|
||||
this.name = '',
|
||||
this.position,
|
||||
this.timer,
|
||||
});
|
||||
}
|
||||
|
||||
class ShareProvider with ChangeNotifier {
|
||||
final ShareService _shareService;
|
||||
|
||||
|
||||
String _shareId = '';
|
||||
String _geoId = '';
|
||||
bool _isLoading = false;
|
||||
String _error = '';
|
||||
|
||||
|
||||
Map<String, dynamic>? _position;
|
||||
|
||||
String _trackingShareId = '';
|
||||
String _trackingToken = '';
|
||||
Map<String, dynamic>? _trackedPosition;
|
||||
|
||||
ShareProvider({ShareService? shareService})
|
||||
: _shareService = shareService ?? ShareService();
|
||||
|
||||
|
||||
final List<TrackedPerson> _trackedPeople = [];
|
||||
final List<Color> _markerColors = [
|
||||
Colors.red,
|
||||
Colors.green,
|
||||
Colors.orange,
|
||||
Colors.purple,
|
||||
Colors.teal,
|
||||
Colors.pink,
|
||||
Colors.brown,
|
||||
Colors.indigo,
|
||||
];
|
||||
int _nextColorIndex = 0;
|
||||
|
||||
ShareProvider({ShareService? shareService})
|
||||
: _shareService = shareService ?? ShareService();
|
||||
|
||||
String get shareId => _shareId;
|
||||
String get geoId => _geoId;
|
||||
bool get isLoading => _isLoading;
|
||||
String get error => _error;
|
||||
Map<String, dynamic>? get position => _position;
|
||||
|
||||
String get trackingShareId => _trackingShareId;
|
||||
Map<String, dynamic>? get trackedPosition => _trackedPosition;
|
||||
|
||||
|
||||
List<TrackedPerson> get trackedPeople => List.unmodifiable(_trackedPeople);
|
||||
|
||||
Future<void> createShare(String token, double x, double y) async {
|
||||
_isLoading = true;
|
||||
_error = '';
|
||||
notifyListeners();
|
||||
|
||||
|
||||
try {
|
||||
final result = await _shareService.createShare(token, x, y);
|
||||
_geoId = result['geo_id']?.toString() ?? '';
|
||||
@@ -46,12 +71,12 @@ class ShareProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<void> getPosition(String token, String shareId) async {
|
||||
_isLoading = true;
|
||||
_error = '';
|
||||
notifyListeners();
|
||||
|
||||
|
||||
try {
|
||||
_position = await _shareService.getPositionByShareId(token, shareId);
|
||||
notifyListeners();
|
||||
@@ -64,7 +89,7 @@ class ShareProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void clearShare() {
|
||||
_shareId = '';
|
||||
_geoId = '';
|
||||
@@ -72,20 +97,29 @@ class ShareProvider with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> startTracking(String shareId, String token) async {
|
||||
_trackingShareId = shareId;
|
||||
_trackingToken = token;
|
||||
_error = '';
|
||||
Future<void> addTracking(String shareId, String token) async {
|
||||
if (_trackedPeople.any((p) => p.shareId == shareId)) return;
|
||||
|
||||
final color = _markerColors[_nextColorIndex % _markerColors.length];
|
||||
_nextColorIndex++;
|
||||
|
||||
final person = TrackedPerson(
|
||||
shareId: shareId,
|
||||
color: color,
|
||||
name: 'Person ${_trackedPeople.length + 1}',
|
||||
);
|
||||
_trackedPeople.add(person);
|
||||
notifyListeners();
|
||||
|
||||
await updateTrackedPosition();
|
||||
|
||||
await _updatePersonPosition(token, person);
|
||||
}
|
||||
|
||||
Future<void> updateTrackedPosition() async {
|
||||
if (_trackingShareId.isEmpty) return;
|
||||
|
||||
Future<void> _updatePersonPosition(String token, TrackedPerson person) async {
|
||||
try {
|
||||
_trackedPosition = await _shareService.getPositionByShareId(_trackingToken, _trackingShareId);
|
||||
person.position = await _shareService.getPositionByShareId(
|
||||
token,
|
||||
person.shareId,
|
||||
);
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
_error = e.toString();
|
||||
@@ -93,10 +127,30 @@ class ShareProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
void stopTracking() {
|
||||
_trackingShareId = '';
|
||||
_trackingToken = '';
|
||||
_trackedPosition = null;
|
||||
void startTrackingPolling(String token) {
|
||||
for (final person in _trackedPeople) {
|
||||
person.timer = Timer.periodic(
|
||||
const Duration(seconds: 5),
|
||||
(_) => _updatePersonPosition(token, person),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void removeTracking(String shareId) {
|
||||
final index = _trackedPeople.indexWhere((p) => p.shareId == shareId);
|
||||
if (index != -1) {
|
||||
_trackedPeople[index].timer?.cancel();
|
||||
_trackedPeople.removeAt(index);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void stopAllTracking() {
|
||||
for (final person in _trackedPeople) {
|
||||
person.timer?.cancel();
|
||||
}
|
||||
_trackedPeople.clear();
|
||||
_nextColorIndex = 0;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user