fix marker rendering & modernize riding widgets for dark mode - 2026-04-11
This commit is contained in:
81
lib/services/emergency_signal_service.dart
Normal file
81
lib/services/emergency_signal_service.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:sensors_plus/sensors_plus.dart';
|
||||
|
||||
import '../print.dart';
|
||||
|
||||
class EmergencySignalService {
|
||||
static final EmergencySignalService instance = EmergencySignalService._();
|
||||
EmergencySignalService._();
|
||||
|
||||
StreamSubscription<AccelerometerEvent>? _accelerometerSubscription;
|
||||
DateTime? _lastShakeTime;
|
||||
int _shakeCount = 0;
|
||||
|
||||
// Custom thresholds for shaking (force required)
|
||||
final double _shakeThresholdGravity = 2.7;
|
||||
final int _shakeSlopTimeMs = 500;
|
||||
final int _shakeCountResetTimeMs = 3000;
|
||||
final int _targetShakes = 5;
|
||||
|
||||
VoidCallback? _onEmergencyTriggered;
|
||||
|
||||
/// Starts listening to phone movement
|
||||
void startListening(VoidCallback onEmergencyTriggered) {
|
||||
_onEmergencyTriggered = onEmergencyTriggered;
|
||||
|
||||
if (_accelerometerSubscription != null) return;
|
||||
|
||||
_accelerometerSubscription = accelerometerEvents.listen((event) {
|
||||
double x = event.x;
|
||||
double y = event.y;
|
||||
double z = event.z;
|
||||
|
||||
// Calculate the gForce using pythagorean theorem
|
||||
double gX = x / 9.80665;
|
||||
double gY = y / 9.80665;
|
||||
double gZ = z / 9.80665;
|
||||
|
||||
// Overall gForce
|
||||
double gForce = sqrt(gX * gX + gY * gY + gZ * gZ);
|
||||
|
||||
if (gForce > _shakeThresholdGravity) {
|
||||
final now = DateTime.now();
|
||||
|
||||
// Ignore shakes that are too close to each other
|
||||
if (_lastShakeTime != null &&
|
||||
now.difference(_lastShakeTime!).inMilliseconds < _shakeSlopTimeMs) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset the counter if elapsed more than the reset window
|
||||
if (_lastShakeTime != null &&
|
||||
now.difference(_lastShakeTime!).inMilliseconds >
|
||||
_shakeCountResetTimeMs) {
|
||||
_shakeCount = 0;
|
||||
}
|
||||
|
||||
_lastShakeTime = now;
|
||||
_shakeCount++;
|
||||
|
||||
if (kDebugMode) {
|
||||
Log.print("🚨 Shake detected! Count: $_shakeCount");
|
||||
}
|
||||
|
||||
if (_shakeCount >= _targetShakes) {
|
||||
_shakeCount = 0; // Reset counter
|
||||
if (_onEmergencyTriggered != null) {
|
||||
_onEmergencyTriggered!();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void stopListening() {
|
||||
_accelerometerSubscription?.cancel();
|
||||
_accelerometerSubscription = null;
|
||||
_shakeCount = 0;
|
||||
}
|
||||
}
|
||||
106
lib/services/offline_map_service.dart
Normal file
106
lib/services/offline_map_service.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:maplibre_gl/maplibre_gl.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'dart:math' as math;
|
||||
import '../../main.dart';
|
||||
import '../print.dart';
|
||||
|
||||
class OfflineMapService {
|
||||
static final OfflineMapService instance = OfflineMapService._();
|
||||
OfflineMapService._();
|
||||
|
||||
final _offlineRegionName = "UserRegion";
|
||||
bool _isDownloading = false;
|
||||
LatLng? _lastDownloadedCenter;
|
||||
|
||||
/// Calculate bounding box for a given center and radius in km
|
||||
LatLngBounds _calculateBounds(LatLng center, double radiusKm) {
|
||||
const double earthRadius = 6371.0;
|
||||
|
||||
// Latitude degrees per km
|
||||
double latDelta = (radiusKm / earthRadius) * (180 / math.pi);
|
||||
// Longitude degrees per km at given latitude
|
||||
double lngDelta = (radiusKm / earthRadius) *
|
||||
(180 / math.pi) /
|
||||
math.cos(center.latitude * math.pi / 180);
|
||||
|
||||
return LatLngBounds(
|
||||
southwest:
|
||||
LatLng(center.latitude - latDelta, center.longitude - lngDelta),
|
||||
northeast:
|
||||
LatLng(center.latitude + latDelta, center.longitude + lngDelta),
|
||||
);
|
||||
}
|
||||
|
||||
/// Downloads a specified radius around a coordinate
|
||||
Future<void> downloadRegion(LatLng center,
|
||||
{double radiusKm = 10.0,
|
||||
double minZoom = 6.0,
|
||||
double maxZoom = 15.0}) async {
|
||||
if (_isDownloading) return;
|
||||
|
||||
// Avoid re-downloading if the user hasn't moved significantly (e.g. > 5km)
|
||||
if (_lastDownloadedCenter != null) {
|
||||
double distance = _calculateDistance(center, _lastDownloadedCenter!);
|
||||
if (distance < 5.0) return; // skip if close to previously downloaded
|
||||
}
|
||||
|
||||
_isDownloading = true;
|
||||
|
||||
try {
|
||||
final bounds = _calculateBounds(center, radiusKm);
|
||||
|
||||
// Select style based on current theme
|
||||
final String styleStr =
|
||||
Get.isDarkMode ? "assets/style_dark.json" : "assets/style.json";
|
||||
|
||||
// iOS native crash guard: MLNTilePyramidOfflineRegion does not support relative asset URLs.
|
||||
// We skip native offline registration on iOS if using local assets to ensure stability.
|
||||
if (Platform.isIOS && !styleStr.startsWith('http')) {
|
||||
Log.print(
|
||||
"ℹ️ Skipping native offline registration on iOS for asset-based style to prevent crash.");
|
||||
return;
|
||||
}
|
||||
|
||||
final regionDefinition = OfflineRegionDefinition(
|
||||
bounds: bounds,
|
||||
mapStyleUrl: styleStr,
|
||||
minZoom: minZoom,
|
||||
maxZoom: maxZoom,
|
||||
);
|
||||
|
||||
// We'll update the last downloaded center immediately
|
||||
_lastDownloadedCenter = center;
|
||||
|
||||
// MapLibre standard API for offline downloads
|
||||
await downloadOfflineRegion(regionDefinition, metadata: {
|
||||
'name': '$_offlineRegionName-${center.latitude}-${center.longitude}',
|
||||
'downloadDate': DateTime.now().toIso8601String(),
|
||||
});
|
||||
|
||||
// Reassurance log for the user
|
||||
Log.print("📍 Map Ready: Service is utilizing local tile cache.");
|
||||
Log.print(
|
||||
"✅ Offline Map Cached for Region: $center (radius: ${radiusKm}km, style: $styleStr)");
|
||||
} catch (e) {
|
||||
Log.print("⚠️ Offline Map Download Failed: $e");
|
||||
} finally {
|
||||
_isDownloading = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to calculate distance in km
|
||||
double _calculateDistance(LatLng p1, LatLng p2) {
|
||||
var p = 0.017453292519943295;
|
||||
var c = math.cos;
|
||||
var a = 0.5 -
|
||||
c((p2.latitude - p1.latitude) * p) / 2 +
|
||||
c(p1.latitude * p) *
|
||||
c(p2.latitude * p) *
|
||||
(1 - c((p2.longitude - p1.longitude) * p)) /
|
||||
2;
|
||||
return 12742 * math.asin(math.sqrt(a));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../print.dart';
|
||||
|
||||
/// خدمة التحكم بوضع النافذة العائمة (Picture-in-Picture) على أندرويد.
|
||||
/// تُستدعى عند بدء الرحلة لتفعيل PiP تلقائياً عند خروج المستخدم من التطبيق.
|
||||
class PipService {
|
||||
@@ -23,7 +25,7 @@ class PipService {
|
||||
try {
|
||||
await _channel.invokeMethod('enablePip');
|
||||
} catch (e) {
|
||||
print('PiP enable error: \$e');
|
||||
Log.print('PiP enable error: \$e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +35,7 @@ class PipService {
|
||||
try {
|
||||
await _channel.invokeMethod('disablePip');
|
||||
} catch (e) {
|
||||
print('PiP disable error: \$e');
|
||||
Log.print('PiP disable error: \$e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +46,7 @@ class PipService {
|
||||
final result = await _channel.invokeMethod<bool>('enterPip');
|
||||
return result ?? false;
|
||||
} catch (e) {
|
||||
print('PiP enter error: \$e');
|
||||
Log.print('PiP enter error: \$e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user