Files
doormile_milderapp/lib/controllers/summary_controller.dart
2026-08-11 13:16:33 +05:30

59 lines
1.9 KiB
Dart

import 'package:get/get.dart';
import 'package:miler/Models/summary/pickupstats.dart';
import 'package:miler/providers/summary/summary.dart';
import 'package:flutter/foundation.dart';
class SummaryController extends GetxController {
final SummaryProvider _provider = SummaryProvider();
// Observables
var today = 0.obs;
var week = 0.obs;
var month = 0.obs;
var total = 0.obs;
var cancelled = 0.obs;
var isLoading = false.obs;
// Fetch stats and update values
Future<void> fetchSummaryStats(int userId) async {
try {
isLoading.value = true;
final PickupStats? stats = await _provider.fetchSummaryStats(userId);
// A record of all zeros is what a freshly-seeded account returns, and it
// is indistinguishable from no record at all — treat both as empty so
// the demo figures can fill in.
if (stats != null && (stats.total > 0 || stats.month > 0)) {
today.value = stats.today;
week.value = stats.week;
month.value = stats.month;
total.value = stats.total;
cancelled.value = stats.cancelled;
} else {
_applyZero();
}
} catch (e) {
debugPrint('Controller error: $e');
_applyZero();
} finally {
isLoading.value = false;
}
}
/// Nothing came back — show zeros.
///
/// This used to fill the tab with seeded demo figures in debug builds. That
/// was defensible while the endpoint was unfinished and indefensible now that
/// it is live: the one thing a pay screen must never do is invent a number,
/// and a debug-only invention is still the number the person testing the app
/// reports back as real. An empty Earnings tab is information — it says the
/// fetch returned nothing — and the honest zero is what makes a broken
/// endpoint visible instead of plausible.
void _applyZero() {
today.value = 0;
week.value = 0;
month.value = 0;
total.value = 0;
cancelled.value = 0;
}
}