Flutter App
This commit is contained in:
54
flutter_app/lib/services/storage_service.dart
Normal file
54
flutter_app/lib/services/storage_service.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user