Files
Onsol-GO/lib/core/achievement_manager.dart
2026-04-23 23:58:59 -05:00

34 lines
1.2 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:onsolgo/core/constants.dart';
class AchievementManager {
static Future<void> unlock(String uid, String achId) async {
final ref = FirebaseFirestore.instance.collection('users').doc(uid).collection('achievements').doc(achId);
final doc = await ref.get();
if (!doc.exists) {
await ref.set({'unlockedAt': FieldValue.serverTimestamp()});
}
}
static Future<void> incrementStat(String uid, String field, {int amount = 1}) async {
final userRef = FirebaseFirestore.instance.collection('users').doc(uid);
await userRef.update({field: FieldValue.increment(amount)});
final snap = await userRef.get();
final d = snap.data() as Map<String, dynamic>;
// Threshold Checks
if (field == 'pagesRead') {
int p = safeInt(d['pagesRead']);
if (p >= 10) unlock(uid, "page_turner_1");
if (p >= 50) unlock(uid, "page_turner_2");
if (p >= 200) unlock(uid, "page_turner_3");
}
if (field == 'chaptersRead') {
int c = safeInt(d['chaptersRead']);
if (c >= 1) unlock(uid, "chapter_complete_1");
if (c >= 10) unlock(uid, "chapter_complete_2");
if (c >= 50) unlock(uid, "chapter_complete_3");
}
}
}