44from collections .abc import Iterator
55from datetime import datetime as dt
66
7+ import dispatch
8+ import Network
79from CoreWLAN import CWInterface , CWNetwork , CWWiFiClient
810
911from vorta .log import logger
1315class DarwinNetworkStatus (NetworkStatusMonitor ):
1416 def __init__ (self ) -> None :
1517 super ().__init__ ()
18+ # Default state of none indicates we haven't received a path update yet
19+ self .nw_path = None
20+ self .nw_path_monitor = Network .nw_path_monitor_create ()
21+ Network .nw_path_monitor_set_update_handler (self .nw_path_monitor , self ._path_updated )
22+ # Needs a dispatch to get the first event
23+ Network .nw_path_monitor_set_queue (self .nw_path_monitor , dispatch .dispatch_get_main_queue ())
24+ Network .nw_path_monitor_start (self .nw_path_monitor )
25+
26+ def _path_updated (self , path ):
27+ self .nw_path = path
28+ self .network_status_changed .emit (self .is_network_active ())
1629
1730 def is_network_metered (self ) -> bool :
1831 interface : CWInterface = self ._get_wifi_interface ()
@@ -30,9 +43,16 @@ def is_network_metered(self) -> bool:
3043
3144 return is_ios_hotspot or any (is_network_metered_with_android (d ) for d in get_network_devices ())
3245
33- def is_network_active (self ) -> bool :
34- # Not yet implemented
35- return True
46+ def is_network_active (self ):
47+ # We haven't received an update yet, surely it is coming soon
48+ if self .nw_path is None :
49+ return False
50+ # https://developer.apple.com/documentation/network/nw_path_status_satisfiable
51+ # Maybe making a network connection will work so treat it as active
52+ return Network .nw_path_get_status (self .nw_path ) in (
53+ Network .nw_path_status_satisfied ,
54+ Network .nw_path_status_satisfiable ,
55+ )
3656
3757 def get_current_wifi (self ) -> str | None :
3858 """
0 commit comments