✨ Edit alert configuration
This commit is contained in:
38
lib/controllers/alert.dart
Normal file
38
lib/controllers/alert.dart
Normal file
@ -0,0 +1,38 @@
|
||||
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");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user