Update: 2026-06-15 01:37:40
This commit is contained in:
@@ -54,6 +54,7 @@ class StatisticsController extends GetxController {
|
||||
);
|
||||
|
||||
if (res != null && res != 'failure') {
|
||||
box.write('stats_weekly_cache', res);
|
||||
var data = jsonDecode(res);
|
||||
if (data['message'] is List) {
|
||||
weeklyStats = (data['message'] as List)
|
||||
@@ -83,6 +84,7 @@ class StatisticsController extends GetxController {
|
||||
payload: {'driverID': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (earningsRes != null && earningsRes != 'failure') {
|
||||
box.write('stats_monthly_earnings_cache', earningsRes);
|
||||
var data = jsonDecode(earningsRes);
|
||||
if (data['message'] is List) {
|
||||
monthlyEarnings = (data['message'] as List)
|
||||
@@ -107,6 +109,7 @@ class StatisticsController extends GetxController {
|
||||
payload: {'driver_id': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (ridesRes != null && ridesRes != 'failure') {
|
||||
box.write('stats_monthly_rides_cache', ridesRes);
|
||||
var data = jsonDecode(ridesRes);
|
||||
if (data['message'] is List) {
|
||||
monthlyRides = (data['message'] as List)
|
||||
@@ -122,6 +125,7 @@ class StatisticsController extends GetxController {
|
||||
payload: {'driver_id': box.read(BoxName.driverID).toString()},
|
||||
);
|
||||
if (durationRes != null && durationRes != 'failure') {
|
||||
box.write('stats_monthly_duration_cache', durationRes);
|
||||
var data = jsonDecode(durationRes);
|
||||
if (data['message'] is List) {
|
||||
monthlyDuration = (data['message'] as List)
|
||||
@@ -160,16 +164,93 @@ class StatisticsController extends GetxController {
|
||||
|
||||
bool _isFetching = false;
|
||||
|
||||
Future<void> reloadData() async {
|
||||
Future<void> reloadData({bool force = false}) async {
|
||||
if (_isFetching) return;
|
||||
_isFetching = true;
|
||||
|
||||
// التحقق من التخزين المؤقت (3 ساعات)
|
||||
String? lastFetchStr = box.read('stats_last_fetch');
|
||||
if (!force && lastFetchStr != null) {
|
||||
try {
|
||||
DateTime lastFetch = DateTime.parse(lastFetchStr);
|
||||
if (DateTime.now().difference(lastFetch).inHours < 3) {
|
||||
debugPrint('ℹ️ [Statistics] Loading cached statistics (last fetch: $lastFetchStr)');
|
||||
|
||||
// تحميل الكاش الأسبوعي
|
||||
String? weeklyCache = box.read('stats_weekly_cache');
|
||||
if (weeklyCache != null) {
|
||||
var data = jsonDecode(weeklyCache);
|
||||
if (data['message'] is List) {
|
||||
weeklyStats = (data['message'] as List)
|
||||
.map((e) => DayStat.fromJson(e))
|
||||
.toList();
|
||||
weeklyEarnings = weeklyStats.fold(0, (s, d) => s + d.earnings);
|
||||
weeklyTrips = weeklyStats.fold(0, (s, d) => s + d.trips);
|
||||
weeklyHours = weeklyStats.fold(0, (s, d) => s + d.hours);
|
||||
}
|
||||
}
|
||||
|
||||
// تحميل الكاش الشهري (الأرباح)
|
||||
String? earningsCache = box.read('stats_monthly_earnings_cache');
|
||||
if (earningsCache != null) {
|
||||
var data = jsonDecode(earningsCache);
|
||||
if (data['message'] is List) {
|
||||
monthlyEarnings = (data['message'] as List)
|
||||
.map((e) => MonthlyPriceDriverModel.fromJson(e))
|
||||
.toList();
|
||||
monthlyTotalEarnings =
|
||||
monthlyEarnings.fold(0, (s, d) => s + d.pricePerDay);
|
||||
if (monthlyEarnings.isNotEmpty) {
|
||||
var best = monthlyEarnings
|
||||
.reduce((a, b) => a.pricePerDay > b.pricePerDay ? a : b);
|
||||
bestDay = best.day.toString();
|
||||
bestDayEarnings = best.pricePerDay;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// تحميل الكاش الشهري (الرحلات)
|
||||
String? ridesCache = box.read('stats_monthly_rides_cache');
|
||||
if (ridesCache != null) {
|
||||
var data = jsonDecode(ridesCache);
|
||||
if (data['message'] is List) {
|
||||
monthlyRides = (data['message'] as List)
|
||||
.map((e) => MonthlyRideModel.fromJson(e))
|
||||
.toList();
|
||||
monthlyTotalTrips = monthlyRides.fold(0, (s, d) => s + d.countRide);
|
||||
}
|
||||
}
|
||||
|
||||
// تحميل الكاش الشهري (المدد)
|
||||
String? durationCache = box.read('stats_monthly_duration_cache');
|
||||
if (durationCache != null) {
|
||||
var data = jsonDecode(durationCache);
|
||||
if (data['message'] is List) {
|
||||
monthlyDuration = (data['message'] as List)
|
||||
.map((e) => MonthlyDataModel.fromJson(e))
|
||||
.toList();
|
||||
monthlyTotalHours =
|
||||
monthlyDuration.fold(0, (s, d) => s + d.totalDuration.toDouble());
|
||||
}
|
||||
}
|
||||
|
||||
_isFetching = false;
|
||||
update();
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [Statistics] Failed to load cached statistics: $e');
|
||||
}
|
||||
}
|
||||
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
try {
|
||||
debugPrint('📊 [Statistics] Reloading data...');
|
||||
debugPrint('📊 [Statistics] Reloading data from server...');
|
||||
await fetchWeeklyData();
|
||||
await fetchMonthlyData();
|
||||
box.write('stats_last_fetch', DateTime.now().toIso8601String());
|
||||
debugPrint('📊 [Statistics] Data reload complete.');
|
||||
} catch (e) {
|
||||
debugPrint('❌ [Statistics] Error reloading data: $e');
|
||||
|
||||
Reference in New Issue
Block a user