2022-07-25 16:26:51 +08:00
|
|
|
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';
|
2022-08-03 22:03:31 +08:00
|
|
|
import 'package:flutter_hbb/models/platform_model.dart';
|
2022-07-26 17:03:19 +08:00
|
|
|
import 'package:get/get.dart';
|
2022-07-25 16:26:51 +08:00
|
|
|
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;
|
2022-07-26 17:03:19 +08:00
|
|
|
var tags = [].obs;
|
2022-10-08 15:53:03 +08:00
|
|
|
var peers = List<Peer>.empty(growable: true).obs;
|
2022-07-26 17:03:19 +08:00
|
|
|
|
|
|
|
var selectedTags = List<String>.empty(growable: true).obs;
|
2022-07-25 16:26:51 +08:00
|
|
|
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
|
|
|
AbModel(this.parent);
|
|
|
|
|
|
|
|
FFI? get _ffi => parent.target;
|
|
|
|
|
2022-10-08 16:13:24 +08:00
|
|
|
Future<dynamic> pullAb() async {
|
2022-10-09 18:41:50 +08:00
|
|
|
if (_ffi!.userModel.userName.isEmpty) return;
|
2022-10-08 16:13:24 +08:00
|
|
|
abLoading.value = true;
|
2022-10-09 18:41:50 +08:00
|
|
|
abError.value = "";
|
2022-10-08 15:53:03 +08:00
|
|
|
final api = "${await bind.mainGetApiServer()}/api/ab/get";
|
2022-07-26 17:14:52 +08:00
|
|
|
try {
|
|
|
|
final resp =
|
2022-09-27 17:52:36 +08:00
|
|
|
await http.post(Uri.parse(api), headers: await getHttpHeaders());
|
2022-09-15 11:06:44 +08:00
|
|
|
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
|
|
|
|
Map<String, dynamic> json = jsonDecode(resp.body);
|
|
|
|
if (json.containsKey('error')) {
|
2022-11-30 11:13:02 +08:00
|
|
|
abError.value = json['error'];
|
2022-09-15 11:06:44 +08:00
|
|
|
} else if (json.containsKey('data')) {
|
|
|
|
final data = jsonDecode(json['data']);
|
2022-11-30 11:13:02 +08:00
|
|
|
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
|
|
|
}
|
2022-09-15 11:06:44 +08:00
|
|
|
}
|
|
|
|
return resp.body;
|
|
|
|
} else {
|
|
|
|
return "";
|
2022-07-26 17:14:52 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-10-08 15:53:03 +08:00
|
|
|
err.printError();
|
2022-10-08 16:13:24 +08:00
|
|
|
abError.value = err.toString();
|
2022-07-26 17:14:52 +08:00
|
|
|
} finally {
|
2022-10-08 16:13:24 +08:00
|
|
|
abLoading.value = false;
|
2022-07-25 16:26:51 +08:00
|
|
|
}
|
2022-07-26 17:14:52 +08:00
|
|
|
return null;
|
2022-07-25 16:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
tags.clear();
|
|
|
|
peers.clear();
|
|
|
|
}
|
|
|
|
|
2022-11-27 12:16:45 +08:00
|
|
|
void addId(String id, String alias, List<dynamic> tags) {
|
2022-07-26 17:03:19 +08:00
|
|
|
if (idContainBy(id)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-27 12:16:45 +08:00
|
|
|
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);
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-07-26 17:03:19 +08:00
|
|
|
if (it.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-08 15:53:03 +08:00
|
|
|
it.first.tags = tags;
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
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";
|
2022-09-27 17:52:36 +08:00
|
|
|
var authHeaders = await getHttpHeaders();
|
2022-07-26 17:03:19 +08:00
|
|
|
authHeaders['Content-Type'] = "application/json";
|
2022-10-08 15:53:03 +08:00
|
|
|
final peersJsonData = peers.map((e) => e.toJson()).toList();
|
2022-07-26 17:03:19 +08:00
|
|
|
final body = jsonEncode({
|
2022-10-08 15:53:03 +08:00
|
|
|
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
|
2022-07-26 17:03:19 +08:00
|
|
|
});
|
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
|
|
|
}
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool idContainBy(String id) {
|
2022-10-08 15:53:03 +08:00
|
|
|
return peers.where((element) => element.id == id).isNotEmpty;
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void deleteTag(String tag) {
|
2022-10-08 18:56:04 +08:00
|
|
|
gFFI.abModel.selectedTags.remove(tag);
|
2022-07-26 17:03:19 +08:00
|
|
|
tags.removeWhere((element) => element == tag);
|
|
|
|
for (var peer in peers) {
|
2022-10-08 15:53:03 +08:00
|
|
|
if (peer.tags.isEmpty) {
|
2022-07-26 17:03:19 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-08 15:53:03 +08:00
|
|
|
if (peer.tags.contains(tag)) {
|
|
|
|
((peer.tags)).remove(tag);
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-07-26 17:03:19 +08:00
|
|
|
if (it.isEmpty) {
|
|
|
|
return [];
|
|
|
|
} else {
|
2022-10-08 15:53:03 +08:00
|
|
|
return it.first.tags;
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-27 14:29:47 +08:00
|
|
|
|
2022-10-08 15:53:03 +08:00
|
|
|
void setPeerAlias(String id, String value) {
|
|
|
|
final it = peers.where((p0) => p0.id == id);
|
2022-07-29 12:03:24 +08:00
|
|
|
if (it.isEmpty) {
|
2022-10-08 15:53:03 +08:00
|
|
|
debugPrint("$id is not exists");
|
2022-07-29 12:03:24 +08:00
|
|
|
return;
|
|
|
|
} else {
|
2022-10-08 15:53:03 +08:00
|
|
|
it.first.alias = value;
|
2022-07-29 12:03:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:29:47 +08:00
|
|
|
void clear() {
|
|
|
|
peers.clear();
|
|
|
|
tags.clear();
|
|
|
|
}
|
2022-07-25 16:26:51 +08:00
|
|
|
}
|