peers_view.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. import 'dart:async';
  2. import 'dart:collection';
  3. import 'package:dynamic_layouts/dynamic_layouts.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_hbb/consts.dart';
  7. import 'package:flutter_hbb/desktop/widgets/scroll_wrapper.dart';
  8. import 'package:flutter_hbb/models/peer_tab_model.dart';
  9. import 'package:flutter_hbb/models/state_model.dart';
  10. import 'package:get/get.dart';
  11. import 'package:provider/provider.dart';
  12. import 'package:visibility_detector/visibility_detector.dart';
  13. import 'package:window_manager/window_manager.dart';
  14. import '../../common.dart';
  15. import '../../models/peer_model.dart';
  16. import '../../models/platform_model.dart';
  17. import 'peer_card.dart';
  18. typedef PeerFilter = bool Function(Peer peer);
  19. typedef PeerCardBuilder = Widget Function(Peer peer);
  20. class PeerSortType {
  21. static const String remoteId = 'Remote ID';
  22. static const String remoteHost = 'Remote Host';
  23. static const String username = 'Username';
  24. // static const String status = 'Status';
  25. static List<String> values = [
  26. PeerSortType.remoteId,
  27. PeerSortType.remoteHost,
  28. PeerSortType.username,
  29. // PeerSortType.status
  30. ];
  31. }
  32. class LoadEvent {
  33. static const String recent = 'load_recent_peers';
  34. static const String favorite = 'load_fav_peers';
  35. static const String lan = 'load_lan_peers';
  36. static const String addressBook = 'load_address_book_peers';
  37. static const String group = 'load_group_peers';
  38. }
  39. class PeersModelName {
  40. static const String recent = 'recent peer';
  41. static const String favorite = 'fav peer';
  42. static const String lan = 'discovered peer';
  43. static const String addressBook = 'address book peer';
  44. static const String group = 'group peer';
  45. }
  46. /// for peer search text, global obs value
  47. final peerSearchText = "".obs;
  48. /// for peer sort, global obs value
  49. RxString? _peerSort;
  50. RxString get peerSort {
  51. _peerSort ??= bind.getLocalFlutterOption(k: kOptionPeerSorting).obs;
  52. return _peerSort!;
  53. }
  54. // list for listener
  55. RxList<RxString> get obslist => [peerSearchText, peerSort].obs;
  56. final peerSearchTextController =
  57. TextEditingController(text: peerSearchText.value);
  58. class _PeersView extends StatefulWidget {
  59. final Peers peers;
  60. final PeerFilter? peerFilter;
  61. final PeerCardBuilder peerCardBuilder;
  62. const _PeersView(
  63. {required this.peers,
  64. required this.peerCardBuilder,
  65. this.peerFilter,
  66. Key? key})
  67. : super(key: key);
  68. @override
  69. _PeersViewState createState() => _PeersViewState();
  70. }
  71. /// State for the peer widget.
  72. class _PeersViewState extends State<_PeersView>
  73. with WindowListener, WidgetsBindingObserver {
  74. static const int _maxQueryCount = 3;
  75. final HashMap<String, String> _emptyMessages = HashMap.from({
  76. LoadEvent.recent: 'empty_recent_tip',
  77. LoadEvent.favorite: 'empty_favorite_tip',
  78. LoadEvent.lan: 'empty_lan_tip',
  79. LoadEvent.addressBook: 'empty_address_book_tip',
  80. });
  81. final space = (isDesktop || isWebDesktop) ? 12.0 : 8.0;
  82. final _curPeers = <String>{};
  83. var _lastChangeTime = DateTime.now();
  84. var _lastQueryPeers = <String>{};
  85. var _lastQueryTime = DateTime.now();
  86. var _lastWindowRestoreTime = DateTime.now();
  87. var _queryCount = 0;
  88. var _exit = false;
  89. bool _isActive = true;
  90. final _scrollController = ScrollController();
  91. _PeersViewState() {
  92. _startCheckOnlines();
  93. }
  94. @override
  95. void initState() {
  96. windowManager.addListener(this);
  97. WidgetsBinding.instance.addObserver(this);
  98. super.initState();
  99. }
  100. @override
  101. void dispose() {
  102. windowManager.removeListener(this);
  103. WidgetsBinding.instance.removeObserver(this);
  104. _exit = true;
  105. super.dispose();
  106. }
  107. @override
  108. void onWindowFocus() {
  109. _queryCount = 0;
  110. _isActive = true;
  111. }
  112. @override
  113. void onWindowBlur() {
  114. // We need this comparison because window restore (on Windows) also triggers `onWindowBlur()`.
  115. // Maybe it's a bug of the window manager, but the source code seems to be correct.
  116. //
  117. // Although `onWindowRestore()` is called after `onWindowBlur()` in my test,
  118. // we need the following comparison to ensure that `_isActive` is true in the end.
  119. if (isWindows &&
  120. DateTime.now().difference(_lastWindowRestoreTime) <
  121. const Duration(milliseconds: 300)) {
  122. return;
  123. }
  124. _queryCount = _maxQueryCount;
  125. _isActive = false;
  126. }
  127. @override
  128. void onWindowRestore() {
  129. // Window restore (on MacOS and Linux) also triggers `onWindowFocus()`.
  130. // But on Windows, it triggers `onWindowBlur()`, mybe it's a bug of the window manager.
  131. if (!isWindows) return;
  132. _queryCount = 0;
  133. _isActive = true;
  134. _lastWindowRestoreTime = DateTime.now();
  135. }
  136. @override
  137. void onWindowMinimize() {
  138. // Window minimize also triggers `onWindowBlur()`.
  139. }
  140. // This function is required for mobile.
  141. // `onWindowFocus` works fine for desktop.
  142. @override
  143. void didChangeAppLifecycleState(AppLifecycleState state) {
  144. super.didChangeAppLifecycleState(state);
  145. if (isDesktop || isWebDesktop) return;
  146. if (state == AppLifecycleState.resumed) {
  147. _isActive = true;
  148. _queryCount = 0;
  149. } else if (state == AppLifecycleState.inactive) {
  150. _isActive = false;
  151. }
  152. }
  153. @override
  154. Widget build(BuildContext context) {
  155. // We should avoid too many rebuilds. MacOS(m1, 14.6.1) on Flutter 3.19.6.
  156. // Continious rebuilds of `ChangeNotifierProvider` will cause memory leak.
  157. // Simple demo can reproduce this issue.
  158. return ChangeNotifierProvider<Peers>.value(
  159. value: widget.peers,
  160. child: Consumer<Peers>(builder: (context, peers, child) {
  161. if (peers.peers.isEmpty) {
  162. gFFI.peerTabModel.setCurrentTabCachedPeers([]);
  163. return Center(
  164. child: Column(
  165. mainAxisAlignment: MainAxisAlignment.center,
  166. children: [
  167. Icon(
  168. Icons.sentiment_very_dissatisfied_rounded,
  169. color: Theme.of(context).tabBarTheme.labelColor,
  170. size: 40,
  171. ).paddingOnly(bottom: 10),
  172. Text(
  173. translate(
  174. _emptyMessages[widget.peers.loadEvent] ?? 'Empty',
  175. ),
  176. textAlign: TextAlign.center,
  177. style: TextStyle(
  178. color: Theme.of(context).tabBarTheme.labelColor,
  179. ),
  180. ),
  181. ],
  182. ),
  183. );
  184. } else {
  185. return _buildPeersView(peers);
  186. }
  187. }),
  188. );
  189. }
  190. onVisibilityChanged(VisibilityInfo info) {
  191. final peerId = _peerId((info.key as ValueKey).value);
  192. if (info.visibleFraction > 0.00001) {
  193. _curPeers.add(peerId);
  194. } else {
  195. _curPeers.remove(peerId);
  196. }
  197. _lastChangeTime = DateTime.now();
  198. }
  199. String _cardId(String id) => widget.peers.name + id;
  200. String _peerId(String cardId) => cardId.replaceAll(widget.peers.name, '');
  201. Widget _buildPeersView(Peers peers) {
  202. final updateEvent = peers.event;
  203. final body = ObxValue<RxList>((filters) {
  204. return FutureBuilder<List<Peer>>(
  205. builder: (context, snapshot) {
  206. if (snapshot.hasData) {
  207. var peers = snapshot.data!;
  208. if (peers.length > 1000) peers = peers.sublist(0, 1000);
  209. gFFI.peerTabModel.setCurrentTabCachedPeers(peers);
  210. buildOnePeer(Peer peer, bool isPortrait) {
  211. final visibilityChild = VisibilityDetector(
  212. key: ValueKey(_cardId(peer.id)),
  213. onVisibilityChanged: onVisibilityChanged,
  214. child: widget.peerCardBuilder(peer),
  215. );
  216. // `Provider.of<PeerTabModel>(context)` will causes infinete loop.
  217. // Because `gFFI.peerTabModel.setCurrentTabCachedPeers(peers)` will trigger `notifyListeners()`.
  218. //
  219. // No need to listen the currentTab change event.
  220. // Because the currentTab change event will trigger the peers change event,
  221. // and the peers change event will trigger _buildPeersView().
  222. return !isPortrait
  223. ? Obx(() => peerCardUiType.value == PeerUiType.list
  224. ? Container(height: 45, child: visibilityChild)
  225. : peerCardUiType.value == PeerUiType.grid
  226. ? SizedBox(
  227. width: 220, height: 140, child: visibilityChild)
  228. : SizedBox(
  229. width: 220, height: 42, child: visibilityChild))
  230. : Container(child: visibilityChild);
  231. }
  232. // We should avoid too many rebuilds. Win10(Some machines) on Flutter 3.19.6.
  233. // Continious rebuilds of `ListView.builder` will cause memory leak.
  234. // Simple demo can reproduce this issue.
  235. final Widget child = Obx(() => stateGlobal.isPortrait.isTrue
  236. ? ListView.builder(
  237. itemCount: peers.length,
  238. itemBuilder: (BuildContext context, int index) {
  239. return buildOnePeer(peers[index], true).marginOnly(
  240. top: index == 0 ? 0 : space / 2, bottom: space / 2);
  241. },
  242. )
  243. : peerCardUiType.value == PeerUiType.list
  244. ? DesktopScrollWrapper(
  245. scrollController: _scrollController,
  246. child: ListView.builder(
  247. controller: _scrollController,
  248. physics: DraggableNeverScrollableScrollPhysics(),
  249. itemCount: peers.length,
  250. itemBuilder: (BuildContext context, int index) {
  251. return buildOnePeer(peers[index], false)
  252. .marginOnly(
  253. right: space,
  254. top: index == 0 ? 0 : space / 2,
  255. bottom: space / 2);
  256. }),
  257. )
  258. : DesktopScrollWrapper(
  259. scrollController: _scrollController,
  260. child: DynamicGridView.builder(
  261. controller: _scrollController,
  262. physics: DraggableNeverScrollableScrollPhysics(),
  263. gridDelegate: SliverGridDelegateWithWrapping(
  264. mainAxisSpacing: space / 2,
  265. crossAxisSpacing: space),
  266. itemCount: peers.length,
  267. itemBuilder: (BuildContext context, int index) {
  268. return buildOnePeer(peers[index], false);
  269. }),
  270. ));
  271. if (updateEvent == UpdateEvent.load) {
  272. _curPeers.clear();
  273. _curPeers.addAll(peers.map((e) => e.id));
  274. _queryOnlines(true);
  275. }
  276. return child;
  277. } else {
  278. return const Center(
  279. child: CircularProgressIndicator(),
  280. );
  281. }
  282. },
  283. future: matchPeers(filters[0].value, filters[1].value, peers.peers),
  284. );
  285. }, obslist);
  286. return body;
  287. }
  288. var _queryInterval = const Duration(seconds: 20);
  289. void _startCheckOnlines() {
  290. () async {
  291. final p = await bind.mainIsUsingPublicServer();
  292. if (!p) {
  293. _queryInterval = const Duration(seconds: 6);
  294. }
  295. while (!_exit) {
  296. final now = DateTime.now();
  297. if (!setEquals(_curPeers, _lastQueryPeers)) {
  298. if (now.difference(_lastChangeTime) > const Duration(seconds: 1)) {
  299. _queryOnlines(false);
  300. }
  301. } else {
  302. final skipIfIsWeb =
  303. isWeb && !(stateGlobal.isWebVisible && stateGlobal.isInMainPage);
  304. final skipIfMobile =
  305. (isAndroid || isIOS) && !stateGlobal.isInMainPage;
  306. final skipIfNotActive = skipIfIsWeb || skipIfMobile || !_isActive;
  307. if (!skipIfNotActive && (_queryCount < _maxQueryCount || !p)) {
  308. if (now.difference(_lastQueryTime) >= _queryInterval) {
  309. if (_curPeers.isNotEmpty) {
  310. bind.queryOnlines(ids: _curPeers.toList(growable: false));
  311. _lastQueryTime = DateTime.now();
  312. _queryCount += 1;
  313. }
  314. }
  315. }
  316. }
  317. await Future.delayed(const Duration(milliseconds: 300));
  318. }
  319. }();
  320. }
  321. _queryOnlines(bool isLoadEvent) {
  322. if (_curPeers.isNotEmpty) {
  323. bind.queryOnlines(ids: _curPeers.toList(growable: false));
  324. _queryCount = 0;
  325. }
  326. _lastQueryPeers = {..._curPeers};
  327. if (isLoadEvent) {
  328. _lastChangeTime = DateTime.now();
  329. } else {
  330. _lastQueryTime = DateTime.now().subtract(_queryInterval);
  331. }
  332. }
  333. Future<List<Peer>>? matchPeers(
  334. String searchText, String sortedBy, List<Peer> peers) async {
  335. if (widget.peerFilter != null) {
  336. peers = peers.where((peer) => widget.peerFilter!(peer)).toList();
  337. }
  338. // fallback to id sorting
  339. if (!PeerSortType.values.contains(sortedBy)) {
  340. sortedBy = PeerSortType.remoteId;
  341. bind.setLocalFlutterOption(
  342. k: kOptionPeerSorting,
  343. v: sortedBy,
  344. );
  345. }
  346. if (widget.peers.loadEvent != LoadEvent.recent) {
  347. switch (sortedBy) {
  348. case PeerSortType.remoteId:
  349. peers.sort((p1, p2) => p1.getId().compareTo(p2.getId()));
  350. break;
  351. case PeerSortType.remoteHost:
  352. peers.sort((p1, p2) =>
  353. p1.hostname.toLowerCase().compareTo(p2.hostname.toLowerCase()));
  354. break;
  355. case PeerSortType.username:
  356. peers.sort((p1, p2) =>
  357. p1.username.toLowerCase().compareTo(p2.username.toLowerCase()));
  358. break;
  359. // case PeerSortType.status:
  360. // peers.sort((p1, p2) => p1.online ? -1 : 1);
  361. // break;
  362. }
  363. }
  364. searchText = searchText.trim();
  365. if (searchText.isEmpty) {
  366. return peers;
  367. }
  368. searchText = searchText.toLowerCase();
  369. final matches =
  370. await Future.wait(peers.map((peer) => matchPeer(searchText, peer)));
  371. final filteredList = List<Peer>.empty(growable: true);
  372. for (var i = 0; i < peers.length; i++) {
  373. if (matches[i]) {
  374. filteredList.add(peers[i]);
  375. }
  376. }
  377. return filteredList;
  378. }
  379. }
  380. abstract class BasePeersView extends StatelessWidget {
  381. final PeerTabIndex peerTabIndex;
  382. final PeerFilter? peerFilter;
  383. final PeerCardBuilder peerCardBuilder;
  384. const BasePeersView({
  385. Key? key,
  386. required this.peerTabIndex,
  387. this.peerFilter,
  388. required this.peerCardBuilder,
  389. }) : super(key: key);
  390. @override
  391. Widget build(BuildContext context) {
  392. Peers peers;
  393. switch (peerTabIndex) {
  394. case PeerTabIndex.recent:
  395. peers = gFFI.recentPeersModel;
  396. break;
  397. case PeerTabIndex.fav:
  398. peers = gFFI.favoritePeersModel;
  399. break;
  400. case PeerTabIndex.lan:
  401. peers = gFFI.lanPeersModel;
  402. break;
  403. case PeerTabIndex.ab:
  404. peers = gFFI.abModel.peersModel;
  405. break;
  406. case PeerTabIndex.group:
  407. peers = gFFI.groupModel.peersModel;
  408. break;
  409. }
  410. return _PeersView(
  411. peers: peers, peerFilter: peerFilter, peerCardBuilder: peerCardBuilder);
  412. }
  413. }
  414. class RecentPeersView extends BasePeersView {
  415. RecentPeersView(
  416. {Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
  417. : super(
  418. key: key,
  419. peerTabIndex: PeerTabIndex.recent,
  420. peerCardBuilder: (Peer peer) => RecentPeerCard(
  421. peer: peer,
  422. menuPadding: menuPadding,
  423. ),
  424. );
  425. @override
  426. Widget build(BuildContext context) {
  427. final widget = super.build(context);
  428. bind.mainLoadRecentPeers();
  429. return widget;
  430. }
  431. }
  432. class FavoritePeersView extends BasePeersView {
  433. FavoritePeersView(
  434. {Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
  435. : super(
  436. key: key,
  437. peerTabIndex: PeerTabIndex.fav,
  438. peerCardBuilder: (Peer peer) => FavoritePeerCard(
  439. peer: peer,
  440. menuPadding: menuPadding,
  441. ),
  442. );
  443. @override
  444. Widget build(BuildContext context) {
  445. final widget = super.build(context);
  446. bind.mainLoadFavPeers();
  447. return widget;
  448. }
  449. }
  450. class DiscoveredPeersView extends BasePeersView {
  451. DiscoveredPeersView(
  452. {Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
  453. : super(
  454. key: key,
  455. peerTabIndex: PeerTabIndex.lan,
  456. peerCardBuilder: (Peer peer) => DiscoveredPeerCard(
  457. peer: peer,
  458. menuPadding: menuPadding,
  459. ),
  460. );
  461. @override
  462. Widget build(BuildContext context) {
  463. final widget = super.build(context);
  464. bind.mainLoadLanPeers();
  465. return widget;
  466. }
  467. }
  468. class AddressBookPeersView extends BasePeersView {
  469. AddressBookPeersView(
  470. {Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
  471. : super(
  472. key: key,
  473. peerTabIndex: PeerTabIndex.ab,
  474. peerFilter: (Peer peer) =>
  475. _hitTag(gFFI.abModel.selectedTags, peer.tags),
  476. peerCardBuilder: (Peer peer) => AddressBookPeerCard(
  477. peer: peer,
  478. menuPadding: menuPadding,
  479. ),
  480. );
  481. static bool _hitTag(List<dynamic> selectedTags, List<dynamic> idents) {
  482. if (selectedTags.isEmpty) {
  483. return true;
  484. }
  485. if (gFFI.abModel.filterByIntersection.value) {
  486. for (final tag in selectedTags) {
  487. if (!idents.contains(tag)) {
  488. return false;
  489. }
  490. }
  491. return true;
  492. } else {
  493. for (final tag in selectedTags) {
  494. if (idents.contains(tag)) {
  495. return true;
  496. }
  497. }
  498. return false;
  499. }
  500. }
  501. }
  502. class MyGroupPeerView extends BasePeersView {
  503. MyGroupPeerView(
  504. {Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
  505. : super(
  506. key: key,
  507. peerTabIndex: PeerTabIndex.group,
  508. peerFilter: filter,
  509. peerCardBuilder: (Peer peer) => MyGroupPeerCard(
  510. peer: peer,
  511. menuPadding: menuPadding,
  512. ),
  513. );
  514. static bool filter(Peer peer) {
  515. if (gFFI.groupModel.searchUserText.isNotEmpty) {
  516. if (!peer.loginName.contains(gFFI.groupModel.searchUserText)) {
  517. return false;
  518. }
  519. }
  520. if (gFFI.groupModel.selectedUser.isNotEmpty) {
  521. if (gFFI.groupModel.selectedUser.value != peer.loginName) {
  522. return false;
  523. }
  524. }
  525. return true;
  526. }
  527. }