MVP I
This commit is contained in:
@@ -15,10 +15,36 @@ class BrushingPrefs {
|
||||
|
||||
static String _key(String base, String scopeId) => '${base}_$scopeId';
|
||||
|
||||
static DateTime _currentWeekMonday() {
|
||||
final now = DateTime.now();
|
||||
return DateTime(
|
||||
now.year,
|
||||
now.month,
|
||||
now.day,
|
||||
).subtract(Duration(days: now.weekday - 1));
|
||||
}
|
||||
|
||||
/// Lê as escovagens guardadas e, de caminho, descarta (apaga do
|
||||
/// armazenamento) qualquer registo de semanas anteriores — o contador
|
||||
/// semanal deve zerar a cada nova semana, não só na exibição mas também
|
||||
/// nos dados guardados, para não crescer para sempre.
|
||||
static Future<List<DateTime>> _getEntries(String scopeId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getStringList(_key(_kDatesKey, scopeId)) ?? const [];
|
||||
return raw.map(DateTime.tryParse).whereType<DateTime>().toList();
|
||||
final key = _key(_kDatesKey, scopeId);
|
||||
final raw = prefs.getStringList(key) ?? const [];
|
||||
final entries = raw.map(DateTime.tryParse).whereType<DateTime>().toList();
|
||||
|
||||
final monday = _currentWeekMonday();
|
||||
final kept = entries
|
||||
.where((d) => !DateTime(d.year, d.month, d.day).isBefore(monday))
|
||||
.toList();
|
||||
if (kept.length != entries.length) {
|
||||
await prefs.setStringList(
|
||||
key,
|
||||
kept.map((d) => d.toIso8601String()).toList(),
|
||||
);
|
||||
}
|
||||
return kept;
|
||||
}
|
||||
|
||||
static Future<int> getWeeklyGoal(String scopeId) async {
|
||||
@@ -51,11 +77,15 @@ class BrushingPrefs {
|
||||
/// o utilizador).
|
||||
static Future<void> logToday(String scopeId) async {
|
||||
if (!(await canLogMore(scopeId))) return;
|
||||
// Usa as entradas já podadas (só da semana atual) para não ressuscitar
|
||||
// registos de semanas anteriores ao gravar de volta.
|
||||
final entries = await _getEntries(scopeId);
|
||||
entries.add(DateTime.now());
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final key = _key(_kDatesKey, scopeId);
|
||||
final list = prefs.getStringList(key) ?? <String>[];
|
||||
list.add(DateTime.now().toIso8601String());
|
||||
await prefs.setStringList(key, list);
|
||||
await prefs.setStringList(
|
||||
_key(_kDatesKey, scopeId),
|
||||
entries.map((d) => d.toIso8601String()).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Já atingiu o limite diário de [maxPerDay] escovagens hoje?
|
||||
@@ -64,21 +94,9 @@ class BrushingPrefs {
|
||||
}
|
||||
|
||||
/// Conta quantas escovagens foram registadas na semana atual
|
||||
/// (segunda-feira a domingo).
|
||||
/// (segunda-feira a domingo). [_getEntries] já descarta semanas
|
||||
/// anteriores, por isso todas as entradas devolvidas já são desta semana.
|
||||
static Future<int> getWeekCount(String scopeId) async {
|
||||
final entries = await _getEntries(scopeId);
|
||||
|
||||
final now = DateTime.now();
|
||||
final monday = DateTime(
|
||||
now.year,
|
||||
now.month,
|
||||
now.day,
|
||||
).subtract(Duration(days: now.weekday - 1));
|
||||
|
||||
return entries
|
||||
.where(
|
||||
(d) => !DateTime(d.year, d.month, d.day).isBefore(monday),
|
||||
)
|
||||
.length;
|
||||
return (await _getEntries(scopeId)).length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user