2022-07-27 22:56:28 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-23 17:21:50 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-19 16:06:03 +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
|
|
|
|
|
|
|
typedef OffstageFunc = bool Function(Peer peer);
|
2022-09-22 15:35:46 +08:00
|
|
|
typedef PeerCardBuilder = BasePeerCard Function(Peer peer);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-08-23 17:52:53 +08:00
|
|
|
/// for peer search text, global obs value
|
|
|
|
final peerSearchText = "".obs;
|
|
|
|
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;
|
|
|
|
final OffstageFunc offstageFunc;
|
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-09-01 21:18:29 +08:00
|
|
|
{required this.peers,
|
|
|
|
required this.offstageFunc,
|
2022-09-22 15:35:46 +08:00
|
|
|
required this.peerCardBuilder,
|
2022-09-01 21:18:29 +08:00
|
|
|
Key? key})
|
|
|
|
: 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.
|
2022-09-22 15:35:46 +08:00
|
|
|
class _PeersViewState extends State<_PeersView> with WindowListener {
|
2022-07-27 22:56:28 +08:00
|
|
|
static const int _maxQueryCount = 3;
|
2022-09-19 20:26:39 +08:00
|
|
|
final space = isDesktop ? 12.0 : 8.0;
|
2022-09-01 21:18:29 +08:00
|
|
|
final _curPeers = <String>{};
|
2022-09-19 16:06:03 +08:00
|
|
|
final _scrollController = ScrollController();
|
2022-07-27 22:56:28 +08:00
|
|
|
var _lastChangeTime = DateTime.now();
|
2022-09-01 21:18:29 +08:00
|
|
|
var _lastQueryPeers = <String>{};
|
2022-09-15 13:22:23 +08:00
|
|
|
var _lastQueryTime = DateTime.now().subtract(const Duration(hours: 1));
|
2022-07-27 22:56:28 +08:00
|
|
|
var _queryCoun = 0;
|
|
|
|
var _exit = false;
|
|
|
|
|
2022-09-19 20:26:39 +08:00
|
|
|
late final mobileWidth = () {
|
|
|
|
const minWidth = 320.0;
|
|
|
|
final windowWidth = MediaQuery.of(context).size.width;
|
|
|
|
var width = windowWidth - 2 * space;
|
|
|
|
if (windowWidth > minWidth + 2 * space) {
|
|
|
|
final n = (windowWidth / (minWidth + 2 * space)).floor();
|
|
|
|
width = windowWidth / n - 2 * space;
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}();
|
|
|
|
|
2022-09-22 15:35:46 +08:00
|
|
|
_PeersViewState() {
|
2022-07-27 22:56:28 +08:00
|
|
|
_startCheckOnlines();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
windowManager.addListener(this);
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
windowManager.removeListener(this);
|
|
|
|
_exit = true;
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onWindowFocus() {
|
|
|
|
_queryCoun = 0;
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
@override
|
|
|
|
void onWindowMinimize() {
|
|
|
|
_queryCoun = _maxQueryCount;
|
|
|
|
}
|
|
|
|
|
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,
|
2022-08-23 17:52:53 +08:00
|
|
|
child: Consumer<Peers>(
|
2022-09-21 14:56:01 +08:00
|
|
|
builder: (context, peers, child) => peers.peers.isEmpty
|
|
|
|
? Center(
|
|
|
|
child: Text(translate("Empty")),
|
|
|
|
)
|
|
|
|
: _buildPeersView(peers)),
|
2022-07-27 22:56:28 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-21 14:56:01 +08:00
|
|
|
Widget _buildPeersView(Peers peers) {
|
|
|
|
final body = ObxValue<RxString>((searchText) {
|
|
|
|
return FutureBuilder<List<Peer>>(
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
final peers = snapshot.data!;
|
|
|
|
final cards = <Widget>[];
|
|
|
|
for (final peer in peers) {
|
|
|
|
final visibilityChild = VisibilityDetector(
|
|
|
|
key: ValueKey(peer.id),
|
|
|
|
onVisibilityChanged: (info) {
|
|
|
|
final peerId = (info.key as ValueKey).value;
|
|
|
|
if (info.visibleFraction > 0.00001) {
|
|
|
|
_curPeers.add(peerId);
|
|
|
|
} else {
|
|
|
|
_curPeers.remove(peerId);
|
|
|
|
}
|
|
|
|
_lastChangeTime = DateTime.now();
|
|
|
|
},
|
2022-09-22 15:35:46 +08:00
|
|
|
child: widget.peerCardBuilder(peer),
|
2022-09-21 14:56:01 +08:00
|
|
|
);
|
|
|
|
cards.add(Offstage(
|
|
|
|
key: ValueKey("off${peer.id}"),
|
|
|
|
offstage: widget.offstageFunc(peer),
|
|
|
|
child: isDesktop
|
|
|
|
? Obx(
|
|
|
|
() => SizedBox(
|
|
|
|
width: 220,
|
|
|
|
height: peerCardUiType.value == PeerUiType.grid
|
|
|
|
? 140
|
|
|
|
: 42,
|
|
|
|
child: visibilityChild,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: SizedBox(width: mobileWidth, child: visibilityChild)));
|
|
|
|
}
|
|
|
|
return Wrap(spacing: space, runSpacing: space, children: cards);
|
|
|
|
} else {
|
|
|
|
return const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
future: matchPeers(searchText.value, peers.peers),
|
|
|
|
);
|
|
|
|
}, peerSearchText);
|
|
|
|
|
|
|
|
if (isDesktop) {
|
|
|
|
return DesktopScrollWrapper(
|
|
|
|
scrollController: _scrollController,
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
physics: NeverScrollableScrollPhysics(),
|
|
|
|
controller: _scrollController,
|
|
|
|
child: body),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return SingleChildScrollView(
|
|
|
|
physics: BouncingScrollPhysics(),
|
|
|
|
controller: _scrollController,
|
|
|
|
child: body,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 22:56:28 +08:00
|
|
|
// ignore: todo
|
|
|
|
// TODO: variables walk through async tasks?
|
|
|
|
void _startCheckOnlines() {
|
|
|
|
() async {
|
|
|
|
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)) {
|
|
|
|
if (_curPeers.isNotEmpty) {
|
2022-08-03 22:03:31 +08:00
|
|
|
platformFFI.ffiBind
|
2022-08-03 15:31:19 +08:00
|
|
|
.queryOnlines(ids: _curPeers.toList(growable: false));
|
|
|
|
_lastQueryPeers = {..._curPeers};
|
|
|
|
_lastQueryTime = DateTime.now();
|
|
|
|
_queryCoun = 0;
|
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_queryCoun < _maxQueryCount) {
|
2022-09-15 13:22:23 +08:00
|
|
|
if (now.difference(_lastQueryTime) > const Duration(seconds: 20)) {
|
|
|
|
if (_curPeers.isNotEmpty) {
|
2022-08-03 22:03:31 +08:00
|
|
|
platformFFI.ffiBind
|
2022-08-03 15:31:19 +08:00
|
|
|
.queryOnlines(ids: _curPeers.toList(growable: false));
|
|
|
|
_lastQueryTime = DateTime.now();
|
|
|
|
_queryCoun += 1;
|
|
|
|
}
|
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-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;
|
|
|
|
final OffstageFunc offstageFunc;
|
2022-09-22 15:35:46 +08:00
|
|
|
final PeerCardBuilder peerCardBuilder;
|
2022-09-01 21:18:29 +08:00
|
|
|
final List<Peer> initPeers;
|
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,
|
|
|
|
required this.offstageFunc,
|
2022-09-22 15:35:46 +08:00
|
|
|
required this.peerCardBuilder,
|
2022-09-01 21:18:29 +08:00
|
|
|
required this.initPeers,
|
|
|
|
}) : 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(
|
2022-09-01 21:18:29 +08:00
|
|
|
peers: Peers(name: name, loadEvent: loadEvent, peers: initPeers),
|
|
|
|
offstageFunc: offstageFunc,
|
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-23 12:20:40 +08:00
|
|
|
RecentPeersView({Key? key, EdgeInsets? menuPadding})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'recent peer',
|
|
|
|
loadEvent: 'load_recent_peers',
|
|
|
|
offstageFunc: (Peer peer) => false,
|
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
|
|
|
),
|
|
|
|
initPeers: [],
|
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-23 12:20:40 +08:00
|
|
|
FavoritePeersView({Key? key, EdgeInsets? menuPadding})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'favorite peer',
|
|
|
|
loadEvent: 'load_fav_peers',
|
|
|
|
offstageFunc: (Peer peer) => false,
|
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
|
|
|
),
|
|
|
|
initPeers: [],
|
|
|
|
);
|
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-23 12:20:40 +08:00
|
|
|
DiscoveredPeersView({Key? key, EdgeInsets? menuPadding})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'discovered peer',
|
|
|
|
loadEvent: 'load_lan_peers',
|
|
|
|
offstageFunc: (Peer peer) => false,
|
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
|
|
|
),
|
|
|
|
initPeers: [],
|
|
|
|
);
|
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-23 12:20:40 +08:00
|
|
|
AddressBookPeersView({Key? key, EdgeInsets? menuPadding})
|
2022-09-01 21:18:29 +08:00
|
|
|
: super(
|
|
|
|
key: key,
|
|
|
|
name: 'address book peer',
|
|
|
|
loadEvent: 'load_address_book_peers',
|
|
|
|
offstageFunc: (Peer peer) =>
|
|
|
|
!_hitTag(gFFI.abModel.selectedTags, peer.tags),
|
2022-09-22 15:35:46 +08:00
|
|
|
peerCardBuilder: (Peer peer) => AddressBookPeerCard(
|
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
|
|
|
),
|
|
|
|
initPeers: _loadPeers(),
|
|
|
|
);
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-09-01 21:18:29 +08:00
|
|
|
static List<Peer> _loadPeers() {
|
2022-09-19 20:26:39 +08:00
|
|
|
debugPrint("_loadPeers : ${gFFI.abModel.peers.toString()}");
|
2022-07-27 22:56:28 +08:00
|
|
|
return gFFI.abModel.peers.map((e) {
|
2022-09-21 21:20:19 +08:00
|
|
|
return Peer.fromJson(e);
|
2022-07-27 22:56:28 +08:00
|
|
|
}).toList();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (idents.isEmpty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (final tag in selectedTags) {
|
|
|
|
if (!idents.contains(tag)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-15 13:22:23 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final widget = super.build(context);
|
2022-09-19 20:26:39 +08:00
|
|
|
// gFFI.abModel.updateAb();
|
2022-09-15 13:22:23 +08:00
|
|
|
return widget;
|
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|