Merge pull request #3567 from fufesou/fix/online_states_2

fix/online states 2
This commit is contained in:
RustDesk 2023-03-09 13:12:42 +08:00 committed by GitHub
commit 4223ed55a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,7 +45,7 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
var _lastChangeTime = DateTime.now();
var _lastQueryPeers = <String>{};
var _lastQueryTime = DateTime.now().subtract(const Duration(hours: 1));
var _queryCoun = 0;
var _queryCount = 0;
var _exit = false;
late final mobileWidth = () {
@ -78,12 +78,12 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
@override
void onWindowFocus() {
_queryCoun = 0;
_queryCount = 0;
}
@override
void onWindowMinimize() {
_queryCoun = _maxQueryCount;
_queryCount = _maxQueryCount;
}
@override
@ -100,6 +100,19 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
);
}
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, '');
Widget _buildPeersView(Peers peers) {
final body = ObxValue<RxString>((searchText) {
return FutureBuilder<List<Peer>>(
@ -109,16 +122,8 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
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();
},
key: ValueKey(_cardId(peer.id)),
onVisibilityChanged: onVisibilityChanged,
child: widget.peerCardBuilder(peer),
);
cards.add(isDesktop
@ -149,6 +154,7 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
// ignore: todo
// TODO: variables walk through async tasks?
void _startCheckOnlines() {
final queryInterval = const Duration(seconds: 20);
() async {
while (!_exit) {
final now = DateTime.now();
@ -158,18 +164,18 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
platformFFI.ffiBind
.queryOnlines(ids: _curPeers.toList(growable: false));
_lastQueryPeers = {..._curPeers};
_lastQueryTime = DateTime.now();
_queryCoun = 0;
_lastQueryTime = DateTime.now().subtract(queryInterval);
_queryCount = 0;
}
}
} else {
if (_queryCoun < _maxQueryCount) {
if (now.difference(_lastQueryTime) > const Duration(seconds: 20)) {
if (_queryCount < _maxQueryCount) {
if (now.difference(_lastQueryTime) >= queryInterval) {
if (_curPeers.isNotEmpty) {
platformFFI.ffiBind
.queryOnlines(ids: _curPeers.toList(growable: false));
_lastQueryTime = DateTime.now();
_queryCoun += 1;
_queryCount += 1;
}
}
}