mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-12-12 02:57:22 +00:00
fix ab peers state
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hbb/models/model.dart';
|
||||
import 'package:flutter_hbb/models/peer_model.dart';
|
||||
import 'package:flutter_hbb/models/platform_model.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
@@ -12,7 +13,7 @@ class AbModel with ChangeNotifier {
|
||||
var abLoading = false;
|
||||
var abError = "";
|
||||
var tags = [].obs;
|
||||
var peers = [].obs;
|
||||
var peers = List<Peer>.empty(growable: true).obs;
|
||||
|
||||
var selectedTags = List<String>.empty(growable: true).obs;
|
||||
|
||||
@@ -26,7 +27,7 @@ class AbModel with ChangeNotifier {
|
||||
abLoading = true;
|
||||
notifyListeners();
|
||||
// request
|
||||
final api = "${await getApiServer()}/api/ab/get";
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab/get";
|
||||
try {
|
||||
final resp =
|
||||
await http.post(Uri.parse(api), headers: await getHttpHeaders());
|
||||
@@ -37,7 +38,10 @@ class AbModel with ChangeNotifier {
|
||||
} else if (json.containsKey('data')) {
|
||||
final data = jsonDecode(json['data']);
|
||||
tags.value = data['tags'];
|
||||
peers.value = data['peers'];
|
||||
peers.clear();
|
||||
for (final peer in data['peers']) {
|
||||
peers.add(Peer.fromJson(peer));
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
return resp.body;
|
||||
@@ -45,6 +49,7 @@ class AbModel with ChangeNotifier {
|
||||
return "";
|
||||
}
|
||||
} catch (err) {
|
||||
err.printError();
|
||||
abError = err.toString();
|
||||
} finally {
|
||||
abLoading = false;
|
||||
@@ -53,10 +58,6 @@ class AbModel with ChangeNotifier {
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String> getApiServer() async {
|
||||
return await bind.mainGetApiServer();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
tags.clear();
|
||||
peers.clear();
|
||||
@@ -67,7 +68,7 @@ class AbModel with ChangeNotifier {
|
||||
if (idContainBy(id)) {
|
||||
return;
|
||||
}
|
||||
peers.add({"id": id});
|
||||
peers.add(Peer.fromJson({"id": id}));
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -80,21 +81,22 @@ class AbModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
void changeTagForPeer(String id, List<dynamic> tags) {
|
||||
final it = peers.where((element) => element['id'] == id);
|
||||
final it = peers.where((element) => element.id == id);
|
||||
if (it.isEmpty) {
|
||||
return;
|
||||
}
|
||||
it.first['tags'] = tags;
|
||||
it.first.tags = tags;
|
||||
}
|
||||
|
||||
Future<void> updateAb() async {
|
||||
abLoading = true;
|
||||
notifyListeners();
|
||||
final api = "${await getApiServer()}/api/ab";
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab";
|
||||
var authHeaders = await getHttpHeaders();
|
||||
authHeaders['Content-Type'] = "application/json";
|
||||
final peersJsonData = peers.map((e) => e.toJson()).toList();
|
||||
final body = jsonEncode({
|
||||
"data": jsonEncode({"tags": tags, "peers": peers})
|
||||
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
|
||||
});
|
||||
try {
|
||||
final resp =
|
||||
@@ -111,7 +113,7 @@ class AbModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
bool idContainBy(String id) {
|
||||
return peers.where((element) => element['id'] == id).isNotEmpty;
|
||||
return peers.where((element) => element.id == id).isNotEmpty;
|
||||
}
|
||||
|
||||
bool tagContainBy(String tag) {
|
||||
@@ -119,18 +121,18 @@ class AbModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
void deletePeer(String id) {
|
||||
peers.removeWhere((element) => element['id'] == id);
|
||||
peers.removeWhere((element) => element.id == id);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void deleteTag(String tag) {
|
||||
tags.removeWhere((element) => element == tag);
|
||||
for (var peer in peers) {
|
||||
if (peer['tags'] == null) {
|
||||
if (peer.tags.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
if (((peer['tags']) as List<dynamic>).contains(tag)) {
|
||||
((peer['tags']) as List<dynamic>).remove(tag);
|
||||
if (peer.tags.contains(tag)) {
|
||||
((peer.tags)).remove(tag);
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
@@ -142,21 +144,21 @@ class AbModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
List<dynamic> getPeerTags(String id) {
|
||||
final it = peers.where((p0) => p0['id'] == id);
|
||||
final it = peers.where((p0) => p0.id == id);
|
||||
if (it.isEmpty) {
|
||||
return [];
|
||||
} else {
|
||||
return it.first['tags'] ?? [];
|
||||
return it.first.tags;
|
||||
}
|
||||
}
|
||||
|
||||
void setPeerOption(String id, String key, String value) {
|
||||
final it = peers.where((p0) => p0['id'] == id);
|
||||
void setPeerAlias(String id, String value) {
|
||||
final it = peers.where((p0) => p0.id == id);
|
||||
if (it.isEmpty) {
|
||||
debugPrint("${id} is not exists");
|
||||
debugPrint("$id is not exists");
|
||||
return;
|
||||
} else {
|
||||
it.first[key] = value;
|
||||
it.first.alias = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ class Peer {
|
||||
final String username;
|
||||
final String hostname;
|
||||
final String platform;
|
||||
final String alias;
|
||||
final List<dynamic> tags;
|
||||
String alias;
|
||||
List<dynamic> tags;
|
||||
bool online = false;
|
||||
|
||||
Peer.fromJson(Map<String, dynamic> json)
|
||||
@@ -19,6 +19,17 @@ class Peer {
|
||||
alias = json['alias'] ?? '',
|
||||
tags = json['tags'] ?? [];
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return <String, dynamic>{
|
||||
"id": id,
|
||||
"username": username,
|
||||
"hostname": hostname,
|
||||
"platform": platform,
|
||||
"alias": alias,
|
||||
"tags": tags,
|
||||
};
|
||||
}
|
||||
|
||||
Peer({
|
||||
required this.id,
|
||||
required this.username,
|
||||
|
||||
Reference in New Issue
Block a user