94 lines
2.6 KiB
Swift
94 lines
2.6 KiB
Swift
//
|
|
// ServerPanelPresenter.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Juraldinio on 1/31/21.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum ServerPanelState {
|
|
case undefined
|
|
case selected(country: String, city: String, flag: String)
|
|
case overqoute(country: String, city: String, flag: String)
|
|
}
|
|
|
|
protocol ServerPanelOutput: AnyObject {
|
|
var updateEmitter: Emitter<Void> { get }
|
|
|
|
func update(for panel: ServerPanelInput)
|
|
func selectRequired(from panel: ServerPanelInput)
|
|
}
|
|
|
|
protocol ServerPanelInput: AnyObject {
|
|
func update(state: ServerPanelState)
|
|
}
|
|
|
|
final class ServerPanelPresenter {
|
|
|
|
private weak var output: ServerPanelOutput?
|
|
|
|
weak var viewInput: ServerPanelViewInput?
|
|
|
|
// MARK: - Init
|
|
|
|
init(output: ServerPanelOutput?) {
|
|
self.output = output
|
|
output?.updateEmitter.addReaction { [weak self] in
|
|
guard let self = self else { return false }
|
|
self.output?.update(for: self)
|
|
return true
|
|
}
|
|
|
|
guard let session = try? Session.instance() else { return }
|
|
|
|
session.vpnConnectionStateEmitter.addReaction { [weak self, weak session] state, _ in
|
|
guard let session = session else {
|
|
self?.viewInput?.countryCircle(isHighlited: false)
|
|
return self.isExist
|
|
}
|
|
|
|
self?.viewInput?.countryCircle(isHighlited: state == .connected)
|
|
// self?.viewInput?.selection(enabled: state == .disconnected && session.currentRecord.isExist)
|
|
|
|
return self.isExist
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - ServerPanelInput
|
|
|
|
extension ServerPanelPresenter: ServerPanelInput {
|
|
|
|
func update(state: ServerPanelState) {
|
|
switch state {
|
|
case .undefined:
|
|
self.viewInput?.hide()
|
|
case let .selected(country: countryCode, city: city, flag: flag):
|
|
self.viewInput?.useHandCursor(enable: true)
|
|
self.viewInput?.show(country: CountryResolver.name(from: countryCode), city: city, flag: flag)
|
|
case let .overqoute(country: countryCode, city: city, flag: flag):
|
|
self.viewInput?.useHandCursor(enable: true)
|
|
self.viewInput?.show(country: CountryResolver.name(from: countryCode), city: city, flag: flag)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - ServerPanelViewOutput
|
|
|
|
extension ServerPanelPresenter: ServerPanelViewOutput {
|
|
|
|
func viewIsReady() {
|
|
self.output?.update(for: self)
|
|
}
|
|
|
|
func viewWillDisappear() {
|
|
|
|
}
|
|
|
|
func serverSelectRequired() {
|
|
self.output?.selectRequired(from: self)
|
|
}
|
|
}
|