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';
|
|
|
|
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-07-27 22:56:28 +08:00
|
|
|
import 'peercard_widget.dart';
|
|
|
|
|
|
|
|
typedef OffstageFunc = bool Function(Peer peer);
|
|
|
|
typedef PeerCardWidgetFunc = Widget Function(Peer peer);
|
|
|
|
|
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-07-27 22:56:28 +08:00
|
|
|
class _PeerWidget extends StatefulWidget {
|
|
|
|
late final _peers;
|
|
|
|
late final OffstageFunc _offstageFunc;
|
|
|
|
late final PeerCardWidgetFunc _peerCardWidgetFunc;
|
2022-08-23 17:52:53 +08:00
|
|
|
|
2022-08-02 13:10:09 +08:00
|
|
|
_PeerWidget(Peers peers, OffstageFunc offstageFunc,
|
2022-07-27 22:56:28 +08:00
|
|
|
PeerCardWidgetFunc peerCardWidgetFunc,
|
|
|
|
{Key? key})
|
|
|
|
: super(key: key) {
|
|
|
|
_peers = peers;
|
|
|
|
_offstageFunc = offstageFunc;
|
|
|
|
_peerCardWidgetFunc = peerCardWidgetFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
_PeerWidgetState createState() => _PeerWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// State for the peer widget.
|
|
|
|
class _PeerWidgetState extends State<_PeerWidget> with WindowListener {
|
|
|
|
static const int _maxQueryCount = 3;
|
|
|
|
|
|
|
|
var _curPeers = Set<String>();
|
|
|
|
var _lastChangeTime = DateTime.now();
|
|
|
|
var _lastQueryPeers = Set<String>();
|
|
|
|
var _lastQueryTime = DateTime.now().subtract(Duration(hours: 1));
|
|
|
|
var _queryCoun = 0;
|
|
|
|
var _exit = false;
|
|
|
|
|
|
|
|
_PeerWidgetState() {
|
|
|
|
_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) {
|
2022-08-23 17:52:53 +08:00
|
|
|
final space = 12.0;
|
2022-07-27 22:56:28 +08:00
|
|
|
return ChangeNotifierProvider<Peers>(
|
2022-08-02 13:10:09 +08:00
|
|
|
create: (context) => super.widget._peers,
|
2022-08-23 17:52:53 +08:00
|
|
|
child: Consumer<Peers>(
|
|
|
|
builder: (context, peers, child) => peers.peers.isEmpty
|
|
|
|
? Center(
|
|
|
|
child: Text(translate("Empty")),
|
|
|
|
)
|
|
|
|
: SingleChildScrollView(
|
|
|
|
child: ObxValue<RxString>((searchText) {
|
2022-08-27 00:45:09 +08:00
|
|
|
return FutureBuilder<List<Peer>>(
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
final peers = snapshot.data!;
|
|
|
|
final cards = <Widget>[];
|
|
|
|
for (final peer in peers) {
|
|
|
|
cards.add(Offstage(
|
|
|
|
key: ValueKey("off${peer.id}"),
|
|
|
|
offstage: super.widget._offstageFunc(peer),
|
|
|
|
child: Obx(
|
|
|
|
() => SizedBox(
|
|
|
|
width: 220,
|
|
|
|
height:
|
|
|
|
peerCardUiType.value == PeerUiType.grid
|
|
|
|
? 140
|
|
|
|
: 42,
|
|
|
|
child: 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();
|
|
|
|
},
|
|
|
|
child: super
|
|
|
|
.widget
|
|
|
|
._peerCardWidgetFunc(peer),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
return Wrap(
|
|
|
|
spacing: space,
|
|
|
|
runSpacing: space,
|
|
|
|
children: cards);
|
|
|
|
} else {
|
|
|
|
return const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
future: matchPeers(searchText.value, peers.peers),
|
|
|
|
);
|
2022-08-23 17:52:53 +08:00
|
|
|
}, peerSearchText),
|
|
|
|
)),
|
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)) {
|
|
|
|
if (now.difference(_lastChangeTime) > Duration(seconds: 1)) {
|
2022-08-03 15:31:19 +08:00
|
|
|
if (_curPeers.length > 0) {
|
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) {
|
|
|
|
if (now.difference(_lastQueryTime) > Duration(seconds: 20)) {
|
2022-08-03 15:31:19 +08:00
|
|
|
if (_curPeers.length > 0) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await Future.delayed(Duration(milliseconds: 300));
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class BasePeerWidget extends StatelessWidget {
|
|
|
|
late final _name;
|
2022-08-02 13:10:09 +08:00
|
|
|
late final _loadEvent;
|
2022-07-27 22:56:28 +08:00
|
|
|
late final OffstageFunc _offstageFunc;
|
|
|
|
late final PeerCardWidgetFunc _peerCardWidgetFunc;
|
2022-08-02 13:10:09 +08:00
|
|
|
late final List<Peer> _initPeers;
|
2022-07-27 22:56:28 +08:00
|
|
|
|
|
|
|
BasePeerWidget({Key? key}) : super(key: key) {}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-02 13:10:09 +08:00
|
|
|
return _PeerWidget(Peers(_name, _loadEvent, _initPeers), _offstageFunc,
|
|
|
|
_peerCardWidgetFunc);
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RecentPeerWidget extends BasePeerWidget {
|
|
|
|
RecentPeerWidget({Key? key}) : super(key: key) {
|
|
|
|
super._name = "recent peer";
|
2022-08-02 13:10:09 +08:00
|
|
|
super._loadEvent = "load_recent_peers";
|
2022-07-27 22:56:28 +08:00
|
|
|
super._offstageFunc = (Peer _peer) => false;
|
2022-08-23 17:21:50 +08:00
|
|
|
super._peerCardWidgetFunc = (Peer peer) => RecentPeerCard(
|
|
|
|
peer: peer,
|
|
|
|
);
|
2022-08-02 13:10:09 +08:00
|
|
|
super._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.mainLoadRecentPeers();
|
2022-08-02 13:10:09 +08:00
|
|
|
return widget;
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FavoritePeerWidget extends BasePeerWidget {
|
|
|
|
FavoritePeerWidget({Key? key}) : super(key: key) {
|
|
|
|
super._name = "favorite peer";
|
2022-08-02 13:10:09 +08:00
|
|
|
super._loadEvent = "load_fav_peers";
|
2022-07-27 22:56:28 +08:00
|
|
|
super._offstageFunc = (Peer _peer) => false;
|
|
|
|
super._peerCardWidgetFunc = (Peer peer) => FavoritePeerCard(peer: peer);
|
2022-08-02 13:10:09 +08:00
|
|
|
super._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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DiscoveredPeerWidget extends BasePeerWidget {
|
|
|
|
DiscoveredPeerWidget({Key? key}) : super(key: key) {
|
|
|
|
super._name = "discovered peer";
|
2022-08-02 13:10:09 +08:00
|
|
|
super._loadEvent = "load_lan_peers";
|
2022-07-27 22:56:28 +08:00
|
|
|
super._offstageFunc = (Peer _peer) => false;
|
|
|
|
super._peerCardWidgetFunc = (Peer peer) => DiscoveredPeerCard(peer: peer);
|
2022-08-02 13:10:09 +08:00
|
|
|
super._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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AddressBookPeerWidget extends BasePeerWidget {
|
|
|
|
AddressBookPeerWidget({Key? key}) : super(key: key) {
|
|
|
|
super._name = "address book peer";
|
|
|
|
super._offstageFunc =
|
|
|
|
(Peer peer) => !_hitTag(gFFI.abModel.selectedTags, peer.tags);
|
|
|
|
super._peerCardWidgetFunc = (Peer peer) => AddressBookPeerCard(peer: peer);
|
2022-08-02 13:10:09 +08:00
|
|
|
super._initPeers = _loadPeers();
|
2022-07-27 22:56:28 +08:00
|
|
|
}
|
|
|
|
|
2022-08-02 13:10:09 +08:00
|
|
|
List<Peer> _loadPeers() {
|
2022-07-27 22:56:28 +08:00
|
|
|
return gFFI.abModel.peers.map((e) {
|
|
|
|
return Peer.fromJson(e['id'], e);
|
|
|
|
}).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _hitTag(List<dynamic> selectedTags, List<dynamic> idents) {
|
|
|
|
if (selectedTags.isEmpty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (idents.isEmpty) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (final tag in selectedTags) {
|
|
|
|
if (!idents.contains(tag)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|