Merge pull request #4716 from 21pages/group

filter group peers without http request
This commit is contained in:
RustDesk 2023-06-21 10:41:57 +08:00 committed by GitHub
commit 5f5d63187d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 91 deletions

View File

@ -48,7 +48,7 @@ class PeerPayload {
note = json['note'] ?? '';
static Peer toPeer(PeerPayload p) {
return Peer.fromJson({"id": p.id});
return Peer.fromJson({"id": p.id, "username": p.user_name});
}
}

View File

@ -16,8 +16,8 @@ class MyGroup extends StatefulWidget {
}
class _MyGroupState extends State<MyGroup> {
static final RxString selectedUser = ''.obs;
static final RxString searchUserText = ''.obs;
RxString get selectedUser => gFFI.groupModel.selectedUser;
RxString get searchUserText => gFFI.groupModel.searchUserText;
static TextEditingController searchUserController = TextEditingController();
@override
@ -37,8 +37,10 @@ class _MyGroupState extends State<MyGroup> {
});
Future<Widget> buildBody(BuildContext context) async {
gFFI.groupModel.pullUserPeers();
return Obx(() {
if (gFFI.groupModel.groupLoading.value) {
if (gFFI.groupModel.groupLoading.value ||
gFFI.groupModel.peerLoading.value) {
return const Center(
child: CircularProgressIndicator(),
);
@ -70,85 +72,81 @@ class _MyGroupState extends State<MyGroup> {
}
Widget _buildDesktop() {
return Obx(
() => Row(
children: [
Card(
margin: EdgeInsets.symmetric(horizontal: 4.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: BorderSide(
color: Theme.of(context).scaffoldBackgroundColor)),
child: Container(
width: 200,
height: double.infinity,
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column(
children: [
_buildLeftHeader(),
Expanded(
child: Container(
width: double.infinity,
height: double.infinity,
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(2)),
child: _buildUserContacts(),
).marginSymmetric(vertical: 8.0),
)
],
),
return Row(
children: [
Card(
margin: EdgeInsets.symmetric(horizontal: 4.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side:
BorderSide(color: Theme.of(context).scaffoldBackgroundColor)),
child: Container(
width: 200,
height: double.infinity,
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column(
children: [
_buildLeftHeader(),
Expanded(
child: Container(
width: double.infinity,
height: double.infinity,
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(2)),
child: _buildUserContacts(),
).marginSymmetric(vertical: 8.0),
)
],
),
).marginOnly(right: 8.0),
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: MyGroupPeerView(
menuPadding: widget.menuPadding,
initPeers: gFFI.groupModel.peersShow.value)),
)
],
),
),
).marginOnly(right: 8.0),
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: MyGroupPeerView(
menuPadding: widget.menuPadding,
initPeers: gFFI.groupModel.peersShow)),
)
],
);
}
Widget _buildMobile() {
return Obx(
() => Column(
children: [
Card(
margin: EdgeInsets.symmetric(horizontal: 4.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: BorderSide(
color: Theme.of(context).scaffoldBackgroundColor)),
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_buildLeftHeader(),
Container(
width: double.infinity,
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(4)),
child: _buildUserContacts(),
).marginSymmetric(vertical: 8.0)
],
),
return Column(
children: [
Card(
margin: EdgeInsets.symmetric(horizontal: 4.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side:
BorderSide(color: Theme.of(context).scaffoldBackgroundColor)),
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_buildLeftHeader(),
Container(
width: double.infinity,
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(4)),
child: _buildUserContacts(),
).marginSymmetric(vertical: 8.0)
],
),
),
Divider(),
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: MyGroupPeerView(
menuPadding: widget.menuPadding,
initPeers: gFFI.groupModel.peersShow.value)),
)
],
),
),
Divider(),
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: MyGroupPeerView(
menuPadding: widget.menuPadding,
initPeers: gFFI.groupModel.peersShow)),
)
],
);
}
@ -198,7 +196,8 @@ class _MyGroupState extends State<MyGroup> {
return InkWell(onTap: () {
if (selectedUser.value != username) {
selectedUser.value = username;
gFFI.groupModel.pullUserPeers(user);
} else {
selectedUser.value = '';
}
}, child: Obx(
() {

View File

@ -441,10 +441,25 @@ class MyGroupPeerView extends BasePeersView {
key: key,
name: 'my group peer',
loadEvent: 'load_my_group_peers',
peerFilter: filter,
peerCardBuilder: (Peer peer) => MyGroupPeerCard(
peer: peer,
menuPadding: menuPadding,
),
initPeers: initPeers,
);
static bool filter(Peer peer) {
if (gFFI.groupModel.searchUserText.isNotEmpty) {
if (!peer.username.contains(gFFI.groupModel.searchUserText)) {
return false;
}
}
if (gFFI.groupModel.selectedUser.isNotEmpty) {
if (gFFI.groupModel.selectedUser.value != peer.username) {
return false;
}
}
return true;
}
}

View File

@ -16,8 +16,9 @@ class GroupModel {
final RxString groupName = ''.obs;
final RxString groupId = ''.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);
final List<Peer> peersShow = List.empty(growable: true);
final RxString selectedUser = ''.obs;
final RxString searchUserText = ''.obs;
WeakReference<FFI> parent;
GroupModel(this.parent);
@ -30,7 +31,6 @@ class GroupModel {
groupName.value = '';
groupId.value = '';
users.clear();
peerPayloads.clear();
peersShow.clear();
}
@ -45,7 +45,7 @@ class GroupModel {
return;
}
} catch (e) {
debugPrintStack(label: '$e');
debugPrint('$e');
reset();
return;
}
@ -98,7 +98,7 @@ class GroupModel {
}
} while (current * pageSize < total);
} catch (err) {
debugPrintStack(label: '$err');
debugPrint('$err');
groupLoadError.value = err.toString();
} finally {
groupLoading.value = false;
@ -128,23 +128,30 @@ class GroupModel {
groupId.value = data['id'] ?? '';
return groupId.value.isNotEmpty && groupName.isNotEmpty;
} catch (e) {
debugPrintStack(label: '$e');
debugPrint('$e');
} finally {}
return false;
}
Future<void> pullUserPeers(UserPayload user) async {
peerPayloads.clear();
Future<void> pullUserPeers() async {
peersShow.clear();
peerLoading.value = true;
peerLoadError.value = "";
List<PeerPayload> peerPayloads = List.empty(growable: true);
final api = "${await bind.mainGetApiServer()}/api/peers";
try {
var uri0 = Uri.parse(api);
final pageSize = 20;
var total = 0;
int current = 0;
var queryParameters = {
'current': current.toString(),
'pageSize': pageSize.toString(),
};
if (!gFFI.userModel.isAdmin.value) {
queryParameters.addAll({'grp': groupId.value});
}
do {
current += 1;
var uri = Uri(
@ -152,11 +159,7 @@ class GroupModel {
host: uri0.host,
path: uri0.path,
port: uri0.port,
queryParameters: {
'current': current.toString(),
'pageSize': pageSize.toString(),
'target_user': user.id,
});
queryParameters: queryParameters);
final resp = await http.get(uri, headers: getHttpHeaders());
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
Map<String, dynamic> json = jsonDecode(utf8.decode(resp.bodyBytes));
@ -180,7 +183,7 @@ class GroupModel {
}
} while (current * pageSize < total);
} catch (err) {
debugPrintStack(label: '$err');
debugPrint('$err');
peerLoadError.value = err.toString();
} finally {
peerLoading.value = false;