rustdesk/flutter/lib/models/ab_model.dart

190 lines
4.7 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/models/model.dart';
2022-10-08 15:53:03 +08:00
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;
2022-09-27 17:52:36 +08:00
import '../common.dart';
2022-10-08 16:13:24 +08:00
class AbModel {
var abLoading = false.obs;
var abError = "".obs;
var tags = [].obs;
2022-10-08 15:53:03 +08:00
var peers = List<Peer>.empty(growable: true).obs;
var selectedTags = List<String>.empty(growable: true).obs;
WeakReference<FFI> parent;
AbModel(this.parent);
2022-10-08 16:13:24 +08:00
Future<dynamic> pullAb() async {
if (gFFI.userModel.userName.isEmpty) return;
2022-10-08 16:13:24 +08:00
abLoading.value = true;
abError.value = "";
2022-10-08 15:53:03 +08:00
final api = "${await bind.mainGetApiServer()}/api/ab/get";
try {
final resp = await http.post(Uri.parse(api), headers: getHttpHeaders());
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
Map<String, dynamic> json = jsonDecode(resp.body);
if (json.containsKey('error')) {
abError.value = json['error'];
} else if (json.containsKey('data')) {
final data = jsonDecode(json['data']);
if (data != null) {
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));
}
}
2022-10-08 15:53:03 +08:00
}
}
return resp.body;
} else {
return "";
}
} catch (err) {
2022-10-08 15:53:03 +08:00
err.printError();
2022-10-08 16:13:24 +08:00
abError.value = err.toString();
} finally {
2022-10-08 16:13:24 +08:00
abLoading.value = false;
}
return null;
}
Future<void> reset() async {
await bind.mainSetLocalOption(key: "selected-tags", value: '');
tags.clear();
peers.clear();
}
void addId(String id, String alias, List<dynamic> tags) {
if (idContainBy(id)) {
return;
}
final peer = Peer.fromJson({
'id': id,
'alias': alias,
'tags': tags,
});
peers.add(peer);
}
void addPeer(Peer peer) {
peers.removeWhere((e) => e.id == peer.id);
peers.add(peer);
}
void addTag(String tag) async {
if (tagContainBy(tag)) {
return;
}
tags.add(tag);
}
void changeTagForPeer(String id, List<dynamic> tags) {
2022-10-08 15:53:03 +08:00
final it = peers.where((element) => element.id == id);
if (it.isEmpty) {
return;
}
2022-10-08 15:53:03 +08:00
it.first.tags = tags;
}
2022-10-08 16:13:24 +08:00
Future<void> pushAb() async {
abLoading.value = true;
2022-10-08 15:53:03 +08:00
final api = "${await bind.mainGetApiServer()}/api/ab";
var authHeaders = getHttpHeaders();
authHeaders['Content-Type'] = "application/json";
2022-10-08 15:53:03 +08:00
final peersJsonData = peers.map((e) => e.toJson()).toList();
final body = jsonEncode({
2022-10-08 15:53:03 +08:00
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
});
2022-09-19 10:22:40 +08:00
try {
final resp =
await http.post(Uri.parse(api), headers: authHeaders, body: body);
2022-10-08 16:13:24 +08:00
abError.value = "";
await pullAb();
2022-09-19 10:22:40 +08:00
debugPrint("resp: ${resp.body}");
} catch (e) {
2022-10-08 16:13:24 +08:00
abError.value = e.toString();
2022-09-19 10:22:40 +08:00
} finally {
2022-10-08 16:13:24 +08:00
abLoading.value = false;
2022-09-19 10:22:40 +08:00
}
}
Peer? find(String id) {
return peers.firstWhereOrNull((e) => e.id == id);
}
bool idContainBy(String id) {
2022-10-08 15:53:03 +08:00
return peers.where((element) => element.id == id).isNotEmpty;
}
bool tagContainBy(String tag) {
return tags.where((element) => element == tag).isNotEmpty;
}
void deletePeer(String id) {
2022-10-08 15:53:03 +08:00
peers.removeWhere((element) => element.id == id);
}
void deleteTag(String tag) {
2022-10-08 18:56:04 +08:00
gFFI.abModel.selectedTags.remove(tag);
tags.removeWhere((element) => element == tag);
for (var peer in peers) {
2022-10-08 15:53:03 +08:00
if (peer.tags.isEmpty) {
continue;
}
2022-10-08 15:53:03 +08:00
if (peer.tags.contains(tag)) {
((peer.tags)).remove(tag);
}
}
}
void unsetSelectedTags() {
selectedTags.clear();
}
List<dynamic> getPeerTags(String id) {
2022-10-08 15:53:03 +08:00
final it = peers.where((p0) => p0.id == id);
if (it.isEmpty) {
return [];
} else {
2022-10-08 15:53:03 +08:00
return it.first.tags;
}
}
Future<void> setPeerAlias(String id, String value) async {
2022-10-08 15:53:03 +08:00
final it = peers.where((p0) => p0.id == id);
if (it.isNotEmpty) {
2022-10-08 15:53:03 +08:00
it.first.alias = value;
await pushAb();
}
}
Future<void> setPeerForceAlwaysRelay(String id, bool value) async {
final it = peers.where((p0) => p0.id == id);
if (it.isNotEmpty) {
it.first.forceAlwaysRelay = value;
await pushAb();
}
}
Future<void> setRdp(String id, String port, String username) async {
final it = peers.where((p0) => p0.id == id);
if (it.isNotEmpty) {
it.first.rdpPort = port;
it.first.rdpUsername = username;
await pushAb();
}
}
}