Phase 2 gaps completed: - HOME-001: Hero slider pause-on-touch (GestureDetector wraps PageView) - HOME-003: Calendar bottom sheet with TableCalendar (replaces custom dialog) - AUTH-004: District dropdown on signup (14 Kerala districts) - EVT-003: Mobile sticky "Book Now" bar + desktop CTA wired to CheckoutScreen - ACH-001: Real achievements from dashboard API with fallback defaults - GAM-002: 3-card EP row (Lifetime EP / Liquid EP / Reward Points) - GAM-005: Horizontal tier roadmap Bronze→Silver→Gold→Platinum→Diamond - CTR-001: Submission status chips (PENDING/APPROVED/REJECTED) - CTR-002: +EP badge on approved submissions - PROF-003: Gamification cards on profile screen with Consumer<GamificationProvider> - UX-001: Shimmer skeleton loaders (shimmer ^3.0.0) replacing CircularProgressIndicator Already complete (verified, no changes needed): - HOME-002: Category shelves already built in _buildTypeSection() - LDR-002: Podium visualization already built (_buildPodium / _buildDesktopPodium) - BOOK-004: UPI handled natively by Razorpay SDK Deferred: LOC-001/002 (needs Django haversine endpoint) Skipped: AUTH-002 (OTP needs SMS provider decision) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
157 lines
5.4 KiB
Dart
157 lines
5.4 KiB
Dart
// lib/features/gamification/providers/gamification_provider.dart
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import '../../../core/utils/error_utils.dart';
|
|
import '../models/gamification_models.dart';
|
|
import '../services/gamification_service.dart';
|
|
|
|
class GamificationProvider extends ChangeNotifier {
|
|
final GamificationService _service = GamificationService();
|
|
|
|
// State
|
|
UserGamificationProfile? profile;
|
|
List<LeaderboardEntry> leaderboard = [];
|
|
List<ShopItem> shopItems = [];
|
|
List<AchievementBadge> achievements = [];
|
|
List<SubmissionModel> submissions = [];
|
|
CurrentUserStats? currentUserStats;
|
|
int totalParticipants = 0;
|
|
|
|
// Leaderboard filters — matches web version
|
|
String leaderboardDistrict = 'Overall Kerala';
|
|
String leaderboardTimePeriod = 'all_time'; // 'all_time' | 'this_month'
|
|
|
|
bool isLoading = false;
|
|
String? error;
|
|
|
|
// TTL guard — prevents redundant API calls from multiple screens
|
|
DateTime? _lastLoadTime;
|
|
static const _loadTtl = Duration(minutes: 2);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Load everything at once (called when ContributeScreen or ProfileScreen mounts)
|
|
// ---------------------------------------------------------------------------
|
|
Future<void> loadAll({bool force = false}) async {
|
|
// Skip if recently loaded (within 2 minutes) unless forced
|
|
if (!force && _lastLoadTime != null && DateTime.now().difference(_lastLoadTime!) < _loadTtl) {
|
|
return;
|
|
}
|
|
|
|
isLoading = true;
|
|
error = null;
|
|
notifyListeners();
|
|
|
|
try {
|
|
final results = await Future.wait([
|
|
_service.getDashboard(),
|
|
_service.getLeaderboard(district: leaderboardDistrict, timePeriod: leaderboardTimePeriod),
|
|
_service.getShopItems(),
|
|
_service.getAchievements(),
|
|
]);
|
|
|
|
final dashboard = results[0] as DashboardResponse;
|
|
profile = dashboard.profile;
|
|
submissions = dashboard.submissions;
|
|
|
|
final lbResponse = results[1] as LeaderboardResponse;
|
|
leaderboard = lbResponse.entries;
|
|
currentUserStats = lbResponse.currentUser;
|
|
totalParticipants = lbResponse.totalParticipants;
|
|
|
|
shopItems = results[2] as List<ShopItem>;
|
|
|
|
// Prefer achievements from dashboard API; fall back to getAchievements()
|
|
final dashAchievements = dashboard.achievements;
|
|
final fetchedAchievements = results[3] as List<AchievementBadge>;
|
|
achievements = dashAchievements.isNotEmpty ? dashAchievements : fetchedAchievements;
|
|
|
|
_lastLoadTime = DateTime.now();
|
|
} catch (e) {
|
|
error = userFriendlyError(e);
|
|
} finally {
|
|
isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Change district filter
|
|
// ---------------------------------------------------------------------------
|
|
Future<void> setDistrict(String district) async {
|
|
if (leaderboardDistrict == district) return;
|
|
leaderboardDistrict = district;
|
|
notifyListeners();
|
|
try {
|
|
final response = await _service.getLeaderboard(district: district, timePeriod: leaderboardTimePeriod);
|
|
leaderboard = response.entries;
|
|
currentUserStats = response.currentUser;
|
|
totalParticipants = response.totalParticipants;
|
|
} catch (e) {
|
|
error = userFriendlyError(e);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Change time period filter
|
|
// ---------------------------------------------------------------------------
|
|
Future<void> setTimePeriod(String period) async {
|
|
if (leaderboardTimePeriod == period) return;
|
|
leaderboardTimePeriod = period;
|
|
notifyListeners();
|
|
try {
|
|
final response = await _service.getLeaderboard(district: leaderboardDistrict, timePeriod: period);
|
|
leaderboard = response.entries;
|
|
currentUserStats = response.currentUser;
|
|
totalParticipants = response.totalParticipants;
|
|
} catch (e) {
|
|
error = userFriendlyError(e);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Redeem a shop item — deducts RP locally optimistically, returns voucher code
|
|
// ---------------------------------------------------------------------------
|
|
Future<String> redeemItem(String itemId) async {
|
|
final item = shopItems.firstWhere((s) => s.id == itemId);
|
|
|
|
// Optimistically deduct RP
|
|
if (profile != null) {
|
|
profile = UserGamificationProfile(
|
|
userId: profile!.userId,
|
|
lifetimeEp: profile!.lifetimeEp,
|
|
currentEp: profile!.currentEp,
|
|
currentRp: profile!.currentRp - item.rpCost,
|
|
tier: profile!.tier,
|
|
);
|
|
notifyListeners();
|
|
}
|
|
|
|
try {
|
|
final record = await _service.redeemItem(itemId);
|
|
return record.voucherCode;
|
|
} catch (e) {
|
|
// Rollback on failure
|
|
if (profile != null) {
|
|
profile = UserGamificationProfile(
|
|
userId: profile!.userId,
|
|
lifetimeEp: profile!.lifetimeEp,
|
|
currentEp: profile!.currentEp,
|
|
currentRp: profile!.currentRp + item.rpCost,
|
|
tier: profile!.tier,
|
|
);
|
|
notifyListeners();
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Submit a contribution
|
|
// ---------------------------------------------------------------------------
|
|
Future<void> submitContribution(Map<String, dynamic> data) async {
|
|
await _service.submitContribution(data);
|
|
}
|
|
}
|