@ -1,6 +1,7 @@
// C o p y r i g h t © 2 0 2 2 R a n g e p r o o f P t y L t d . A l l r i g h t s r e s e r v e d .
import UIKit
import Reachability
import SessionUIKit
final class PathStatusView : UIView {
@ -42,6 +43,7 @@ final class PathStatusView: UIView {
// MARK: - I n i t i a l i z a t i o n
private let size : Size
private let reachability : Reachability = Reachability . forInternetConnection ( )
init ( size : Size = . small ) {
self . size = size
@ -73,15 +75,34 @@ final class PathStatusView: UIView {
self . set ( . width , to : self . size . pointSize )
self . set ( . height , to : self . size . pointSize )
setStatus ( to : ( ! OnionRequestAPI . paths . isEmpty ? . connected : . connecting ) )
switch ( reachability . isReachable ( ) , OnionRequestAPI . paths . isEmpty ) {
case ( false , _ ) : setStatus ( to : . error )
case ( true , true ) : setStatus ( to : . connecting )
case ( true , false ) : setStatus ( to : . connected )
}
}
// MARK: - F u n c t i o n s
private func registerObservers ( ) {
let notificationCenter = NotificationCenter . default
notificationCenter . addObserver ( self , selector : #selector ( handleBuildingPathsNotification ) , name : . buildingPaths , object : nil )
notificationCenter . addObserver ( self , selector : #selector ( handlePathsBuiltNotification ) , name : . pathsBuilt , object : nil )
NotificationCenter . default . addObserver (
self ,
selector : #selector ( handleBuildingPathsNotification ) ,
name : . buildingPaths ,
object : nil
)
NotificationCenter . default . addObserver (
self ,
selector : #selector ( handlePathsBuiltNotification ) ,
name : . pathsBuilt ,
object : nil
)
NotificationCenter . default . addObserver (
self ,
selector : #selector ( reachabilityChanged ) ,
name : . reachabilityChanged ,
object : nil
)
}
private func setStatus ( to status : Status ) {
@ -102,10 +123,34 @@ final class PathStatusView: UIView {
}
@objc private func handleBuildingPathsNotification ( ) {
guard reachability . isReachable ( ) else {
setStatus ( to : . error )
return
}
setStatus ( to : . connecting )
}
@objc private func handlePathsBuiltNotification ( ) {
guard reachability . isReachable ( ) else {
setStatus ( to : . error )
return
}
setStatus ( to : . connected )
}
@objc private func reachabilityChanged ( ) {
guard Thread . isMainThread else {
DispatchQueue . main . async { [ weak self ] in self ? . reachabilityChanged ( ) }
return
}
guard reachability . isReachable ( ) else {
setStatus ( to : . error )
return
}
setStatus ( to : ( ! OnionRequestAPI . paths . isEmpty ? . connected : . connecting ) )
}
}