Files
PeachDeployments/lib/Peach/memory/DeploymentStore.dart
alvocool 38881d1fe1 Add app source and tests
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-11 10:15:30 +03:00

50 lines
1.6 KiB
Dart

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<List<DeploymentRecord>> 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<dynamic>;
final records = list
.map((e) => DeploymentRecord.fromJson(e as Map<String, dynamic>))
.toList();
records.sort((a, b) => b.time.compareTo(a.time));
return records;
}
Future<List<DeploymentRecord>> 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<void> clear() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_key);
}
}