DietaryGuard/lib/controllers/alert.dart

39 lines
1.0 KiB
Dart
Raw Permalink Normal View History

2024-08-15 03:53:01 +00:00
import 'dart:convert';
import 'package:dietary_guard/models/alert_configuration.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AlertController extends GetxController {
List<AlertConfiguration> configuration = List.empty();
late final SharedPreferences _prefs;
@override
void onInit() async {
_prefs = await SharedPreferences.getInstance();
loadAlertConfiguration();
super.onInit();
}
void loadAlertConfiguration() {
final raw = _prefs.getString("alert_configuration");
if (raw == null) return;
configuration = List<AlertConfiguration>.from(jsonDecode(raw).map(
(x) => AlertConfiguration.fromJson(x),
));
}
Future<void> setAlertConfiguration(List<AlertConfiguration> value) async {
await _prefs.setString(
"alert_configuration",
jsonEncode(value.map((x) => x.toJson()).toList()),
);
loadAlertConfiguration();
}
void clearAlertConfiguration() {
_prefs.remove("alert_configuration");
}
}