Skip to content

Commit

Permalink
Fix Swiftlint warnings in MapConnectionStatusOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson committed Sep 5, 2023
1 parent 7444ac2 commit c2fe77d
Showing 1 changed file with 57 additions and 45 deletions.
102 changes: 57 additions & 45 deletions ios/MullvadVPN/TunnelManager/MapConnectionStatusOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,7 @@ class MapConnectionStatusOperation: AsyncOperation {

switch connectionStatus {
case .connecting:
switch tunnelState {
case .connecting:
break

default:
interactor.updateTunnelStatus { tunnelStatus in
tunnelStatus.state = .connecting(nil)
}
}

fetchTunnelStatus(tunnel: tunnel) { packetTunnelStatus in
if packetTunnelStatus.isNetworkReachable {
return packetTunnelStatus.tunnelRelay.map { .connecting($0) }
} else {
return .waitingForConnectivity(.noConnection)
}
}
handleConnectingState(tunnelState, tunnel)
return

case .reasserting:
Expand All @@ -87,36 +71,10 @@ class MapConnectionStatusOperation: AsyncOperation {
return

case .disconnected:
switch tunnelState {
case .pendingReconnect:
logger.debug("Ignore disconnected state when pending reconnect.")

case .disconnecting(.reconnect):
logger.debug("Restart the tunnel on disconnect.")
interactor.updateTunnelStatus { tunnelStatus in
tunnelStatus = TunnelStatus()
tunnelStatus.state = .pendingReconnect
}
interactor.startTunnel()

default:
setTunnelDisconnectedStatus()
}
handleDisconnectedState(tunnelState)

case .disconnecting:
switch tunnelState {
case .disconnecting:
break
default:
interactor.updateTunnelStatus { tunnelStatus in
let packetTunnelStatus = tunnelStatus.packetTunnelStatus

tunnelStatus = TunnelStatus()
tunnelStatus.state = packetTunnelStatus.isNetworkReachable
? .disconnecting(.nothing)
: .waitingForConnectivity(.noNetwork)
}
}
handleDisconnectionState(tunnelState)

case .invalid:
setTunnelDisconnectedStatus()
Expand All @@ -132,6 +90,60 @@ class MapConnectionStatusOperation: AsyncOperation {
request?.cancel()
}

private func handleConnectingState(_ tunnelState: TunnelState, _ tunnel: Tunnel) {
switch tunnelState {
case .connecting:
break

default:
interactor.updateTunnelStatus { tunnelStatus in
tunnelStatus.state = .connecting(nil)
}
}

fetchTunnelStatus(tunnel: tunnel) { packetTunnelStatus in
if packetTunnelStatus.isNetworkReachable {
return packetTunnelStatus.tunnelRelay.map { .connecting($0) }
} else {
return .waitingForConnectivity(.noConnection)
}
}
}

private func handleDisconnectionState(_ tunnelState: TunnelState) {
switch tunnelState {
case .disconnecting:
break
default:
interactor.updateTunnelStatus { tunnelStatus in
let packetTunnelStatus = tunnelStatus.packetTunnelStatus

tunnelStatus = TunnelStatus()
tunnelStatus.state = packetTunnelStatus.isNetworkReachable
? .disconnecting(.nothing)
: .waitingForConnectivity(.noNetwork)
}
}
}

private func handleDisconnectedState(_ tunnelState: TunnelState) {
switch tunnelState {
case .pendingReconnect:
logger.debug("Ignore disconnected state when pending reconnect.")

case .disconnecting(.reconnect):
logger.debug("Restart the tunnel on disconnect.")
interactor.updateTunnelStatus { tunnelStatus in
tunnelStatus = TunnelStatus()
tunnelStatus.state = .pendingReconnect
}
interactor.startTunnel()

default:
setTunnelDisconnectedStatus()
}
}

private func setTunnelDisconnectedStatus() {
interactor.updateTunnelStatus { tunnelStatus in
tunnelStatus = TunnelStatus()
Expand Down

0 comments on commit c2fe77d

Please sign in to comment.