// // InterfaceApplicationSettingsPresenter.swift // PrivadoVPN // // Created by Murad Shabanov on 05.10.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import Foundation import ServiceManagement final class InterfaceApplicationSettingsPresenter: InterfaceApplicationSettingsViewOutput { // swiftlint:disable nesting private enum Constants { enum Localized { enum WindowStyle { static let title = "preference.interfaceSettings.windowStyle.title" static let dockToTray = "preference.interfaceSettings.windowStyle.dockToTray" static let undock = "preference.interfaceSettings.windowStyle.undock" } enum Update { static let title = "preference.interfaceSettings.update.title" static let description = "preference.interfaceSettings.update.description" } } static let windowStyles = [ WindowDockStyle(title: NSLocalizedString(Constants.Localized.WindowStyle.dockToTray, comment: ""), option: .dock), WindowDockStyle(title: NSLocalizedString(Constants.Localized.WindowStyle.undock, comment: ""), option: .undock) ] } // swiftlint:enable nesting private let interactor: InterfaceApplicationSettingsInteractorInput weak var viewInput: InterfaceApplicationSettingsViewInput? init(interactor: InterfaceApplicationSettingsInteractorInput) { self.interactor = interactor UserSettings.shared.isDockedEmitter.addReaction { [weak self] isDocked -> ShouldContinueReceiveNotifications in self?.viewInput?.enableDock(at: isDocked ? 0 : 1) return self.isExist } } private var settings: [InterfaceApplicationRecord] { [ InterfaceApplicationRecord(title: "Auto Start", description: "Automatically launch the PrivadoVPN app when your computer starts.", enabled: true, selected: UserSettings.shared.autostartEnabled, type: .autoStart, options: nil), InterfaceApplicationRecord(title: NSLocalizedString(Constants.Localized.WindowStyle.title, comment: ""), description: nil, enabled: true, selected: nil, type: .dock, options: [PreferenceOptions.radio(Constants.windowStyles, "", UserSettings.shared.isDocked ? 0 : 1, .init(size: CGSize(width: 25, height: 25), fontName: PrivadoConstants.Font.medium, fontSize: 14, textYoffset: 5))] ) /*InterfaceApplicationRecord(title: NSLocalizedString(Constants.Localized.Update.title, comment: ""), description: NSLocalizedString(Constants.Localized.Update.description, comment: ""), enabled: true, selected: UserSettings.shared.backgroundUpdate, type: .backgroundUpdate, options: nil)*/ ] } // MARK: - InterfaceSettingsViewOutput func isReady() { self.viewInput?.available(options: self.settings) self.updateApplicationVersion() } func changedUpdater(enabled: Bool) { UserSettings.shared.backgroundUpdate = enabled } func changedDock(index: Int) { let route: Route = index == 0 ? .dock : .undock UserSettings.shared.isDocked = index == 0 openApplicationRoute(route) } 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?.enableAutostart(enabled: !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?.enableAutostart(enabled: !enabled) } private func updateApplicationVersion() { guard let viewInput = self.viewInput else { return } let formatter = DateFormatter() formatter.locale = Locale(identifier: "en_US") formatter.dateFormat = "yyyy-MM-dd" let timestamp: String if let compileTimestamp = self.interactor.compileTimestamp { timestamp = compileTimestamp } else { timestamp = formatter.string(from: Date()) } let debug: Bool #if PRODUCTION debug = false #else debug = true #endif viewInput.update(version: self.interactor.version, buildDate: timestamp, debug: debug) } func changed(_ record: InterfaceApplicationRecord, selected: Bool) { guard let session = try? Session.instance() , session.currentRecord.isExist else { return } switch record.type { case .autoStart: self.switchAutostart(enabled: selected) case .dock: break default: break } } }