178 lines
6.8 KiB
Swift
178 lines
6.8 KiB
Swift
//
|
|
// OptionsPreferencesPresenter.swift
|
|
// Cyberlock
|
|
//
|
|
// Created by Jura on 9/18/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import ServiceManagement
|
|
|
|
final class OptionsPreferencesPresenter: OptionsPreferencesViewOutput {
|
|
|
|
let enableKillSwitch: Bool = {
|
|
if #available(OSX 10.14, *) {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}()
|
|
|
|
|
|
weak var viewInput: OptionsPreferencesViewInput?
|
|
|
|
init(output: Any?) {
|
|
// self.output = output
|
|
|
|
guard let session = try? Session.instance() else { return }
|
|
|
|
session.sessionEmitter.addReaction { [weak self] session -> ShouldContinueReceiveNotifications in
|
|
if let self = self
|
|
, !session.isExist {
|
|
self.switchKillSwitch(enabled: false)
|
|
self.switchAutoConnect(enabled: false)
|
|
self.switchAutostart(enabled: true)
|
|
}
|
|
|
|
guard let self = self else { return false }
|
|
|
|
self.enableDefault()
|
|
return true
|
|
}
|
|
|
|
UserSettings.shared.killSwitchEmitter.addReaction { [weak self] enabled in
|
|
self?.viewInput?.select(type: .killSwitch, selected: enabled)
|
|
return self.isExist
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private var options: [OptionsPreferenceRecord] {
|
|
|
|
let autoConnectOptions = self.autoConnectOptions()
|
|
|
|
return [
|
|
OptionsPreferenceRecord(title: NSLocalizedString("preference.options.killSwitch.title", comment: ""),
|
|
description: NSLocalizedString("preference.options.killSwitch.description", comment: ""),
|
|
enabled: self.enableKillSwitch,
|
|
selected: UserSettings.shared.killSwitchEnabled,
|
|
type: .killSwitch,
|
|
options: nil),
|
|
// OptionsPreferenceRecord(title: NSLocalizedString("preference.options.autoStart.title", comment: ""),
|
|
// description: NSLocalizedString("preference.options.autoStart.description", comment: ""),
|
|
// enabled: true,
|
|
// selected: UserSettings.shared.autostartEnabled,
|
|
// type: .autoStart,
|
|
// options: nil),
|
|
OptionsPreferenceRecord(title: NSLocalizedString("preference.options.autoConnect.title", comment: ""),
|
|
description: NSLocalizedString("preference.options.autoConnect.description", comment: ""),
|
|
enabled: true,
|
|
selected: UserSettings.shared.preferredServer.isExist,
|
|
type: .autoConnect,
|
|
options: autoConnectOptions)
|
|
]
|
|
}
|
|
|
|
private let autoConnectOptionsArray = [
|
|
AutoConnectPreferenceRecord(title: NSLocalizedString("preference.options.autoConnect.best", comment: ""), type: .best),
|
|
AutoConnectPreferenceRecord(title: NSLocalizedString("preference.options.autoConnect.previous", comment: ""), type: .last),
|
|
AutoConnectPreferenceRecord(title: NSLocalizedString("preference.options.autoConnect.random", comment: ""), type: .random)
|
|
]
|
|
|
|
private func autoConnectOptions() -> [PreferenceOptions] {
|
|
return [
|
|
PreferenceOptions.radio(
|
|
autoConnectOptionsArray,
|
|
"Preferred Server",
|
|
PreferredServerType.allCases.firstIndex(where: { (type) in
|
|
return UserSettings.shared.preferredServer == type
|
|
}) ?? 0,
|
|
.init(size: CGSize(width: 25, height: 25), fontName: PrivadoConstants.Font.medium, fontSize: 14, textYoffset: 5)
|
|
)
|
|
]
|
|
}
|
|
|
|
func switchAutostart(enabled: Bool) {
|
|
|
|
let launcherAppId = PrivadoConstants.Launcher.bundle
|
|
if !SMLoginItemSetEnabled(launcherAppId as CFString, enabled) {
|
|
|
|
CometLogger.shared.event(name: PrivadoConstants.Event.loginItem,
|
|
attributes: [PrivadoConstants.Event.Attributes.enabled: enabled,
|
|
PrivadoConstants.Event.Attributes.success: false],
|
|
secured: nil)
|
|
|
|
self.viewInput?.select(type: .autoConnect, selected: !enabled)
|
|
return
|
|
}
|
|
|
|
UserSettings.shared.autostartEnabled = enabled
|
|
|
|
CometLogger.shared.event(name: PrivadoConstants.Event.Statistic.Application.settings,
|
|
attributes: [PrivadoConstants.Event.Attributes.autostart: enabled],
|
|
secured: nil)
|
|
|
|
self.viewInput?.select(type: .autoStart, selected: enabled)
|
|
}
|
|
|
|
func switchAutoConnect(enabled: Bool) {
|
|
UserSettings.shared.preferredServer = enabled
|
|
? .last
|
|
: .none
|
|
self.viewInput?.select(type: .autoConnect, selected: enabled)
|
|
}
|
|
|
|
func switchKillSwitch(enabled: Bool) {
|
|
|
|
CometLogger.shared.event(name: PrivadoConstants.Event.Statistic.Application.killswitch,
|
|
attributes: [PrivadoConstants.Event.Attributes.value: enabled],
|
|
secured: nil)
|
|
|
|
UserSettings.shared.killSwitchEnabled = enabled
|
|
}
|
|
|
|
func enableDefault() {
|
|
|
|
let session = try? Session.instance().currentRecord
|
|
|
|
self.viewInput?.options(enable: session.isExist, for: .autoStart)
|
|
self.viewInput?.options(enable: session.isExist, for: .autoConnect)
|
|
self.viewInput?.options(enable: self.enableKillSwitch && session.isExist, for: .killSwitch)
|
|
}
|
|
|
|
// MARK: - OptionsPreferencesViewOutput
|
|
|
|
func isReady() {
|
|
self.viewInput?.available(options: self.options)
|
|
self.enableDefault()
|
|
}
|
|
|
|
func changed(_ record: OptionsPreferenceRecord, selected: Bool) {
|
|
|
|
guard let session = try? Session.instance()
|
|
, session.currentRecord.isExist else { return }
|
|
|
|
switch record.type {
|
|
case .autoStart:
|
|
self.switchAutostart(enabled: selected)
|
|
case .autoConnect:
|
|
self.switchAutoConnect(enabled: selected)
|
|
case .killSwitch:
|
|
self.switchKillSwitch(enabled: selected)
|
|
case .preferredServer:
|
|
break
|
|
}
|
|
}
|
|
|
|
func changeAutoConnectPreference(_ selectedIndex: Int) {
|
|
PreferredServerType.allCases.enumerated().forEach { (index, type) in
|
|
if selectedIndex == index {
|
|
UserSettings.shared.preferredServer = type
|
|
}
|
|
}
|
|
}
|
|
}
|