Files
rustdesk/flutter/lib/plugin/model.dart
T

111 lines
2.6 KiB
Dart
Raw Normal View History

2023-04-20 22:53:43 +08:00
import 'package:flutter/material.dart';
import './common.dart';
2023-05-09 19:47:26 +08:00
import './manager.dart';
2023-04-20 22:53:43 +08:00
2023-04-22 22:21:02 +08:00
final Map<String, LocationModel> _locationModels = {};
final Map<String, OptionModel> _optionModels = {};
2023-04-21 21:40:34 +08:00
class OptionModel with ChangeNotifier {
String? v;
2023-04-21 21:40:34 +08:00
String? get value => v;
set value(String? v) {
this.v = v;
2023-04-21 21:40:34 +08:00
notifyListeners();
}
static String key(String location, PluginId id, String peer, String k) =>
'$location|$id|$peer|$k';
2023-04-21 21:40:34 +08:00
}
2023-04-20 22:53:43 +08:00
class PluginModel with ChangeNotifier {
final List<UiType> uiList = [];
2023-04-21 21:40:34 +08:00
final Map<String, String> opts = {};
2023-04-20 22:53:43 +08:00
2023-05-10 18:58:45 +08:00
void add(List<UiType> uiList) {
2023-04-29 12:34:27 +08:00
bool found = false;
2023-05-10 18:58:45 +08:00
for (var ui in uiList) {
for (int i = 0; i < this.uiList.length; i++) {
if (this.uiList[i].key == ui.key) {
this.uiList[i] = ui;
found = true;
}
}
if (!found) {
this.uiList.add(ui);
2023-04-29 12:34:27 +08:00
}
}
2023-04-20 22:53:43 +08:00
notifyListeners();
}
2023-04-21 21:40:34 +08:00
String? getOpt(String key) => opts.remove(key);
2023-04-20 22:53:43 +08:00
bool get isEmpty => uiList.isEmpty;
}
class LocationModel with ChangeNotifier {
final Map<PluginId, PluginModel> pluginModels = {};
2023-05-10 18:58:45 +08:00
void add(PluginId id, List<UiType> uiList) {
2023-04-20 22:53:43 +08:00
if (pluginModels[id] != null) {
2023-05-10 18:58:45 +08:00
pluginModels[id]!.add(uiList);
2023-04-20 22:53:43 +08:00
} else {
var model = PluginModel();
2023-05-10 18:58:45 +08:00
model.add(uiList);
2023-04-20 22:53:43 +08:00
pluginModels[id] = model;
notifyListeners();
}
}
2023-04-22 22:21:02 +08:00
void clear() {
pluginModels.clear();
notifyListeners();
}
2023-04-25 15:35:48 +08:00
void remove(PluginId id) {
pluginModels.remove(id);
notifyListeners();
}
2023-04-20 22:53:43 +08:00
bool get isEmpty => pluginModels.isEmpty;
}
2023-05-10 18:58:45 +08:00
void addLocationUi(String location, PluginId id, List<UiType> uiList) {
2023-04-22 22:21:02 +08:00
if (_locationModels[location] == null) {
_locationModels[location] = LocationModel();
}
2023-05-10 18:58:45 +08:00
_locationModels[location]?.add(id, uiList);
2023-04-22 22:21:02 +08:00
}
2023-04-24 18:45:22 +08:00
LocationModel? getLocationModel(String location) => _locationModels[location];
PluginModel? getPluginModel(String location, PluginId id) =>
_locationModels[location]?.pluginModels[id];
2023-04-22 22:21:02 +08:00
void clearPlugin(PluginId pluginId) {
for (var element in _locationModels.values) {
2023-04-25 15:35:48 +08:00
element.remove(pluginId);
2023-04-22 22:21:02 +08:00
}
}
void clearLocations() {
for (var element in _locationModels.values) {
element.clear();
2023-04-20 22:53:43 +08:00
}
}
2023-04-21 21:40:34 +08:00
2023-04-24 18:45:22 +08:00
OptionModel getOptionModel(
String location, PluginId pluginId, String peer, String key) {
final k = OptionModel.key(location, pluginId, peer, key);
2023-04-22 22:21:02 +08:00
if (_optionModels[k] == null) {
_optionModels[k] = OptionModel();
2023-04-21 21:40:34 +08:00
}
2023-04-22 22:21:02 +08:00
return _optionModels[k]!;
2023-04-21 21:40:34 +08:00
}
void updateOption(
String location, PluginId id, String peer, String key, String value) {
final k = OptionModel.key(location, id, peer, key);
2023-04-22 22:21:02 +08:00
_optionModels[k]?.value = value;
2023-04-21 21:40:34 +08:00
}