import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import '../about/internal/application/DeploymentRecord.dart'; /// Persists the deployment history so successes/failures can be tracked. class DeploymentStore { static const _key = 'peach.deployments.v1'; /// The key used before the app was renamed; migrated forward on first read. static const _legacyKey = 'jarpilot.deployments.v1'; static const _max = 300; Future> load() async { final prefs = await SharedPreferences.getInstance(); var raw = prefs.getString(_key); if (raw == null || raw.isEmpty) { raw = prefs.getString(_legacyKey); if (raw != null && raw.isNotEmpty) { await prefs.setString(_key, raw); await prefs.remove(_legacyKey); } } if (raw == null || raw.isEmpty) return []; final list = jsonDecode(raw) as List; final records = list .map((e) => DeploymentRecord.fromJson(e as Map)) .toList(); records.sort((a, b) => b.time.compareTo(a.time)); return records; } Future> add(DeploymentRecord record) async { final records = await load(); records.insert(0, record); if (records.length > _max) records.removeRange(_max, records.length); final prefs = await SharedPreferences.getInstance(); await prefs.setString( _key, jsonEncode(records.map((r) => r.toJson()).toList()), ); return records; } Future clear() async { final prefs = await SharedPreferences.getInstance(); await prefs.remove(_key); } }