2022-07-27 22:56:28 +08:00
|
|
|
import 'dart:async';
|
2023-03-11 02:37:49 +08:00
|
|
|
import 'dart:collection';
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2023-09-19 15:01:16 +08:00
|
|
|
import 'package:dynamic_layouts/dynamic_layouts.dart';
|
2022-07-27 22:56:28 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-23 17:21:50 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-18 23:13:54 +08:00
|
|
|
import 'package:flutter_hbb/consts.dart';
|
2023-09-26 19:58:51 +08:00
|
|
|
import 'package:flutter_hbb/desktop/widgets/scroll_wrapper.dart';
|
2022-08-23 17:21:50 +08:00
|
|
|
import 'package:get/get.dart';
|
2022-07-27 22:56:28 +08:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:visibility_detector/visibility_detector.dart';
|
|
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
|
2022-08-23 17:21:50 +08:00
|
|
|
import '../../common.dart';
|
2022-07-27 22:56:28 +08:00
|
|
|
import '../../models/peer_model.dart';
|
2022-08-03 22:03:31 +08:00
|
|
|
import '../../models/platform_model.dart';
|
2022-09-22 15:35:46 +08:00
|
|
|
import 'peer_card.dart';
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-10-10 20:10:31 +08:00
|
|
|
typedef PeerFilter = bool Function(Peer peer);
|
2022-10-08 09:55:54 +08:00
|
|
|
typedef PeerCardBuilder = Widget Function(Peer peer);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2023-03-09 03:10:08 +08:00
|
|
|
class PeerSortType {
|
|
|
|
static const String remoteId = 'Remote ID';
|
|
|
|
static const String remoteHost = 'Remote Host';
|
|
|
|
static const String username = 'Username';
|
2023-03-10 15:25:19 +08:00
|
|
|
// static const String status = 'Status';
|
2023-03-09 03:10:08 +08:00
|
|
|
|
|
|
|
static List<String> values = [
|
|
|
|
PeerSortType.remoteId,
|
|
|
|
PeerSortType.remoteHost,
|
|
|
|
PeerSortType.username,
|
2023-03-10 15:25:19 +08:00
|
|
|
// PeerSortType.status
|
2023-03-09 03:10:08 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-03-11 02:37:49 +08:00
|
|
|
class LoadEvent {
|
|
|
|
static const String recent = 'load_recent_peers';
|
|
|
|
static const String favorite = 'load_fav_peers';
|
|
|
|
static const String lan = 'load_lan_peers';
|
|
|
|
static const String addressBook = 'load_address_book_peers';
|
2023-09-14 10:17:03 +08:00
|
|
|
static const String group = 'load_group_peers';
|
2023-03-11 02:37:49 +08:00
|
|
|
}
|
|
|
|
|
2022-08-23 17:52:53 +08:00
|
|
|
/// for peer search text, global obs value
|
|
|
|
final peerSearchText = "".obs;
|
2023-03-04 03:53:42 +08:00
|
|
|
|
|
|
|
/// for peer sort, global obs value
|
2024-05-18 23:13:54 +08:00
|
|
|
final peerSort = bind.getLocalFlutterOption(k: kOptionPeerSorting).obs;
|
2023-03-04 03:53:42 +08:00
|
|
|
|
|
|
|
// list for listener
|
|
|
|
final obslist = [peerSearchText, peerSort].obs;
|
|
|
|
|
2022-08-23 17:52:53 +08:00
|
|
|
final peerSearchTextController =
|
|
|
|
TextEditingController(text: peerSearchText.value);
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
class _PeersView extends StatefulWidget {
|
2022-09-01 21:18:29 +08:00
|
|
|
final Peers peers;
|
2022-10-10 20:10:31 +08:00
|
|
|
final PeerFilter? peerFilter;
|
2022-09-22 15:35:46 +08:00
|
|
|
final PeerCardBuilder peerCardBuilder;
|
2022-08-23 17:52:53 +08:00
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
const _PeersView(
|
2022-10-10 20:10:31 +08:00
|
|
|
{required this.peers,
|
|
|
|
required this.peerCardBuilder,
|
|
|
|
this.peerFilter,
|
|
|
|
Key? key})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(key: key);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
|
|
|
@override
|
2022-09-22 15:35:46 +08:00
|
|
|
_PeersViewState createState() => _PeersViewState();
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// State for the peer widget.
|
2024-07-23 23:28:03 +08:00
|
|
|
class _PeersViewState extends State<_PeersView>
|
|
|
|
with WindowListener, WidgetsBindingObserver {
|
2022-07-27 22:56:28 +08:00
|
|
|
static const int _maxQueryCount = 3;
|
2023-03-11 02:37:49 +08:00
|
|
|
final HashMap<String, String> _emptyMessages = HashMap.from({
|
|
|
|
LoadEvent.recent: 'empty_recent_tip',
|
|
|
|
LoadEvent.favorite: 'empty_favorite_tip',
|
|
|
|
LoadEvent.lan: 'empty_lan_tip',
|
|
|
|
LoadEvent.addressBook: 'empty_address_book_tip',
|
|
|
|
});
|
2024-03-28 11:38:11 +08:00
|
|
|
final space = (isDesktop || isWebDesktop) ? 12.0 : 8.0;
|
2022-09-01 21:18:29 +08:00
|
|
|
final _curPeers = <String>{};
|
2022-07-27 22:56:28 +08:00
|
|
|
var _lastChangeTime = DateTime.now();
|
2022-09-01 21:18:29 +08:00
|
|
|
var _lastQueryPeers = <String>{};
|
2024-07-23 10:26:36 +08:00
|
|
|
var _lastQueryTime = DateTime.now();
|
2023-03-09 11:54:17 +08:00
|
|
|
var _queryCount = 0;
|
2022-07-27 22:56:28 +08:00
|
|
|
var _exit = false;
|
2024-07-23 23:28:03 +08:00
|
|
|
bool _isActive = true;
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2023-09-26 19:58:51 +08:00
|
|
|
final _scrollController = ScrollController();
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
_PeersViewState() {
|
2022-07-27 22:56:28 +08:00
|
|
|
_startCheckOnlines();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
windowManager.addListener(this);
|
2024-07-23 23:28:03 +08:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2022-07-27 22:56:28 +08:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
windowManager.removeListener(this);
|
2024-07-23 23:28:03 +08:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2022-07-27 22:56:28 +08:00
|
|
|
_exit = true;
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onWindowFocus() {
|
2023-03-09 11:54:17 +08:00
|
|
|
_queryCount = 0;
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
@override
|
|
|
|
void onWindowMinimize() {
|
2023-03-09 11:54:17 +08:00
|
|
|
_queryCount = _maxQueryCount;
|
2022-08-03 15:31:19 +08:00
|
|
|
}
|
|
|
|
|
2024-07-23 23:28:03 +08:00
|
|
|
// This function is required for mobile.
|
|
|
|
// `onWindowFocus` works fine for desktop.
|
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
super.didChangeAppLifecycleState(state);
|
|
|
|
if (isDesktop) return;
|
|
|
|
if (state == AppLifecycleState.resumed) {
|
|
|
|
_isActive = true;
|
|
|
|
_queryCount = 0;
|
|
|
|
} else if (state == AppLifecycleState.inactive) {
|
|
|
|
_isActive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 22:56:28 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ChangeNotifierProvider<Peers>(
|
2022-09-01 21:18:29 +08:00
|
|
|
create: (context) => widget.peers,
|
2023-08-10 21:32:26 +08:00
|
|
|
child: Consumer<Peers>(builder: (context, peers, child) {
|
|
|
|
if (peers.peers.isEmpty) {
|
|
|
|
gFFI.peerTabModel.setCurrentTabCachedPeers([]);
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.sentiment_very_dissatisfied_rounded,
|
|
|
|
color: Theme.of(context).tabBarTheme.labelColor,
|
|
|
|
size: 40,
|
|
|
|
).paddingOnly(bottom: 10),
|
|
|
|
Text(
|
|
|
|
translate(
|
|
|
|
_emptyMessages[widget.peers.loadEvent] ?? 'Empty',
|
|
|
|
),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).tabBarTheme.labelColor,
|
|
|
|
),
|
2023-03-11 02:37:49 +08:00
|
|
|
),
|
2023-08-10 21:32:26 +08:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return _buildPeersView(peers);
|
|
|
|
}
|
|
|
|
}),
|
2022-07-27 22:56:28 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:07:56 +08:00
|
|
|
onVisibilityChanged(VisibilityInfo info) {
|
|
|
|
final peerId = _peerId((info.key as ValueKey).value);
|
|
|
|
if (info.visibleFraction > 0.00001) {
|
|
|
|
_curPeers.add(peerId);
|
|
|
|
} else {
|
|
|
|
_curPeers.remove(peerId);
|
|
|
|
}
|
|
|
|
_lastChangeTime = DateTime.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
String _cardId(String id) => widget.peers.name + id;
|
|
|
|
String _peerId(String cardId) => cardId.replaceAll(widget.peers.name, '');
|
|
|
|
|
2022-09-21 14:56:01 +08:00
|
|
|
Widget _buildPeersView(Peers peers) {
|
2023-06-20 23:34:05 +08:00
|
|
|
final updateEvent = peers.event;
|
2023-03-04 03:53:42 +08:00
|
|
|
final body = ObxValue<RxList>((filters) {
|
2022-09-21 14:56:01 +08:00
|
|
|
return FutureBuilder<List<Peer>>(
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
2023-09-25 11:19:30 +08:00
|
|
|
var peers = snapshot.data!;
|
|
|
|
if (peers.length > 1000) peers = peers.sublist(0, 1000);
|
2023-08-03 16:48:14 +08:00
|
|
|
gFFI.peerTabModel.setCurrentTabCachedPeers(peers);
|
2023-09-24 19:54:11 +08:00
|
|
|
buildOnePeer(Peer peer) {
|
|
|
|
final visibilityChild = VisibilityDetector(
|
|
|
|
key: ValueKey(_cardId(peer.id)),
|
|
|
|
onVisibilityChanged: onVisibilityChanged,
|
|
|
|
child: widget.peerCardBuilder(peer),
|
|
|
|
);
|
2023-10-27 15:58:54 +08:00
|
|
|
// `Provider.of<PeerTabModel>(context)` will causes infinete loop.
|
|
|
|
// Because `gFFI.peerTabModel.setCurrentTabCachedPeers(peers)` will trigger `notifyListeners()`.
|
|
|
|
//
|
|
|
|
// No need to listen the currentTab change event.
|
|
|
|
// Because the currentTab change event will trigger the peers change event,
|
|
|
|
// and the peers change event will trigger _buildPeersView().
|
2024-03-28 11:38:11 +08:00
|
|
|
return (isDesktop || isWebDesktop)
|
2024-05-01 23:38:39 +08:00
|
|
|
? Obx(() => peerCardUiType.value == PeerUiType.list
|
|
|
|
? Container(height: 45, child: visibilityChild)
|
|
|
|
: peerCardUiType.value == PeerUiType.grid
|
|
|
|
? SizedBox(
|
|
|
|
width: 220, height: 140, child: visibilityChild)
|
|
|
|
: SizedBox(
|
|
|
|
width: 220, height: 42, child: visibilityChild))
|
|
|
|
: Container(child: visibilityChild);
|
2023-09-24 19:54:11 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 19:58:51 +08:00
|
|
|
final Widget child;
|
|
|
|
if (isMobile) {
|
2024-05-01 23:38:39 +08:00
|
|
|
child = ListView.builder(
|
2023-09-26 19:58:51 +08:00
|
|
|
itemCount: peers.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
2024-05-01 23:38:39 +08:00
|
|
|
return buildOnePeer(peers[index]).marginOnly(
|
|
|
|
top: index == 0 ? 0 : space / 2, bottom: space / 2);
|
2023-09-26 19:58:51 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
2024-05-01 23:38:39 +08:00
|
|
|
child = Obx(() => peerCardUiType.value == PeerUiType.list
|
|
|
|
? DesktopScrollWrapper(
|
|
|
|
scrollController: _scrollController,
|
|
|
|
child: ListView.builder(
|
|
|
|
controller: _scrollController,
|
|
|
|
physics: DraggableNeverScrollableScrollPhysics(),
|
|
|
|
itemCount: peers.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
return buildOnePeer(peers[index]).marginOnly(
|
|
|
|
right: space,
|
|
|
|
top: index == 0 ? 0 : space / 2,
|
|
|
|
bottom: space / 2);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
: DesktopScrollWrapper(
|
|
|
|
scrollController: _scrollController,
|
|
|
|
child: DynamicGridView.builder(
|
|
|
|
controller: _scrollController,
|
|
|
|
physics: DraggableNeverScrollableScrollPhysics(),
|
|
|
|
gridDelegate: SliverGridDelegateWithWrapping(
|
|
|
|
mainAxisSpacing: space / 2,
|
|
|
|
crossAxisSpacing: space),
|
|
|
|
itemCount: peers.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
return buildOnePeer(peers[index]);
|
|
|
|
}),
|
|
|
|
));
|
2023-09-26 19:58:51 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 23:34:05 +08:00
|
|
|
if (updateEvent == UpdateEvent.load) {
|
|
|
|
_curPeers.clear();
|
|
|
|
_curPeers.addAll(peers.map((e) => e.id));
|
|
|
|
_queryOnlines(true);
|
|
|
|
}
|
|
|
|
return child;
|
2022-09-21 14:56:01 +08:00
|
|
|
} else {
|
|
|
|
return const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2023-03-04 03:53:42 +08:00
|
|
|
future: matchPeers(filters[0].value, filters[1].value, peers.peers),
|
2022-09-21 14:56:01 +08:00
|
|
|
);
|
2023-03-04 03:53:42 +08:00
|
|
|
}, obslist);
|
2022-09-21 14:56:01 +08:00
|
|
|
|
2022-09-28 11:20:57 +08:00
|
|
|
return body;
|
2022-09-21 14:56:01 +08:00
|
|
|
}
|
|
|
|
|
2024-07-23 10:26:36 +08:00
|
|
|
var _queryInterval = const Duration(seconds: 20);
|
2023-06-20 23:34:05 +08:00
|
|
|
|
2022-07-27 22:56:28 +08:00
|
|
|
void _startCheckOnlines() {
|
|
|
|
() async {
|
2024-07-23 10:26:36 +08:00
|
|
|
final p = await bind.mainIsUsingPublicServer();
|
|
|
|
if (!p) {
|
|
|
|
_queryInterval = const Duration(seconds: 6);
|
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
while (!_exit) {
|
|
|
|
final now = DateTime.now();
|
|
|
|
if (!setEquals(_curPeers, _lastQueryPeers)) {
|
2022-09-15 13:22:23 +08:00
|
|
|
if (now.difference(_lastChangeTime) > const Duration(seconds: 1)) {
|
2023-06-20 23:34:05 +08:00
|
|
|
_queryOnlines(false);
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-23 23:28:03 +08:00
|
|
|
if (_isActive && (_queryCount < _maxQueryCount || !p)) {
|
2023-06-20 23:34:05 +08:00
|
|
|
if (now.difference(_lastQueryTime) >= _queryInterval) {
|
2022-09-15 13:22:23 +08:00
|
|
|
if (_curPeers.isNotEmpty) {
|
2023-11-16 10:15:51 +08:00
|
|
|
bind.queryOnlines(ids: _curPeers.toList(growable: false));
|
2022-08-03 15:31:19 +08:00
|
|
|
_lastQueryTime = DateTime.now();
|
2023-03-09 11:54:17 +08:00
|
|
|
_queryCount += 1;
|
2022-08-03 15:31:19 +08:00
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 13:22:23 +08:00
|
|
|
await Future.delayed(const Duration(milliseconds: 300));
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
}
|
2022-10-10 20:10:31 +08:00
|
|
|
|
2023-06-20 23:34:05 +08:00
|
|
|
_queryOnlines(bool isLoadEvent) {
|
|
|
|
if (_curPeers.isNotEmpty) {
|
2023-11-16 10:15:51 +08:00
|
|
|
bind.queryOnlines(ids: _curPeers.toList(growable: false));
|
2023-06-20 23:34:05 +08:00
|
|
|
_queryCount = 0;
|
|
|
|
}
|
2024-07-23 23:28:03 +08:00
|
|
|
_lastQueryPeers = {..._curPeers};
|
|
|
|
if (isLoadEvent) {
|
|
|
|
_lastChangeTime = DateTime.now();
|
|
|
|
} else {
|
|
|
|
_lastQueryTime = DateTime.now().subtract(_queryInterval);
|
|
|
|
}
|
2023-06-20 23:34:05 +08:00
|
|
|
}
|
|
|
|
|
2023-03-04 03:53:42 +08:00
|
|
|
Future<List<Peer>>? matchPeers(
|
|
|
|
String searchText, String sortedBy, List<Peer> peers) async {
|
2022-10-10 20:10:31 +08:00
|
|
|
if (widget.peerFilter != null) {
|
|
|
|
peers = peers.where((peer) => widget.peerFilter!(peer)).toList();
|
|
|
|
}
|
|
|
|
|
2023-03-04 03:53:42 +08:00
|
|
|
// fallback to id sorting
|
2023-03-09 03:10:08 +08:00
|
|
|
if (!PeerSortType.values.contains(sortedBy)) {
|
|
|
|
sortedBy = PeerSortType.remoteId;
|
2023-08-10 22:27:35 +08:00
|
|
|
bind.setLocalFlutterOption(
|
2024-05-18 23:13:54 +08:00
|
|
|
k: kOptionPeerSorting,
|
2023-03-04 03:53:42 +08:00
|
|
|
v: sortedBy,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-11 02:37:49 +08:00
|
|
|
if (widget.peers.loadEvent != LoadEvent.recent) {
|
2023-03-04 03:53:42 +08:00
|
|
|
switch (sortedBy) {
|
2023-03-09 03:10:08 +08:00
|
|
|
case PeerSortType.remoteId:
|
2023-03-10 15:25:19 +08:00
|
|
|
peers.sort((p1, p2) => p1.getId().compareTo(p2.getId()));
|
2023-03-04 03:53:42 +08:00
|
|
|
break;
|
2023-03-09 03:10:08 +08:00
|
|
|
case PeerSortType.remoteHost:
|
|
|
|
peers.sort((p1, p2) =>
|
|
|
|
p1.hostname.toLowerCase().compareTo(p2.hostname.toLowerCase()));
|
|
|
|
break;
|
|
|
|
case PeerSortType.username:
|
2023-03-04 03:53:42 +08:00
|
|
|
peers.sort((p1, p2) =>
|
|
|
|
p1.username.toLowerCase().compareTo(p2.username.toLowerCase()));
|
|
|
|
break;
|
2023-03-10 15:25:19 +08:00
|
|
|
// case PeerSortType.status:
|
|
|
|
// peers.sort((p1, p2) => p1.online ? -1 : 1);
|
|
|
|
// break;
|
2023-03-04 03:53:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:10:31 +08:00
|
|
|
searchText = searchText.trim();
|
|
|
|
if (searchText.isEmpty) {
|
|
|
|
return peers;
|
|
|
|
}
|
|
|
|
searchText = searchText.toLowerCase();
|
|
|
|
final matches =
|
|
|
|
await Future.wait(peers.map((peer) => matchPeer(searchText, peer)));
|
|
|
|
final filteredList = List<Peer>.empty(growable: true);
|
|
|
|
for (var i = 0; i < peers.length; i++) {
|
|
|
|
if (matches[i]) {
|
|
|
|
filteredList.add(peers[i]);
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 03:53:42 +08:00
|
|
|
|
2022-10-10 20:10:31 +08:00
|
|
|
return filteredList;
|
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
abstract class BasePeersView extends StatelessWidget {
|
2022-09-01 21:18:29 +08:00
|
|
|
final String name;
|
|
|
|
final String loadEvent;
|
2022-10-10 20:10:31 +08:00
|
|
|
final PeerFilter? peerFilter;
|
2022-09-22 15:35:46 +08:00
|
|
|
final PeerCardBuilder peerCardBuilder;
|
2024-03-20 15:05:54 +08:00
|
|
|
final GetInitPeers? getInitPeers;
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
const BasePeersView({
|
2022-09-01 21:18:29 +08:00
|
|
|
Key? key,
|
|
|
|
required this.name,
|
|
|
|
required this.loadEvent,
|
2022-10-10 20:10:31 +08:00
|
|
|
this.peerFilter,
|
2022-09-22 15:35:46 +08:00
|
|
|
required this.peerCardBuilder,
|
2024-03-20 15:05:54 +08:00
|
|
|
required this.getInitPeers,
|
2022-09-01 21:18:29 +08:00
|
|
|
}) : super(key: key);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-09-22 15:35:46 +08:00
|
|
|
return _PeersView(
|
2024-03-20 15:05:54 +08:00
|
|
|
peers:
|
|
|
|
Peers(name: name, loadEvent: loadEvent, getInitPeers: getInitPeers),
|
2022-10-10 20:10:31 +08:00
|
|
|
peerFilter: peerFilter,
|
2022-09-22 15:35:46 +08:00
|
|
|
peerCardBuilder: peerCardBuilder);
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
class RecentPeersView extends BasePeersView {
|
2022-09-28 11:20:57 +08:00
|
|
|
RecentPeersView(
|
|
|
|
{Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'recent peer',
|
2023-03-11 02:37:49 +08:00
|
|
|
loadEvent: LoadEvent.recent,
|
2022-09-22 15:35:46 +08:00
|
|
|
peerCardBuilder: (Peer peer) => RecentPeerCard(
|
2022-09-01 21:18:29 +08:00
|
|
|
peer: peer,
|
2022-09-23 12:20:40 +08:00
|
|
|
menuPadding: menuPadding,
|
2022-09-01 21:18:29 +08:00
|
|
|
),
|
2024-03-20 15:05:54 +08:00
|
|
|
getInitPeers: null,
|
2022-08-23 17:21:50 +08:00
|
|
|
);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-08-02 13:10:09 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final widget = super.build(context);
|
2022-08-03 22:03:31 +08:00
|
|
|
bind.mainLoadRecentPeers();
|
2022-08-02 13:10:09 +08:00
|
|
|
return widget;
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
class FavoritePeersView extends BasePeersView {
|
2022-09-28 11:20:57 +08:00
|
|
|
FavoritePeersView(
|
|
|
|
{Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'favorite peer',
|
2023-03-11 02:37:49 +08:00
|
|
|
loadEvent: LoadEvent.favorite,
|
2022-09-22 15:35:46 +08:00
|
|
|
peerCardBuilder: (Peer peer) => FavoritePeerCard(
|
2022-09-01 21:18:29 +08:00
|
|
|
peer: peer,
|
2022-09-23 12:20:40 +08:00
|
|
|
menuPadding: menuPadding,
|
2022-09-01 21:18:29 +08:00
|
|
|
),
|
2024-03-20 15:05:54 +08:00
|
|
|
getInitPeers: null,
|
2022-09-01 21:18:29 +08:00
|
|
|
);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
|
|
|
@override
|
2022-08-02 13:10:09 +08:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final widget = super.build(context);
|
2022-08-03 22:03:31 +08:00
|
|
|
bind.mainLoadFavPeers();
|
2022-08-02 13:10:09 +08:00
|
|
|
return widget;
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
class DiscoveredPeersView extends BasePeersView {
|
2022-09-28 11:20:57 +08:00
|
|
|
DiscoveredPeersView(
|
|
|
|
{Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'discovered peer',
|
2023-03-11 02:37:49 +08:00
|
|
|
loadEvent: LoadEvent.lan,
|
2022-09-22 15:35:46 +08:00
|
|
|
peerCardBuilder: (Peer peer) => DiscoveredPeerCard(
|
2022-09-01 21:18:29 +08:00
|
|
|
peer: peer,
|
2022-09-23 12:20:40 +08:00
|
|
|
menuPadding: menuPadding,
|
2022-09-01 21:18:29 +08:00
|
|
|
),
|
2024-03-20 15:05:54 +08:00
|
|
|
getInitPeers: null,
|
2022-09-01 21:18:29 +08:00
|
|
|
);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-08-02 13:10:09 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final widget = super.build(context);
|
2022-08-03 22:03:31 +08:00
|
|
|
bind.mainLoadLanPeers();
|
2022-08-02 13:10:09 +08:00
|
|
|
return widget;
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
class AddressBookPeersView extends BasePeersView {
|
2022-09-28 11:20:57 +08:00
|
|
|
AddressBookPeersView(
|
2022-10-08 15:53:03 +08:00
|
|
|
{Key? key,
|
|
|
|
EdgeInsets? menuPadding,
|
|
|
|
ScrollController? scrollController,
|
2024-03-20 15:05:54 +08:00
|
|
|
required GetInitPeers getInitPeers})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'address book peer',
|
2023-03-11 02:37:49 +08:00
|
|
|
loadEvent: LoadEvent.addressBook,
|
2022-10-10 20:10:31 +08:00
|
|
|
peerFilter: (Peer peer) =>
|
|
|
|
_hitTag(gFFI.abModel.selectedTags, peer.tags),
|
|
|
|
peerCardBuilder: (Peer peer) => AddressBookPeerCard(
|
|
|
|
peer: peer,
|
|
|
|
menuPadding: menuPadding,
|
|
|
|
),
|
2024-03-20 15:05:54 +08:00
|
|
|
getInitPeers: getInitPeers,
|
2022-09-01 21:18:29 +08:00
|
|
|
);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-09-01 21:18:29 +08:00
|
|
|
static bool _hitTag(List<dynamic> selectedTags, List<dynamic> idents) {
|
2022-07-27 22:56:28 +08:00
|
|
|
if (selectedTags.isEmpty) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-11 16:50:48 +08:00
|
|
|
if (gFFI.abModel.filterByIntersection.value) {
|
|
|
|
for (final tag in selectedTags) {
|
|
|
|
if (!idents.contains(tag)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
for (final tag in selectedTags) {
|
|
|
|
if (idents.contains(tag)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
2023-10-11 16:50:48 +08:00
|
|
|
return false;
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 21:40:35 +08:00
|
|
|
|
|
|
|
class MyGroupPeerView extends BasePeersView {
|
|
|
|
MyGroupPeerView(
|
|
|
|
{Key? key,
|
|
|
|
EdgeInsets? menuPadding,
|
|
|
|
ScrollController? scrollController,
|
2024-03-20 15:05:54 +08:00
|
|
|
required GetInitPeers getInitPeers})
|
2022-12-11 21:40:35 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
2023-09-14 10:17:03 +08:00
|
|
|
name: 'group peer',
|
|
|
|
loadEvent: LoadEvent.group,
|
2023-06-21 09:30:32 +08:00
|
|
|
peerFilter: filter,
|
2022-12-11 21:40:35 +08:00
|
|
|
peerCardBuilder: (Peer peer) => MyGroupPeerCard(
|
|
|
|
peer: peer,
|
|
|
|
menuPadding: menuPadding,
|
|
|
|
),
|
2024-03-20 15:05:54 +08:00
|
|
|
getInitPeers: getInitPeers,
|
2022-12-11 21:40:35 +08:00
|
|
|
);
|
2023-06-21 09:30:32 +08:00
|
|
|
|
|
|
|
static bool filter(Peer peer) {
|
|
|
|
if (gFFI.groupModel.searchUserText.isNotEmpty) {
|
2023-09-14 10:17:03 +08:00
|
|
|
if (!peer.loginName.contains(gFFI.groupModel.searchUserText)) {
|
2023-06-21 09:30:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gFFI.groupModel.selectedUser.isNotEmpty) {
|
2023-09-14 10:17:03 +08:00
|
|
|
if (gFFI.groupModel.selectedUser.value != peer.loginName) {
|
2023-06-21 09:30:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-11 21:40:35 +08:00
|
|
|
}
|