Atualização de gauge
This commit is contained in:
@@ -1,9 +1,27 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Resultado do quiz, sempre com os dois indicadores juntos — nunca faz
|
||||
/// sentido guardar/ler um sem o outro.
|
||||
class QuizResultData {
|
||||
const QuizResultData({
|
||||
required this.signs,
|
||||
required this.signsMax,
|
||||
required this.factors,
|
||||
required this.factorsMax,
|
||||
});
|
||||
|
||||
final int signs;
|
||||
final int signsMax;
|
||||
final int factors;
|
||||
final int factorsMax;
|
||||
}
|
||||
|
||||
class QuizPrefs {
|
||||
static const String _kSeenQuizKey = 'seen_oral_quiz_v1';
|
||||
static const String _kLastScoreKey = 'last_oral_quiz_score_v1';
|
||||
static const String _kLastMaxScoreKey = 'last_oral_quiz_max_score_v1';
|
||||
static const String _kSignsKey = 'last_oral_quiz_signs_v2';
|
||||
static const String _kSignsMaxKey = 'last_oral_quiz_signs_max_v2';
|
||||
static const String _kFactorsKey = 'last_oral_quiz_factors_v2';
|
||||
static const String _kFactorsMaxKey = 'last_oral_quiz_factors_max_v2';
|
||||
|
||||
static String _scopeKey(String base, String? scopeId) {
|
||||
final id = (scopeId ?? '').trim();
|
||||
@@ -21,47 +39,56 @@ class QuizPrefs {
|
||||
await prefs.setBool(_kSeenQuizKey, true);
|
||||
}
|
||||
|
||||
static Future<void> saveLastResult({required int score, required int maxScore}) async {
|
||||
static Future<void> saveLastResult(QuizResultData result) async {
|
||||
await saveLastResultForScope(scopeId: null, result: result);
|
||||
}
|
||||
|
||||
static Future<void> saveLastResultForUser({
|
||||
required String userId,
|
||||
required QuizResultData result,
|
||||
}) async {
|
||||
await saveLastResultForScope(scopeId: userId, result: result);
|
||||
}
|
||||
|
||||
static Future<void> saveLastResultForScope({
|
||||
required String? scopeId,
|
||||
required QuizResultData result,
|
||||
}) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_kLastScoreKey, score);
|
||||
await prefs.setInt(_kLastMaxScoreKey, maxScore);
|
||||
await prefs.setInt(_scopeKey(_kSignsKey, scopeId), result.signs);
|
||||
await prefs.setInt(_scopeKey(_kSignsMaxKey, scopeId), result.signsMax);
|
||||
await prefs.setInt(_scopeKey(_kFactorsKey, scopeId), result.factors);
|
||||
await prefs.setInt(
|
||||
_scopeKey(_kFactorsMaxKey, scopeId),
|
||||
result.factorsMax,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> saveLastResultForUser({required String userId, required int score, required int maxScore}) async {
|
||||
await saveLastResultForScope(scopeId: userId, score: score, maxScore: maxScore);
|
||||
}
|
||||
static Future<QuizResultData?> getLastResult() =>
|
||||
getLastResultForScope(null);
|
||||
|
||||
static Future<void> saveLastResultForScope({required String scopeId, required int score, required int maxScore}) async {
|
||||
static Future<QuizResultData?> getLastResultForUser(String userId) =>
|
||||
getLastResultForScope(userId);
|
||||
|
||||
static Future<QuizResultData?> getLastResultForScope(
|
||||
String? scopeId,
|
||||
) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_scopeKey(_kLastScoreKey, scopeId), score);
|
||||
await prefs.setInt(_scopeKey(_kLastMaxScoreKey, scopeId), maxScore);
|
||||
}
|
||||
|
||||
static Future<int?> getLastScore() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_kLastScoreKey);
|
||||
}
|
||||
|
||||
static Future<int?> getLastScoreForUser(String userId) async {
|
||||
return getLastScoreForScope(userId);
|
||||
}
|
||||
|
||||
static Future<int?> getLastScoreForScope(String scopeId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_scopeKey(_kLastScoreKey, scopeId));
|
||||
}
|
||||
|
||||
static Future<int?> getLastMaxScore() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_kLastMaxScoreKey);
|
||||
}
|
||||
|
||||
static Future<int?> getLastMaxScoreForUser(String userId) async {
|
||||
return getLastMaxScoreForScope(userId);
|
||||
}
|
||||
|
||||
static Future<int?> getLastMaxScoreForScope(String scopeId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_scopeKey(_kLastMaxScoreKey, scopeId));
|
||||
final signs = prefs.getInt(_scopeKey(_kSignsKey, scopeId));
|
||||
final signsMax = prefs.getInt(_scopeKey(_kSignsMaxKey, scopeId));
|
||||
final factors = prefs.getInt(_scopeKey(_kFactorsKey, scopeId));
|
||||
final factorsMax = prefs.getInt(_scopeKey(_kFactorsMaxKey, scopeId));
|
||||
if (signs == null ||
|
||||
signsMax == null ||
|
||||
factors == null ||
|
||||
factorsMax == null) {
|
||||
return null;
|
||||
}
|
||||
return QuizResultData(
|
||||
signs: signs,
|
||||
signsMax: signsMax,
|
||||
factors: factors,
|
||||
factorsMax: factorsMax,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user