@ -1,9 +1,11 @@
// 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 NVActivityIndicatorView
import SessionMessagingKit
import SessionUIKit
import SessionSnodeKit
final class PathVC : BaseVC {
public static let dotSize : CGFloat = 8
@ -239,6 +241,7 @@ private final class LineView: UIView {
private var dotViewWidthConstraint : NSLayoutConstraint !
private var dotViewHeightConstraint : NSLayoutConstraint !
private var dotViewAnimationTimer : Timer !
private let reachability : Reachability = Reachability . forInternetConnection ( )
enum Location {
case top , middle , bottom
@ -273,6 +276,7 @@ private final class LineView: UIView {
super . init ( frame : CGRect . zero )
setUpViewHierarchy ( )
registerObservers ( )
}
override init ( frame : CGRect ) {
@ -283,6 +287,12 @@ private final class LineView: UIView {
preconditionFailure ( " Use init(location:dotAnimationStartDelay:dotAnimationRepeatInterval:) instead. " )
}
deinit {
NotificationCenter . default . removeObserver ( self )
dotViewAnimationTimer ? . invalidate ( )
}
private func setUpViewHierarchy ( ) {
let lineView = UIView ( )
lineView . set ( . width , to : Values . separatorThickness )
@ -315,10 +325,33 @@ private final class LineView: UIView {
self ? . animate ( )
}
}
switch ( reachability . isReachable ( ) , OnionRequestAPI . paths . isEmpty ) {
case ( false , _ ) : setStatus ( to : . error )
case ( true , true ) : setStatus ( to : . connecting )
case ( true , false ) : setStatus ( to : . connected )
}
}
deinit {
dotViewAnimationTimer ? . invalidate ( )
private func registerObservers ( ) {
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 animate ( ) {
@ -340,4 +373,41 @@ private final class LineView: UIView {
self ? . dotView . transform = CGAffineTransform . scale ( 1 )
}
}
private func setStatus ( to status : PathStatusView . Status ) {
dotView . themeBackgroundColor = status . themeColor
dotView . layer . themeShadowColor = status . themeColor
}
@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 ) )
}
}