83 lines
2.4 KiB
Swift
83 lines
2.4 KiB
Swift
//
|
|
// ConnectionStatusViewPresenter.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Zhandos Bolatbekov on 08.07.2021.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class ConnectionStatusViewPresenter: ConnectionStatusViewOutput {
|
|
|
|
private enum Constants {
|
|
|
|
enum LocalizedString {
|
|
static let connecting = "connection.ipad.status.connecting"
|
|
static let disconnecting = "connection.ipad.status.disconnecting"
|
|
}
|
|
|
|
static let animationInterval: TimeInterval = 0.5
|
|
}
|
|
|
|
weak var viewInput: ConnectionStatusViewInput?
|
|
|
|
private var scheduler: Timer?
|
|
private var statusText: String? {
|
|
didSet {
|
|
DispatchQueue.main.async { [weak self] in
|
|
self?.viewInput?.updateConnectionStatus(title: self?.statusText)
|
|
}
|
|
}
|
|
}
|
|
|
|
init(session: Session) {
|
|
|
|
session.vpnConnectionStateEmitter.addReaction { [weak self] state, _ -> ShouldContinueReceiveNotifications in
|
|
|
|
switch state {
|
|
case .connecting, .prepare:
|
|
self?.statusText = NSLocalizedString(Constants.LocalizedString.connecting, comment: "Connecting") + "."
|
|
self?.startAnimating()
|
|
|
|
case .disconnecting:
|
|
self?.statusText = NSLocalizedString(Constants.LocalizedString.disconnecting, comment: "Disconnecting") + "."
|
|
self?.startAnimating()
|
|
|
|
default:
|
|
self?.statusText = nil
|
|
self?.stopAnimating()
|
|
}
|
|
return self.isExist
|
|
}
|
|
}
|
|
|
|
private func startAnimating() {
|
|
guard !self.scheduler.isExist else { return }
|
|
|
|
let scheduler = Timer.scheduledTimer(withTimeInterval: Constants.animationInterval, repeats: true) { [weak self] _ in
|
|
self?.updateToNextState()
|
|
}
|
|
self.scheduler = scheduler
|
|
}
|
|
|
|
private func stopAnimating() {
|
|
if let scheduler = self.scheduler {
|
|
scheduler.invalidate()
|
|
scheduler.fire()
|
|
}
|
|
|
|
self.scheduler = nil
|
|
}
|
|
|
|
private func updateToNextState() {
|
|
guard var text = self.statusText else { return }
|
|
if text.hasSuffix("...") {
|
|
text.removeLast(2)
|
|
} else {
|
|
text += "."
|
|
}
|
|
self.statusText = text
|
|
}
|
|
}
|