2022-08-02 13:10:09 +08:00
import ' dart:convert ' ;
2022-07-27 22:56:28 +08:00
import ' package:flutter/foundation.dart ' ;
2023-09-14 10:17:03 +08:00
import ' package:get/get.dart ' ;
2022-08-03 22:03:31 +08:00
import ' platform_model.dart ' ;
2023-08-16 08:59:50 +08:00
// ignore: depend_on_referenced_packages
import ' package:collection/collection.dart ' ;
2022-07-27 22:56:28 +08:00
class Peer {
final String id ;
2023-08-02 22:25:54 +08:00
String hash ;
2023-09-14 10:17:03 +08:00
String username ; // pc username
2023-08-17 21:17:31 +08:00
String hostname ;
String platform ;
2022-10-08 16:53:03 +09:00
String alias ;
List < dynamic > tags ;
2022-11-28 18:16:29 +08:00
bool forceAlwaysRelay = false ;
String rdpPort ;
String rdpUsername ;
2022-07-27 22:56:28 +08:00
bool online = false ;
2023-09-14 10:17:03 +08:00
String loginName ; //login username
2022-07-27 22:56:28 +08:00
2023-03-10 15:25:19 +08:00
String getId ( ) {
if ( alias ! = ' ' ) {
return alias ;
}
return id ;
}
2022-09-21 21:20:19 +08:00
Peer . fromJson ( Map < String , dynamic > json )
: id = json [ ' id ' ] ? ? ' ' ,
2023-08-02 22:25:54 +08:00
hash = json [ ' hash ' ] ? ? ' ' ,
2022-09-21 21:20:19 +08:00
username = json [ ' username ' ] ? ? ' ' ,
2022-07-27 22:56:28 +08:00
hostname = json [ ' hostname ' ] ? ? ' ' ,
platform = json [ ' platform ' ] ? ? ' ' ,
2022-09-21 21:20:19 +08:00
alias = json [ ' alias ' ] ? ? ' ' ,
2022-11-28 18:16:29 +08:00
tags = json [ ' tags ' ] ? ? [ ] ,
forceAlwaysRelay = json [ ' forceAlwaysRelay ' ] = = ' true ' ,
rdpPort = json [ ' rdpPort ' ] ? ? ' ' ,
2023-09-14 10:17:03 +08:00
rdpUsername = json [ ' rdpUsername ' ] ? ? ' ' ,
loginName = json [ ' loginName ' ] ? ? ' ' ;
2022-07-27 22:56:28 +08:00
2022-10-08 16:53:03 +09:00
Map < String , dynamic > toJson ( ) {
return < String , dynamic > {
" id " : id ,
2023-08-02 22:25:54 +08:00
" hash " : hash ,
2022-10-08 16:53:03 +09:00
" username " : username ,
" hostname " : hostname ,
" platform " : platform ,
" alias " : alias ,
" tags " : tags ,
2022-11-28 18:16:29 +08:00
" forceAlwaysRelay " : forceAlwaysRelay . toString ( ) ,
" rdpPort " : rdpPort ,
" rdpUsername " : rdpUsername ,
2023-09-14 10:17:03 +08:00
' loginName ' : loginName ,
2022-10-08 16:53:03 +09:00
} ;
}
2023-08-02 22:25:54 +08:00
Map < String , dynamic > toAbUploadJson ( ) {
return < String , dynamic > {
" id " : id ,
" hash " : hash ,
" username " : username ,
" hostname " : hostname ,
" platform " : platform ,
2023-08-15 09:23:55 +08:00
" alias " : alias ,
2023-08-02 22:25:54 +08:00
" tags " : tags ,
} ;
}
2023-09-14 10:17:03 +08:00
Map < String , dynamic > toGroupCacheJson ( ) {
return < String , dynamic > {
" id " : id ,
" username " : username ,
" hostname " : hostname ,
" platform " : platform ,
" login_name " : loginName ,
} ;
}
2022-07-27 22:56:28 +08:00
Peer ( {
required this . id ,
2023-08-02 22:25:54 +08:00
required this . hash ,
2022-07-27 22:56:28 +08:00
required this . username ,
required this . hostname ,
required this . platform ,
2022-09-21 21:20:19 +08:00
required this . alias ,
2022-07-27 22:56:28 +08:00
required this . tags ,
2022-11-28 18:16:29 +08:00
required this . forceAlwaysRelay ,
required this . rdpPort ,
required this . rdpUsername ,
2023-09-14 10:17:03 +08:00
required this . loginName ,
2022-07-27 22:56:28 +08:00
} ) ;
Peer . loading ( )
: this (
2022-11-28 18:16:29 +08:00
id: ' ... ' ,
2023-08-02 22:25:54 +08:00
hash: ' ' ,
2022-11-28 18:16:29 +08:00
username: ' ... ' ,
hostname: ' ... ' ,
platform: ' ... ' ,
alias: ' ' ,
tags: [ ] ,
forceAlwaysRelay: false ,
rdpPort: ' ' ,
rdpUsername: ' ' ,
2023-09-14 10:17:03 +08:00
loginName: ' ' ,
2022-11-28 18:16:29 +08:00
) ;
2023-08-16 08:59:50 +08:00
bool equal ( Peer other ) {
return id = = other . id & &
hash = = other . hash & &
username = = other . username & &
hostname = = other . hostname & &
platform = = other . platform & &
alias = = other . alias & &
tags . equals ( other . tags ) & &
forceAlwaysRelay = = other . forceAlwaysRelay & &
rdpPort = = other . rdpPort & &
2023-09-14 10:17:03 +08:00
rdpUsername = = other . rdpUsername & &
loginName = = other . loginName ;
2023-08-16 08:59:50 +08:00
}
2023-08-16 22:43:17 +08:00
Peer . copy ( Peer other )
: this (
2023-09-14 10:17:03 +08:00
id: other . id ,
hash: other . hash ,
username: other . username ,
hostname: other . hostname ,
platform: other . platform ,
alias: other . alias ,
tags: other . tags . toList ( ) ,
forceAlwaysRelay: other . forceAlwaysRelay ,
rdpPort: other . rdpPort ,
rdpUsername: other . rdpUsername ,
loginName: other . loginName ,
) ;
2022-07-27 22:56:28 +08:00
}
2023-06-20 23:34:05 +08:00
enum UpdateEvent { online , load }
2022-07-27 22:56:28 +08:00
class Peers extends ChangeNotifier {
2022-09-01 06:18:29 -07:00
final String name ;
final String loadEvent ;
2023-09-14 10:17:03 +08:00
List < Peer > peers = List . empty ( growable: true ) ;
final RxList < Peer > ? initPeers ;
2023-06-20 23:34:05 +08:00
UpdateEvent event = UpdateEvent . load ;
2022-08-02 13:10:09 +08:00
static const _cbQueryOnlines = ' callback_query_onlines ' ;
2022-07-27 22:56:28 +08:00
2023-09-14 10:17:03 +08:00
Peers (
{ required this . name , required this . initPeers , required this . loadEvent } ) {
peers = initPeers ? ? [ ] ;
2022-09-11 19:52:38 -07:00
platformFFI . registerEventHandler ( _cbQueryOnlines , name , ( evt ) async {
2022-07-27 22:56:28 +08:00
_updateOnlineState ( evt ) ;
} ) ;
2022-09-11 19:52:38 -07:00
platformFFI . registerEventHandler ( loadEvent , name , ( evt ) async {
2022-08-02 13:10:09 +08:00
_updatePeers ( evt ) ;
} ) ;
2022-07-27 22:56:28 +08:00
}
@ override
void dispose ( ) {
2022-09-01 06:18:29 -07:00
platformFFI . unregisterEventHandler ( _cbQueryOnlines , name ) ;
platformFFI . unregisterEventHandler ( loadEvent , name ) ;
2022-07-27 22:56:28 +08:00
super . dispose ( ) ;
}
Peer getByIndex ( int index ) {
2022-09-01 06:18:29 -07:00
if ( index < peers . length ) {
return peers [ index ] ;
2022-07-27 22:56:28 +08:00
} else {
return Peer . loading ( ) ;
}
}
int getPeersCount ( ) {
2022-09-01 06:18:29 -07:00
return peers . length ;
2022-07-27 22:56:28 +08:00
}
void _updateOnlineState ( Map < String , dynamic > evt ) {
evt [ ' onlines ' ] . split ( ' , ' ) . forEach ( ( online ) {
2022-09-01 06:18:29 -07:00
for ( var i = 0 ; i < peers . length ; i + + ) {
if ( peers [ i ] . id = = online ) {
peers [ i ] . online = true ;
2022-07-27 22:56:28 +08:00
}
}
} ) ;
evt [ ' offlines ' ] . split ( ' , ' ) . forEach ( ( offline ) {
2022-09-01 06:18:29 -07:00
for ( var i = 0 ; i < peers . length ; i + + ) {
if ( peers [ i ] . id = = offline ) {
peers [ i ] . online = false ;
2022-07-27 22:56:28 +08:00
}
}
} ) ;
2023-06-20 23:34:05 +08:00
event = UpdateEvent . online ;
2022-07-27 22:56:28 +08:00
notifyListeners ( ) ;
}
2022-08-02 13:10:09 +08:00
void _updatePeers ( Map < String , dynamic > evt ) {
final onlineStates = _getOnlineStates ( ) ;
2023-09-14 10:17:03 +08:00
if ( initPeers ! = null ) {
peers = initPeers ! ;
} else {
peers = _decodePeers ( evt [ ' peers ' ] ) ;
}
2022-09-01 06:18:29 -07:00
for ( var peer in peers ) {
2022-08-02 13:10:09 +08:00
final state = onlineStates [ peer . id ] ;
peer . online = state ! = null & & state ! = false ;
2022-09-01 06:18:29 -07:00
}
2023-06-20 23:34:05 +08:00
event = UpdateEvent . load ;
2022-08-02 13:10:09 +08:00
notifyListeners ( ) ;
}
Map < String , bool > _getOnlineStates ( ) {
2022-09-01 06:18:29 -07:00
var onlineStates = < String , bool > { } ;
for ( var peer in peers ) {
2022-08-02 13:10:09 +08:00
onlineStates [ peer . id ] = peer . online ;
2022-09-01 06:18:29 -07:00
}
2022-08-02 13:10:09 +08:00
return onlineStates ;
}
List < Peer > _decodePeers ( String peersStr ) {
try {
if ( peersStr = = " " ) return [ ] ;
List < dynamic > peers = json . decode ( peersStr ) ;
2022-09-21 21:20:19 +08:00
return peers . map ( ( peer ) {
return Peer . fromJson ( peer as Map < String , dynamic > ) ;
} ) . toList ( ) ;
2022-08-02 13:10:09 +08:00
} catch ( e ) {
2022-09-01 06:18:29 -07:00
debugPrint ( ' peers(): $ e ' ) ;
2022-08-02 13:10:09 +08:00
}
return [ ] ;
}
2022-07-27 22:56:28 +08:00
}