2022-12-11 21:40:35 +08:00
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:flutter_hbb/common.dart';
|
|
|
|
import 'package:flutter_hbb/common/widgets/peer_tab_page.dart';
|
|
|
|
import 'package:flutter_hbb/common/hbbs/hbbs.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 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
|
|
class GroupModel {
|
|
|
|
final RxBool userLoading = false.obs;
|
|
|
|
final RxString userLoadError = "".obs;
|
|
|
|
final RxBool peerLoading = false.obs; //to-do: not used
|
|
|
|
final RxString peerLoadError = "".obs;
|
|
|
|
final RxList<UserPayload> users = RxList.empty(growable: true);
|
|
|
|
final RxList<PeerPayload> peerPayloads = RxList.empty(growable: true);
|
|
|
|
final RxList<Peer> peersShow = RxList.empty(growable: true);
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
|
|
|
GroupModel(this.parent);
|
|
|
|
|
|
|
|
Future<void> reset() async {
|
|
|
|
userLoading.value = false;
|
|
|
|
userLoadError.value = "";
|
|
|
|
peerLoading.value = false;
|
|
|
|
peerLoadError.value = "";
|
|
|
|
users.clear();
|
|
|
|
peerPayloads.clear();
|
|
|
|
peersShow.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> pull() async {
|
|
|
|
await reset();
|
|
|
|
if (gFFI.userModel.userName.isEmpty ||
|
|
|
|
(gFFI.userModel.isAdmin.isFalse && gFFI.userModel.groupName.isEmpty)) {
|
|
|
|
statePeerTab.check();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
userLoading.value = true;
|
|
|
|
userLoadError.value = "";
|
|
|
|
final api = "${await bind.mainGetApiServer()}/api/users";
|
|
|
|
try {
|
|
|
|
var uri0 = Uri.parse(api);
|
|
|
|
final pageSize = 20;
|
|
|
|
var total = 0;
|
2022-12-23 10:36:18 +08:00
|
|
|
int current = 0;
|
2022-12-11 21:40:35 +08:00
|
|
|
do {
|
2022-12-23 10:36:18 +08:00
|
|
|
current += 1;
|
2022-12-11 21:40:35 +08:00
|
|
|
var uri = Uri(
|
|
|
|
scheme: uri0.scheme,
|
|
|
|
host: uri0.host,
|
|
|
|
path: uri0.path,
|
|
|
|
port: uri0.port,
|
|
|
|
queryParameters: {
|
|
|
|
'current': current.toString(),
|
|
|
|
'pageSize': pageSize.toString(),
|
|
|
|
if (gFFI.userModel.isAdmin.isFalse)
|
|
|
|
'grp': gFFI.userModel.groupName.value,
|
|
|
|
});
|
|
|
|
final resp = await http.get(uri, headers: await getHttpHeaders());
|
|
|
|
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
|
|
|
|
Map<String, dynamic> json = jsonDecode(resp.body);
|
|
|
|
if (json.containsKey('error')) {
|
|
|
|
throw json['error'];
|
|
|
|
} else {
|
2022-12-14 12:27:57 +08:00
|
|
|
if (total == 0) total = json['total'];
|
2022-12-11 21:40:35 +08:00
|
|
|
if (json.containsKey('data')) {
|
|
|
|
final data = json['data'];
|
|
|
|
if (data is List) {
|
|
|
|
for (final user in data) {
|
|
|
|
users.add(UserPayload.fromJson(user));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 10:36:18 +08:00
|
|
|
} while (current * pageSize < total);
|
2022-12-11 21:40:35 +08:00
|
|
|
} catch (err) {
|
|
|
|
debugPrint('$err');
|
|
|
|
userLoadError.value = err.toString();
|
|
|
|
} finally {
|
|
|
|
userLoading.value = false;
|
|
|
|
statePeerTab.check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> pullUserPeers(String username) async {
|
|
|
|
peerPayloads.clear();
|
|
|
|
peersShow.clear();
|
|
|
|
peerLoading.value = true;
|
|
|
|
peerLoadError.value = "";
|
|
|
|
final api = "${await bind.mainGetApiServer()}/api/peers";
|
|
|
|
try {
|
|
|
|
var uri0 = Uri.parse(api);
|
|
|
|
final pageSize = 20;
|
|
|
|
var total = 0;
|
2022-12-23 10:36:18 +08:00
|
|
|
int current = 0;
|
2022-12-11 21:40:35 +08:00
|
|
|
do {
|
2022-12-23 10:36:18 +08:00
|
|
|
current += 1;
|
2022-12-11 21:40:35 +08:00
|
|
|
var uri = Uri(
|
|
|
|
scheme: uri0.scheme,
|
|
|
|
host: uri0.host,
|
|
|
|
path: uri0.path,
|
|
|
|
port: uri0.port,
|
|
|
|
queryParameters: {
|
|
|
|
'current': current.toString(),
|
|
|
|
'pageSize': pageSize.toString(),
|
2022-12-19 21:38:33 +08:00
|
|
|
'grp': gFFI.userModel.groupName.value,
|
|
|
|
'target_user': username
|
2022-12-11 21:40:35 +08:00
|
|
|
});
|
|
|
|
final resp = await http.get(uri, headers: await getHttpHeaders());
|
|
|
|
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
|
|
|
|
Map<String, dynamic> json = jsonDecode(resp.body);
|
|
|
|
if (json.containsKey('error')) {
|
|
|
|
throw json['error'];
|
|
|
|
} else {
|
2022-12-14 12:27:57 +08:00
|
|
|
if (total == 0) total = json['total'];
|
2022-12-11 21:40:35 +08:00
|
|
|
if (json.containsKey('data')) {
|
|
|
|
final data = json['data'];
|
|
|
|
if (data is List) {
|
|
|
|
for (final p in data) {
|
|
|
|
final peer = PeerPayload.fromJson(p);
|
|
|
|
peerPayloads.add(peer);
|
|
|
|
peersShow.add(PeerPayload.toPeer(peer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 10:36:18 +08:00
|
|
|
} while (current * pageSize < total);
|
2022-12-11 21:40:35 +08:00
|
|
|
} catch (err) {
|
|
|
|
debugPrint('$err');
|
|
|
|
peerLoadError.value = err.toString();
|
|
|
|
} finally {
|
|
|
|
peerLoading.value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|