26-1-20/1

This commit is contained in:
Hamza-Ayed
2026-01-20 10:11:10 +03:00
parent 374f9e9bf3
commit 3c0ae4cf2f
53 changed files with 89652 additions and 6861 deletions

View File

@@ -1,208 +1,178 @@
import 'dart:convert';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; // Import for WidgetsBinding
import 'package:geolocator/geolocator.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:just_audio/just_audio.dart'; // لتشغيل صوت عند وصول رحلة
import '../../constant/box_name.dart';
import '../../constant/links.dart';
import '../../main.dart'; // للوصول لـ box
import '../functions/crud.dart';
import '../functions/location_controller.dart';
class RideAvailableController extends GetxController {
bool isLoading = false;
// FIX 1: Initialize the map with a default structure.
// This prevents `rideAvailableMap['message']` from ever being null in the UI.
Map rideAvailableMap = {'message': []};
late LatLng southwest;
late LatLng northeast;
LatLngBounds calculateBounds(double lat, double lng, double radiusInMeters) {
const double earthRadius = 6378137.0; // Earth's radius in meters
// RxList: أي تغيير هنا سينعكس فوراً على الشاشة
RxList<Map<String, dynamic>> availableRides = <Map<String, dynamic>>[].obs;
double latDelta = (radiusInMeters / earthRadius) * (180 / pi);
double lngDelta =
(radiusInMeters / (earthRadius * cos(pi * lat / 180))) * (180 / pi);
DateTime? _lastFetchTime;
static const _cacheDuration = Duration(seconds: 5); // تقليل مدة الكاش قليلاً
double minLat = lat - latDelta;
double maxLat = lat + latDelta;
double minLng = lng - lngDelta;
double maxLng = lng + lngDelta;
// مشغل الصوت
final AudioPlayer _audioPlayer = AudioPlayer();
minLat = max(-90.0, minLat);
maxLat = min(90.0, maxLat);
@override
void onInit() {
super.onInit();
minLng = (minLng + 180) % 360 - 180;
maxLng = (maxLng + 180) % 360 - 180;
// 1. جلب القائمة الأولية من السيرفر (HTTP)
getRideAvailable(forceRefresh: true);
if (minLng > maxLng) {
double temp = minLng;
minLng = maxLng;
maxLng = temp;
// 2. تفعيل الاستماع المباشر للتحديثات (Socket)
_initSocketListeners();
}
@override
void onClose() {
// تنظيف الموارد عند الخروج
var socket = Get.find<LocationController>().socket;
socket?.off('market_new_ride');
socket?.off('ride_taken'); // تم توحيد الحدث لـ ride_taken
_audioPlayer.dispose();
super.onClose();
}
// ========================================================================
// 1. جلب الرحلات (HTTP Request) - الطريقة الجديدة (Lat/Lng)
// ========================================================================
Future<void> getRideAvailable({bool forceRefresh = false}) async {
// منع الطلبات المتكررة السريعة
if (!forceRefresh &&
_lastFetchTime != null &&
DateTime.now().difference(_lastFetchTime!) < _cacheDuration) {
return;
}
return LatLngBounds(
southwest: LatLng(minLat, minLng),
northeast: LatLng(maxLat, maxLng),
);
}
double calculateDistance(String startLocation) {
List<String> startLocationParts = startLocation.split(',');
double startLatitude = double.parse(startLocationParts[0]);
double startLongitude = double.parse(startLocationParts[1]);
double currentLatitude = Get.find<LocationController>().myLocation.latitude;
double currentLongitude =
Get.find<LocationController>().myLocation.longitude;
return Geolocator.distanceBetween(
currentLatitude,
currentLongitude,
startLatitude,
startLongitude,
);
}
// A helper function to safely show dialogs after the build cycle is complete.
void _showDialogAfterBuild(Widget dialog) {
// FIX 2: Use addPostFrameCallback to ensure dialogs are shown after the build process.
// This resolves the "visitChildElements() called during build" error.
WidgetsBinding.instance.addPostFrameCallback((_) {
Get.dialog(
dialog,
barrierDismissible: true,
transitionCurve: Curves.easeOutBack,
transitionDuration: const Duration(milliseconds: 200),
);
});
}
Future<void> getRideAvailable() async {
try {
isLoading = true;
update();
if (forceRefresh) {
isLoading = true;
update();
}
LatLngBounds bounds = calculateBounds(
Get.find<LocationController>().myLocation.latitude,
Get.find<LocationController>().myLocation.longitude,
4000);
// الحصول على موقع السائق الحالي
final location = Get.find<LocationController>().myLocation;
// 🔥 التعديل الجوهري: نرسل Lat/Lng فقط بدلاً من Bounds
var payload = {
'southwestLat': bounds.southwest.latitude.toString(),
'southwestLon': bounds.southwest.longitude.toString(),
'northeastLat': bounds.northeast.latitude.toString(),
'northeastLon': bounds.northeast.longitude.toString(),
'lat': location.latitude.toString(),
'lng': location.longitude.toString(),
'radius': '50', // نصف القطر بالكيلومتر (كما حددناه في السيرفر)
};
var res =
await CRUD().get(link: AppLink.getRideWaiting, payload: payload);
isLoading = false; // Request is complete, stop loading indicator.
isLoading = false;
_lastFetchTime = DateTime.now();
if (res != 'failure') {
final decodedResponse = jsonDecode(res);
// Check for valid response structure
if (decodedResponse is Map &&
decodedResponse.containsKey('message') &&
decodedResponse['message'] is List) {
rideAvailableMap = decodedResponse;
// If the list of rides is empty, show the "No Rides" dialog
if ((rideAvailableMap['message'] as List).isEmpty) {
_showDialogAfterBuild(_buildNoRidesDialog());
if (decodedResponse is Map && decodedResponse['status'] == 'success') {
final rides = decodedResponse['message'];
if (rides is List) {
// تحويل البيانات وتخزينها
availableRides.value = List<Map<String, dynamic>>.from(rides);
} else {
availableRides.clear();
}
} else {
// If response format is unexpected, treat as no rides and show dialog
rideAvailableMap = {'message': []};
_showDialogAfterBuild(_buildNoRidesDialog());
availableRides.clear();
}
update(); // Update the UI with new data (or empty list)
} else {
// This block now handles network/server errors correctly
HapticFeedback.lightImpact();
update(); // Update UI to turn off loader
// Show a proper error dialog instead of "No Rides"
_showDialogAfterBuild(
_buildErrorDialog("Failed to fetch rides. Please try again.".tr));
}
update(); // تحديث الواجهة
} catch (e) {
isLoading = false;
update();
// This catches other exceptions like JSON parsing errors
_showDialogAfterBuild(
_buildErrorDialog("An unexpected error occurred.".tr));
print("Error fetching rides: $e");
}
}
// Extracted dialogs into builder methods for cleanliness.
Widget _buildNoRidesDialog() {
return CupertinoAlertDialog(
title: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
CupertinoIcons.car,
size: 44,
color: CupertinoColors.systemGrey,
),
const SizedBox(height: 12),
Text(
"No Rides Available".tr,
style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
),
],
),
content: Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
"Please check back later for available rides.".tr,
style:
const TextStyle(fontSize: 13, color: CupertinoColors.systemGrey),
),
),
actions: [
CupertinoDialogAction(
onPressed: () {
Get.back(); // Close dialog
Get.back(); // Go back from AvailableRidesPage
},
child: Text('OK'.tr),
),
],
);
// ========================================================================
// 2. الاستماع للسوكيت (Real-time Updates) ⚡
// ========================================================================
void _initSocketListeners() {
var locationCtrl = Get.find<LocationController>();
var socket = locationCtrl.socket;
if (socket == null) {
print("⚠️ Socket is null in RideAvailableController");
return;
}
// A. عند وصول رحلة جديدة للسوق (market_new_ride)
socket.on('market_new_ride', (data) {
print("🔔 Socket: New Ride Market: $data");
if (data != null && data is Map) {
// فلترة: هل نوع السيارة يناسبني؟
if (_isCarTypeMatch(data['carType'])) {
// منع التكرار (إذا كانت الرحلة موجودة مسبقاً)
bool exists = availableRides
.any((r) => r['id'].toString() == data['id'].toString());
if (!exists) {
// إضافة الرحلة لأعلى القائمة
availableRides.insert(0, Map<String, dynamic>.from(data));
// تشغيل صوت تنبيه (Bling) 🎵
_playNotificationSound();
}
}
}
});
// B. عند أخذ رحلة من قبل سائق آخر (ride_taken)
// هذا الحدث يصل من acceptRide.php عبر السوكيت
socket.on('ride_taken', (data) {
print("🗑️ Socket: Ride Taken: $data");
if (data != null && data['ride_id'] != null) {
// حذف الرحلة من القائمة فوراً
availableRides.removeWhere(
(r) => r['id'].toString() == data['ride_id'].toString());
}
});
}
Widget _buildErrorDialog(String error) {
// You can log the error here for debugging.
// print("Error fetching rides: $error");
return CupertinoAlertDialog(
title: const Icon(
CupertinoIcons.exclamationmark_triangle_fill,
color: CupertinoColors.systemRed,
size: 44,
),
content: Text(
error, // Display the specific error message passed to the function
style: const TextStyle(fontSize: 14),
),
actions: [
CupertinoDialogAction(
onPressed: () {
Get.back(); // Close dialog
Get.back(); // Go back from AvailableRidesPage
},
child: Text('OK'.tr),
),
],
);
// دالة مساعدة للتحقق من نوع السيارة
bool _isCarTypeMatch(String? rideCarType) {
if (rideCarType == null) return false;
String myDriverType = box.read(BoxName.carTypeOfDriver).toString();
// منطق التوزيع الهرمي
switch (myDriverType) {
case 'Comfort':
return ['Speed', 'Comfort', 'Fixed Price'].contains(rideCarType);
case 'Speed':
case 'Scooter':
case 'Awfar Car':
return rideCarType == myDriverType;
case 'Lady':
return ['Comfort', 'Speed', 'Lady'].contains(rideCarType);
default:
return true; // احتياطياً
}
}
@override
void onInit() {
super.onInit();
getRideAvailable();
// تشغيل صوت التنبيه
Future<void> _playNotificationSound() async {
try {
// تأكد من وجود الملف في assets وإضافته في pubspec.yaml
await _audioPlayer.setAsset('assets/audio/notification.mp3');
_audioPlayer.play();
} catch (e) {
// تجاهل الخطأ إذا لم يوجد ملف صوت
}
}
}