Files
rmtPocketWatcher/flutter_app/lib/services/storage_service.dart
2025-12-14 21:53:46 -05:00

55 lines
1.7 KiB
Dart

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/price_data.dart';
class StorageService {
static const String _alertsKey = 'price_alerts';
static const String _customAuecKey = 'custom_auec_amount';
Future<List<PriceAlert>> getAlerts() async {
final prefs = await SharedPreferences.getInstance();
final alertsJson = prefs.getString(_alertsKey);
if (alertsJson == null) return [];
final List<dynamic> decoded = jsonDecode(alertsJson);
return decoded.map((e) => PriceAlert.fromJson(e as Map<String, dynamic>)).toList();
}
Future<void> saveAlerts(List<PriceAlert> alerts) async {
final prefs = await SharedPreferences.getInstance();
final alertsJson = jsonEncode(alerts.map((e) => e.toJson()).toList());
await prefs.setString(_alertsKey, alertsJson);
}
Future<void> addAlert(PriceAlert alert) async {
final alerts = await getAlerts();
alerts.add(alert);
await saveAlerts(alerts);
}
Future<void> updateAlert(PriceAlert alert) async {
final alerts = await getAlerts();
final index = alerts.indexWhere((a) => a.id == alert.id);
if (index != -1) {
alerts[index] = alert;
await saveAlerts(alerts);
}
}
Future<void> deleteAlert(String id) async {
final alerts = await getAlerts();
alerts.removeWhere((a) => a.id == id);
await saveAlerts(alerts);
}
Future<double?> getCustomAuecAmount() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getDouble(_customAuecKey);
}
Future<void> setCustomAuecAmount(double amount) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setDouble(_customAuecKey, amount);
}
}