import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; class NotificationLogEntry { final int id; final String kind; // 'daily' | 'snooze' final String type; // 'single' | 'group' final int whenEpochMs; // exact scheduled time (epoch ms) final int createdAtEpochMs; // when we created the schedule (epoch ms) final String title; final String payload; final int? singleId; // supplement id for single final String? timeKey; // HH:mm for group const NotificationLogEntry({ required this.id, required this.kind, required this.type, required this.whenEpochMs, required this.createdAtEpochMs, required this.title, required this.payload, this.singleId, this.timeKey, }); Map toJson() => { 'id': id, 'kind': kind, 'type': type, 'when': whenEpochMs, 'createdAt': createdAtEpochMs, 'title': title, 'payload': payload, 'singleId': singleId, 'timeKey': timeKey, }; static NotificationLogEntry fromJson(Map map) { return NotificationLogEntry( id: map['id'] is int ? map['id'] as int : int.tryParse('${map['id']}') ?? 0, kind: map['kind'] ?? 'unknown', type: map['type'] ?? 'unknown', whenEpochMs: map['when'] is int ? map['when'] as int : int.tryParse('${map['when']}') ?? 0, createdAtEpochMs: map['createdAt'] is int ? map['createdAt'] as int : int.tryParse('${map['createdAt']}') ?? 0, title: map['title'] ?? '', payload: map['payload'] ?? '', singleId: map['singleId'], timeKey: map['timeKey'], ); } } class NotificationDebugStore { NotificationDebugStore._internal(); static final NotificationDebugStore instance = NotificationDebugStore._internal(); static const String _prefsKey = 'notification_log'; static const int _maxEntries = 200; Future> getAll() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(_prefsKey); if (raw == null || raw.isEmpty) return []; try { final list = jsonDecode(raw) as List; return list .map((e) => NotificationLogEntry.fromJson(e as Map)) .toList(); } catch (_) { return []; } } Future add(NotificationLogEntry entry) async { final prefs = await SharedPreferences.getInstance(); final current = await getAll(); current.add(entry); // Cap size final trimmed = current.length > _maxEntries ? current.sublist(current.length - _maxEntries) : current; final serialized = jsonEncode(trimmed.map((e) => e.toJson()).toList()); await prefs.setString(_prefsKey, serialized); } Future clear() async { final prefs = await SharedPreferences.getInstance(); await prefs.remove(_prefsKey); } }