From ec9062f505ca664076653afae75bf36183540e7b Mon Sep 17 00:00:00 2001 From: 21pages Date: Wed, 21 Jun 2023 09:30:32 +0800 Subject: [PATCH] filter group peers without http request Signed-off-by: 21pages --- flutter/lib/common/hbbs/hbbs.dart | 2 +- flutter/lib/common/widgets/my_group.dart | 151 ++++++++++----------- flutter/lib/common/widgets/peers_view.dart | 15 ++ flutter/lib/models/group_model.dart | 31 +++-- 4 files changed, 108 insertions(+), 91 deletions(-) diff --git a/flutter/lib/common/hbbs/hbbs.dart b/flutter/lib/common/hbbs/hbbs.dart index b798c8c9b..d6baabdc0 100644 --- a/flutter/lib/common/hbbs/hbbs.dart +++ b/flutter/lib/common/hbbs/hbbs.dart @@ -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}); } } diff --git a/flutter/lib/common/widgets/my_group.dart b/flutter/lib/common/widgets/my_group.dart index 5ba6bbfad..cf7c4ae45 100644 --- a/flutter/lib/common/widgets/my_group.dart +++ b/flutter/lib/common/widgets/my_group.dart @@ -16,8 +16,8 @@ class MyGroup extends StatefulWidget { } class _MyGroupState extends State { - 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 { }); Future 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 { } 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 { return InkWell(onTap: () { if (selectedUser.value != username) { selectedUser.value = username; - gFFI.groupModel.pullUserPeers(user); + } else { + selectedUser.value = ''; } }, child: Obx( () { diff --git a/flutter/lib/common/widgets/peers_view.dart b/flutter/lib/common/widgets/peers_view.dart index 16a6f98ef..a0b972d1e 100644 --- a/flutter/lib/common/widgets/peers_view.dart +++ b/flutter/lib/common/widgets/peers_view.dart @@ -427,10 +427,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; + } } diff --git a/flutter/lib/models/group_model.dart b/flutter/lib/models/group_model.dart index 5573d7361..bcd0ab9f1 100644 --- a/flutter/lib/models/group_model.dart +++ b/flutter/lib/models/group_model.dart @@ -16,8 +16,9 @@ class GroupModel { final RxString groupName = ''.obs; final RxString groupId = ''.obs; final RxList users = RxList.empty(growable: true); - final RxList peerPayloads = RxList.empty(growable: true); - final RxList peersShow = RxList.empty(growable: true); + final List peersShow = List.empty(growable: true); + final RxString selectedUser = ''.obs; + final RxString searchUserText = ''.obs; WeakReference 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 pullUserPeers(UserPayload user) async { - peerPayloads.clear(); + Future pullUserPeers() async { peersShow.clear(); peerLoading.value = true; peerLoadError.value = ""; + List 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 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;