129 lines
4.0 KiB
Swift
129 lines
4.0 KiB
Swift
//
|
|
// HeaderControllerPresenter.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Juraldinio on 1/27/21.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
protocol HeaderControllerModuleOutput: AnyObject {
|
|
var hasUnreadEmitter: Emitter<Bool>? { get }
|
|
}
|
|
|
|
final class HeaderControllerPresenter {
|
|
|
|
private enum MenuItem {
|
|
|
|
case preferences
|
|
case contactUs
|
|
case logout
|
|
case quit
|
|
|
|
var tag: Int {
|
|
switch self {
|
|
case .preferences: return 39
|
|
case .contactUs: return 40
|
|
case .logout: return 41
|
|
case .quit: return 42
|
|
}
|
|
}
|
|
|
|
var localizationIdentity: String {
|
|
switch self {
|
|
case .preferences: return "header.menu.preferences"
|
|
case .contactUs: return "header.menu.contactus"
|
|
case .logout: return "header.menu.logout"
|
|
case .quit: return "header.menu.quit"
|
|
}
|
|
}
|
|
|
|
static func item(by tag: Int) -> MenuItem? {
|
|
switch tag {
|
|
case 39: return .preferences
|
|
case 40: return .contactUs
|
|
case 41: return .logout
|
|
case 42: return .quit
|
|
default: return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
private weak var output: HeaderControllerModuleOutput?
|
|
weak var viewInput: HeaderControllerInput?
|
|
|
|
init(output: HeaderControllerModuleOutput?) {
|
|
self.output = output
|
|
|
|
UserSettings.shared.isDockedEmitter.addReaction { [weak self] isDocked -> ShouldContinueReceiveNotifications in
|
|
self?.viewInput?.window(isDocked: isDocked)
|
|
return self.isExist
|
|
}
|
|
|
|
UserSettings.shared.isPinnedEmitter.addReaction { [weak self] isPinned -> ShouldContinueReceiveNotifications in
|
|
self?.viewInput?.window(isPinned: isPinned)
|
|
return self.isExist
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension HeaderControllerPresenter: HeaderControllerOutput {
|
|
|
|
var isDocked: Bool { UserSettings.shared.isDocked }
|
|
|
|
var isPinned: Bool { UserSettings.shared.isPinned }
|
|
|
|
var hasUnreadEmitter: Emitter<Bool>? { self.output?.hasUnreadEmitter }
|
|
|
|
func controllerDelegateIcon(activate type: HeaderControllerLeftIconType, in controller: HeaderControllerInput) {
|
|
switch type {
|
|
case .back: openApplicationRoute(.back)
|
|
default:
|
|
break
|
|
// case .bell: openApplicationRoute(.notification)
|
|
}
|
|
}
|
|
|
|
func viewIsReady() {
|
|
self.viewInput?.window(isDocked: UserSettings.shared.isDocked)
|
|
}
|
|
|
|
func openMenu() {
|
|
openApplicationRoute(.menu)
|
|
}
|
|
|
|
func controllerDelegateMenu(in controller: HeaderControllerInput) -> [HeaderControllerMenuItem]? {
|
|
|
|
return [
|
|
.menu(title: NSLocalizedString(MenuItem.preferences.localizationIdentity, comment: ""), enabled: true, tag: MenuItem.preferences.tag),
|
|
.menu(title: NSLocalizedString(MenuItem.contactUs.localizationIdentity, comment: ""), enabled: true, tag: MenuItem.contactUs.tag),
|
|
.line,
|
|
.menu(title: NSLocalizedString(MenuItem.logout.localizationIdentity, comment: ""), enabled: true, tag: MenuItem.logout.tag),
|
|
.line,
|
|
.menu(title: NSLocalizedString(MenuItem.quit.localizationIdentity, comment: ""), enabled: true, tag: MenuItem.quit.tag)
|
|
]
|
|
}
|
|
|
|
func controllerDelegateMenuItem(selected tag: Int) {
|
|
|
|
guard let item = MenuItem.item(by: tag) else { return }
|
|
|
|
var route: Route?
|
|
switch item {
|
|
case .preferences: route = .preference(tab: nil)
|
|
case .contactUs:
|
|
if let url = URL(string: PrivadoConstants.Link.contactUs) { route = .navigate(url: url) }
|
|
case .logout: route = .login(type: .signout)
|
|
case .quit: route = .quit
|
|
}
|
|
|
|
if let route = route { openApplicationRoute(route) }
|
|
}
|
|
|
|
func controllerDelegateSwitchPinning() {
|
|
UserSettings.shared.isPinned.toggle()
|
|
}
|
|
}
|