mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-12-12 02:57:22 +00:00
@@ -28,6 +28,7 @@ class AbModel {
|
||||
final pullError = "".obs;
|
||||
final pushError = "".obs;
|
||||
final tags = [].obs;
|
||||
final RxMap<String, int> tagColors = Map<String, int>.fromEntries([]).obs;
|
||||
final peers = List<Peer>.empty(growable: true).obs;
|
||||
final sortTags = shouldSortTags().obs;
|
||||
final retrying = false.obs;
|
||||
@@ -80,10 +81,11 @@ class AbModel {
|
||||
if (resp.body.toLowerCase() == "null") {
|
||||
// normal reply, emtpy ab return null
|
||||
tags.clear();
|
||||
tagColors.clear();
|
||||
peers.clear();
|
||||
} else if (resp.body.isNotEmpty) {
|
||||
Map<String, dynamic> json =
|
||||
_jsonDecode(utf8.decode(resp.bodyBytes), resp.statusCode);
|
||||
_jsonDecodeResp(utf8.decode(resp.bodyBytes), resp.statusCode);
|
||||
if (json.containsKey('error')) {
|
||||
throw json['error'];
|
||||
} else if (json.containsKey('data')) {
|
||||
@@ -93,26 +95,7 @@ class AbModel {
|
||||
} catch (e) {}
|
||||
final data = jsonDecode(json['data']);
|
||||
if (data != null) {
|
||||
final oldOnlineIDs =
|
||||
peers.where((e) => e.online).map((e) => e.id).toList();
|
||||
tags.clear();
|
||||
peers.clear();
|
||||
if (data['tags'] is List) {
|
||||
tags.value = data['tags'];
|
||||
}
|
||||
if (data['peers'] is List) {
|
||||
for (final peer in data['peers']) {
|
||||
peers.add(Peer.fromJson(peer));
|
||||
}
|
||||
}
|
||||
if (isFull(false)) {
|
||||
peers.removeRange(licensedDevices, peers.length);
|
||||
}
|
||||
// restore online
|
||||
peers
|
||||
.where((e) => oldOnlineIDs.contains(e.id))
|
||||
.map((e) => e.online = true)
|
||||
.toList();
|
||||
_deserialize(data);
|
||||
_saveCache(); // save on success
|
||||
}
|
||||
}
|
||||
@@ -242,10 +225,7 @@ class AbModel {
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab";
|
||||
var authHeaders = getHttpHeaders();
|
||||
authHeaders['Content-Type'] = "application/json";
|
||||
final peersJsonData = peers.map((e) => e.toAbUploadJson()).toList();
|
||||
final body = jsonEncode({
|
||||
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
|
||||
});
|
||||
final body = jsonEncode(_serialize());
|
||||
http.Response resp;
|
||||
// support compression
|
||||
if (licensedDevices > 0 && body.length > 1024) {
|
||||
@@ -261,7 +241,7 @@ class AbModel {
|
||||
ret = true;
|
||||
_saveCache();
|
||||
} else {
|
||||
Map<String, dynamic> json = _jsonDecode(resp.body, resp.statusCode);
|
||||
Map<String, dynamic> json = _jsonDecodeResp(resp.body, resp.statusCode);
|
||||
if (json.containsKey('error')) {
|
||||
throw json['error'];
|
||||
} else if (resp.statusCode == 200) {
|
||||
@@ -318,6 +298,7 @@ class AbModel {
|
||||
void deleteTag(String tag) {
|
||||
gFFI.abModel.selectedTags.remove(tag);
|
||||
tags.removeWhere((element) => element == tag);
|
||||
tagColors.remove(tag);
|
||||
for (var peer in peers) {
|
||||
if (peer.tags.isEmpty) {
|
||||
continue;
|
||||
@@ -353,6 +334,11 @@ class AbModel {
|
||||
}
|
||||
}).toList();
|
||||
}
|
||||
int? oldColor = tagColors[oldTag];
|
||||
if (oldColor != null) {
|
||||
tagColors.remove(oldTag);
|
||||
tagColors.addAll({newTag: oldColor});
|
||||
}
|
||||
}
|
||||
|
||||
void unsetSelectedTags() {
|
||||
@@ -368,6 +354,20 @@ class AbModel {
|
||||
}
|
||||
}
|
||||
|
||||
Color getTagColor(String tag) {
|
||||
int? colorValue = tagColors[tag];
|
||||
if (colorValue != null) {
|
||||
return Color(colorValue);
|
||||
}
|
||||
return str2color2(tag, existing: tagColors.values.toList());
|
||||
}
|
||||
|
||||
setTagColor(String tag, Color color) {
|
||||
if (tags.contains(tag)) {
|
||||
tagColors[tag] = color.value;
|
||||
}
|
||||
}
|
||||
|
||||
void merge(Peer r, Peer p) {
|
||||
p.hash = r.hash.isEmpty ? p.hash : r.hash;
|
||||
p.username = r.username.isEmpty ? p.username : r.username;
|
||||
@@ -467,12 +467,10 @@ class AbModel {
|
||||
|
||||
_saveCache() {
|
||||
try {
|
||||
final peersJsonData = peers.map((e) => e.toAbUploadJson()).toList();
|
||||
final m = <String, dynamic>{
|
||||
var m = _serialize();
|
||||
m.addAll(<String, dynamic>{
|
||||
"access_token": bind.mainGetLocalOption(key: 'access_token'),
|
||||
"peers": peersJsonData,
|
||||
"tags": tags.map((e) => e.toString()).toList(),
|
||||
};
|
||||
});
|
||||
bind.mainSaveAb(json: jsonEncode(m));
|
||||
} catch (e) {
|
||||
debugPrint('ab save:$e');
|
||||
@@ -488,22 +486,13 @@ class AbModel {
|
||||
final cache = await bind.mainLoadAb();
|
||||
final data = jsonDecode(cache);
|
||||
if (data == null || data['access_token'] != access_token) return;
|
||||
tags.clear();
|
||||
peers.clear();
|
||||
if (data['tags'] is List) {
|
||||
tags.value = data['tags'];
|
||||
}
|
||||
if (data['peers'] is List) {
|
||||
for (final peer in data['peers']) {
|
||||
peers.add(Peer.fromJson(peer));
|
||||
}
|
||||
}
|
||||
_deserialize(data);
|
||||
} catch (e) {
|
||||
debugPrint("load ab cache: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _jsonDecode(String body, int statusCode) {
|
||||
Map<String, dynamic> _jsonDecodeResp(String body, int statusCode) {
|
||||
try {
|
||||
Map<String, dynamic> json = jsonDecode(body);
|
||||
return json;
|
||||
@@ -516,6 +505,50 @@ class AbModel {
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _serialize() {
|
||||
final peersJsonData = peers.map((e) => e.toAbUploadJson()).toList();
|
||||
final tagColorJsonData = jsonEncode(tagColors);
|
||||
return {
|
||||
"tags": tags,
|
||||
"peers": peersJsonData,
|
||||
"tag_colors": tagColorJsonData
|
||||
};
|
||||
}
|
||||
|
||||
_deserialize(dynamic data) {
|
||||
if (data == null) return;
|
||||
final oldOnlineIDs = peers.where((e) => e.online).map((e) => e.id).toList();
|
||||
tags.clear();
|
||||
tagColors.clear();
|
||||
peers.clear();
|
||||
if (data['tags'] is List) {
|
||||
tags.value = data['tags'];
|
||||
}
|
||||
if (data['peers'] is List) {
|
||||
for (final peer in data['peers']) {
|
||||
peers.add(Peer.fromJson(peer));
|
||||
}
|
||||
}
|
||||
if (isFull(false)) {
|
||||
peers.removeRange(licensedDevices, peers.length);
|
||||
}
|
||||
// restore online
|
||||
peers
|
||||
.where((e) => oldOnlineIDs.contains(e.id))
|
||||
.map((e) => e.online = true)
|
||||
.toList();
|
||||
if (data['tag_colors'] is String) {
|
||||
Map<String, dynamic> map = jsonDecode(data['tag_colors']);
|
||||
tagColors.value = Map<String, int>.from(map);
|
||||
}
|
||||
// add color to tag
|
||||
final tagsWithoutColor =
|
||||
tags.toList().where((e) => !tagColors.containsKey(e)).toList();
|
||||
for (var t in tagsWithoutColor) {
|
||||
tagColors[t] = str2color2(t, existing: tagColors.values.toList()).value;
|
||||
}
|
||||
}
|
||||
|
||||
reSyncToast(Future<bool> future) {
|
||||
if (!shouldSyncAb()) return;
|
||||
Future.delayed(Duration.zero, () async {
|
||||
|
||||
Reference in New Issue
Block a user