Workforce rotation settings and location permission handling

This commit is contained in:
2026-03-08 17:31:39 +08:00
parent ce82a88e04
commit 8bf0dc13d7
8 changed files with 1200 additions and 26 deletions
+49
View File
@@ -6,6 +6,7 @@ import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/live_position.dart';
import '../services/background_location_service.dart';
import '../utils/location_permission.dart';
import 'profile_provider.dart';
import 'supabase_provider.dart';
import 'stream_recovery.dart';
@@ -49,10 +50,27 @@ class WhereaboutsController {
return data as bool? ?? false;
}
/// Public convenience: fetch device position and upsert to live_positions.
/// Call after check-in / check-out to ensure immediate data freshness.
Future<void> updatePositionNow() => _updatePositionNow();
/// Toggle allow_tracking preference.
///
/// When enabling, requests location permission and immediately saves the
/// current position to `live_positions`. When disabling, updates position
/// one last time (so the final location is recorded) then removes the entry.
Future<void> setTracking(bool allow) async {
final userId = _client.auth.currentUser?.id;
if (userId == null) throw Exception('Not authenticated');
if (allow) {
// Ensure location permission before enabling
final granted = await ensureLocationPermission();
if (!granted) {
throw Exception('Location permission is required for tracking');
}
}
await _client
.from('profiles')
.update({'allow_tracking': allow})
@@ -61,12 +79,43 @@ class WhereaboutsController {
// Start or stop background location updates
if (allow) {
await startBackgroundLocationUpdates();
// Immediately save current position
await _updatePositionNow();
} else {
// Record final position before removing
await _updatePositionNow();
await stopBackgroundLocationUpdates();
// Remove the live position entry
await _client.from('live_positions').delete().eq('user_id', userId);
}
}
/// Immediately fetches current position and upserts into live_positions.
Future<void> _updatePositionNow() async {
try {
final serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) return;
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
return;
}
final position = await Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
),
);
await _client.rpc(
'update_live_position',
params: {'p_lat': position.latitude, 'p_lng': position.longitude},
);
} catch (_) {
// Best-effort; don't block the toggle action
}
}
}
/// Background location reporting service.