application.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import 'dart:convert';
  2. import 'package:fis_common/logger/logger.dart';
  3. import 'package:vitalapp/architecture/storage/text_storage.dart';
  4. import 'interfaces/application.dart';
  5. class ApplicationManager implements IApplicationManager {
  6. final _storage = TextStorage(fileName: "local.properties");
  7. Map<String, dynamic>? _cache;
  8. @override
  9. Future<Map<String, dynamic>> getProperties() async {
  10. if (_cache == null) {
  11. try {
  12. final jsonText = await _storage.read();
  13. if (jsonText != null) {
  14. _cache = jsonDecode(jsonText);
  15. } else {
  16. _cache = {};
  17. }
  18. } catch (e) {
  19. logger.e("ApplicationManager getProperties error", e);
  20. }
  21. }
  22. return _cache!;
  23. }
  24. @override
  25. Future<dynamic> getProperty(String key) async {
  26. final map = await getProperties();
  27. return map[key];
  28. }
  29. @override
  30. Future<void> setProperty(String key, dynamic value) async {
  31. try {
  32. final map = await getProperties();
  33. map[key] = value;
  34. final jsonText = jsonEncode(map);
  35. await _storage.save(jsonText);
  36. } catch (e) {
  37. logger.e("ApplicationManager setProperty-$key error", e);
  38. }
  39. }
  40. @override
  41. Future<void> setProperties(Map<String, dynamic> data) async {
  42. try {
  43. final map = await getProperties();
  44. for (var key in data.keys) {
  45. map[key] = data[key];
  46. }
  47. final jsonText = jsonEncode(map);
  48. await _storage.save(jsonText);
  49. } catch (e) {
  50. logger.e("ApplicationManager setProperties error", e);
  51. }
  52. }
  53. }