CKT 1.0.2 | otorrinolaringologista
This commit is contained in:
61
lib/quiz/quiz_progress_prefs.dart
Normal file
61
lib/quiz/quiz_progress_prefs.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'quiz_question_screen.dart' show QuizScore;
|
||||
|
||||
/// Progresso de um quiz por terminar: em que pergunta ficou e a pontuação
|
||||
/// acumulada até aí.
|
||||
class QuizProgressData {
|
||||
const QuizProgressData({required this.questionIndex, required this.score});
|
||||
|
||||
/// Número (1-based) da pergunta onde retomar.
|
||||
final int questionIndex;
|
||||
final QuizScore score;
|
||||
}
|
||||
|
||||
/// Guarda o progresso de um quiz abandonado a meio (ex.: "Voltar para
|
||||
/// homepage"), para oferecer "Continuar de onde parou" da próxima vez que o
|
||||
/// mesmo utilizador iniciar o quiz para a mesma criança. Só existe uma
|
||||
/// ranhura (não uma por criança): ao guardar progresso para uma criança
|
||||
/// diferente, substitui o anterior — como só se mostra a opção de continuar
|
||||
/// quando o [scopeId] guardado corresponde exatamente à criança escolhida,
|
||||
/// isto já cumpre "só se for a mesma criança" sem precisar de armazenamento
|
||||
/// por criança.
|
||||
class QuizProgressPrefs {
|
||||
static const String _kScopeKey = 'quiz_progress_scope';
|
||||
static const String _kIndexKey = 'quiz_progress_index';
|
||||
static const String _kSignsKey = 'quiz_progress_signs';
|
||||
static const String _kFactorsKey = 'quiz_progress_factors';
|
||||
|
||||
static Future<void> saveProgress({
|
||||
required String scopeId,
|
||||
required int questionIndex,
|
||||
required QuizScore score,
|
||||
}) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_kScopeKey, scopeId);
|
||||
await prefs.setInt(_kIndexKey, questionIndex);
|
||||
await prefs.setInt(_kSignsKey, score.signs);
|
||||
await prefs.setInt(_kFactorsKey, score.factors);
|
||||
}
|
||||
|
||||
static Future<QuizProgressData?> getProgress(String scopeId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (prefs.getString(_kScopeKey) != scopeId) return null;
|
||||
final index = prefs.getInt(_kIndexKey);
|
||||
final signs = prefs.getInt(_kSignsKey);
|
||||
final factors = prefs.getInt(_kFactorsKey);
|
||||
if (index == null || signs == null || factors == null) return null;
|
||||
return QuizProgressData(
|
||||
questionIndex: index,
|
||||
score: QuizScore(signs: signs, factors: factors),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> clearProgress() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_kScopeKey);
|
||||
await prefs.remove(_kIndexKey);
|
||||
await prefs.remove(_kSignsKey);
|
||||
await prefs.remove(_kFactorsKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user