model.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'package:flutter/material.dart';
  2. import './common.dart';
  3. import './manager.dart';
  4. final Map<String, LocationModel> _locationModels = {};
  5. final Map<String, OptionModel> _optionModels = {};
  6. class OptionModel with ChangeNotifier {
  7. String? v;
  8. String? get value => v;
  9. set value(String? v) {
  10. this.v = v;
  11. notifyListeners();
  12. }
  13. static String key(String location, PluginId id, String peer, String k) =>
  14. '$location|$id|$peer|$k';
  15. }
  16. class PluginModel with ChangeNotifier {
  17. final List<UiType> uiList = [];
  18. final Map<String, String> opts = {};
  19. void add(List<UiType> uiList) {
  20. bool found = false;
  21. for (var ui in uiList) {
  22. for (int i = 0; i < this.uiList.length; i++) {
  23. if (this.uiList[i].key == ui.key) {
  24. this.uiList[i] = ui;
  25. found = true;
  26. }
  27. }
  28. if (!found) {
  29. this.uiList.add(ui);
  30. }
  31. }
  32. notifyListeners();
  33. }
  34. String? getOpt(String key) => opts.remove(key);
  35. bool get isEmpty => uiList.isEmpty;
  36. }
  37. class LocationModel with ChangeNotifier {
  38. final Map<PluginId, PluginModel> pluginModels = {};
  39. void add(PluginId id, List<UiType> uiList) {
  40. if (pluginModels[id] != null) {
  41. pluginModels[id]!.add(uiList);
  42. } else {
  43. var model = PluginModel();
  44. model.add(uiList);
  45. pluginModels[id] = model;
  46. notifyListeners();
  47. }
  48. }
  49. void clear() {
  50. pluginModels.clear();
  51. notifyListeners();
  52. }
  53. void remove(PluginId id) {
  54. pluginModels.remove(id);
  55. notifyListeners();
  56. }
  57. bool get isEmpty => pluginModels.isEmpty;
  58. }
  59. void addLocationUi(String location, PluginId id, List<UiType> uiList) {
  60. if (_locationModels[location] == null) {
  61. _locationModels[location] = LocationModel();
  62. }
  63. _locationModels[location]?.add(id, uiList);
  64. }
  65. LocationModel? getLocationModel(String location) => _locationModels[location];
  66. PluginModel? getPluginModel(String location, PluginId id) =>
  67. _locationModels[location]?.pluginModels[id];
  68. void clearPlugin(PluginId pluginId) {
  69. for (var element in _locationModels.values) {
  70. element.remove(pluginId);
  71. }
  72. }
  73. void clearLocations() {
  74. for (var element in _locationModels.values) {
  75. element.clear();
  76. }
  77. }
  78. OptionModel getOptionModel(
  79. String location, PluginId pluginId, String peer, String key) {
  80. final k = OptionModel.key(location, pluginId, peer, key);
  81. if (_optionModels[k] == null) {
  82. _optionModels[k] = OptionModel();
  83. }
  84. return _optionModels[k]!;
  85. }
  86. void updateOption(
  87. String location, PluginId id, String peer, String key, String value) {
  88. final k = OptionModel.key(location, id, peer, key);
  89. _optionModels[k]?.value = value;
  90. }