Skip to content

Commit

Permalink
#11: Pass node address instead of storing it
Browse files Browse the repository at this point in the history
  • Loading branch information
lika-vorobeva committed Oct 5, 2022
1 parent ece615e commit ceecf13
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 306 deletions.
16 changes: 12 additions & 4 deletions SolardVPNCommunityCoreiOS/SOLARAPI/APISupport/NetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ struct InnerErrors: Codable {
let errors: [InnerError]
}

struct SingleInnerError: Codable {
let error: InnerError
public struct SingleInnerError: Codable {
public let error: InnerError

public init(error: InnerError) {
self.error = error
}

public init(code: Int, message: String) {
self.error = .init(code: code, message: message)
}
}

public struct InnerError: Codable {
let code: Int
let message: String
public let code: Int
public let message: String

public init(code: Int, message: String) {
self.code = code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import GRPC
// MARK: - ConnectionModelEvent

enum ConnectionModelEvent {
case error(Error)
case warning(Error)
case info(String)

case error(SingleInnerError)
case warning(SingleInnerError)
case updateTunnelActivity(isActive: Bool)
}

Expand Down Expand Up @@ -59,110 +57,38 @@ extension ConnectionModel: ConnectionModelDelegate {
}
}

// MARK: - NodeModelDelegate

extension ConnectionModel: NodeModelDelegate {
func suggestUnsubscribe(from node: Node) {
eventSubject.send(.error(SessionsServiceError.nodeMisconfigured))
}

func openPlans(node: Node, resubscribe: Bool) {
eventSubject.send(.error(resubscribe ? ConnectionModelError.noQuotaLeft : .noSubscription))
}
}

// MARK: - Connection functions

extension ConnectionModel {
func setInitData() {
updateConnectionType(forceUpdate: true, disconnect: false)
}

func refresh() {
updateConnectionType()
}

/// Should be called each time when we turn toggle to "on" state
func connect() {
delegate?.connect()
func connect(to node: String) -> Bool {
delegate?.connect(to: node) ?? false
}

/// Should be called each time when we turn toggle to "off" state
func disconnect() {
guard let tunnel = context.tunnelManager.lastTunnel, tunnel.status != .disconnected else {
stopLoading()
return
}

context.tunnelManager.startDeactivation(of: tunnel)
}

func cancelSubscriptions(for nodeAddress: String) {
context.subscriptionsService.loadActiveSubscriptions { [weak self] result in
switch result {
case let .success(subscriptions):
let subscriptionsToCancel = subscriptions.filter { $0.node == nodeAddress }.map { $0.id }

self?.context.subscriptionsService.cancel(
subscriptions: subscriptionsToCancel,
with: nodeAddress
) { [weak self] result in
switch result {
case let .failure(error):
self?.show(error: error)
case .success:
self?.handleCancellation(address: nodeAddress)
}
}
case let .failure(error):
self?.show(error: error)
}
}
func disconnect() -> Bool {
context.tunnelManager.startDeactivationOfActiveTunnel()
}
}

// MARK: - Events

extension ConnectionModel {
internal func show(error: Error) {
internal func show(error: SingleInnerError) {
log.error(error)
stopLoading()

eventSubject.send(.updateTunnelActivity(isActive: isTunnelActive))
eventSubject.send(.error(error))
}

func show(warning: Error) {
func show(warning: SingleInnerError) {
eventSubject.send(.warning(warning))
}

private func stopLoading() {
eventSubject.send(.updateTunnelActivity(isActive: isTunnelActive))
}

private func handleCancellation(address: String) {
eventSubject.send(.info(TunnelRouteEvent.subscriptionCanceled.rawValue))

stopLoading()
delegate?.refreshNode()
}
}

// MARK: - Connection type

extension ConnectionModel {
func updateConnectionType(forceUpdate: Bool = false, disconnect: Bool = true) {
guard forceUpdate else {
delegate?.refreshNode()
return
}

if disconnect {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.disconnect()
}
}
}
}

// MARK: - Wallet
Expand All @@ -174,7 +100,7 @@ extension ConnectionModel {
if let statusError = error as? GRPC.GRPCStatus, statusError.code == .notFound {
return
}
self?.show(error: error)
self?.show(error: .init(code: 500, message: error.localizedDescription))
}
}

Expand All @@ -183,7 +109,7 @@ extension ConnectionModel {
case .success(let info):
log.debug(info)
case .failure(let error):
self?.show(error: error)
self?.show(error: .init(code: 500, message: error.localizedDescription))
}
}
}
Expand All @@ -203,7 +129,7 @@ extension ConnectionModel: TunnelManagerDelegate {
func handleTunnelUpdatingStatus() { }

func handleError(_ error: Error) {
show(error: error)
show(error: .init(code: 500, message: error.localizedDescription))
}

func handleTunnelReconnection() { }
Expand All @@ -215,7 +141,7 @@ extension ConnectionModel: TunnelManagerDelegate {

extension ConnectionModel: TunnelsServiceStatusDelegate {
func activationAttemptFailed(for tunnel: TunnelContainer, with error: TunnelActivationError) {
show(error: error)
show(error: .init(code: 500, message: error.localizedDescription))
}

func activationAttemptSucceeded(for tunnel: TunnelContainer) {
Expand All @@ -227,7 +153,7 @@ extension ConnectionModel: TunnelsServiceStatusDelegate {
}

func activationFailed(for tunnel: TunnelContainer, with error: TunnelActivationError) {
show(error: error)
show(error: .init(code: 500, message: error.localizedDescription))
}

func activationSucceeded(for tunnel: TunnelContainer) {
Expand Down
Loading

0 comments on commit ceecf13

Please sign in to comment.