95 lines
2.7 KiB
Swift
95 lines
2.7 KiB
Swift
//
|
|
// SideMenuPresenter.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Murad Shabanov on 17.03.2022.
|
|
// Copyright © 2022 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
protocol SideMenuModuleOutput {
|
|
func closeSideMenu()
|
|
var hasUnreadEmitter: Emitter<Bool>? { get }
|
|
}
|
|
|
|
final class SideMenuPresenter: SideMenuControllerOutput {
|
|
|
|
enum Constants {
|
|
|
|
enum Image {
|
|
static let dock = "sidemenu-dock"
|
|
static let undock = "sidemenu-undock"
|
|
static let notifications = "sidemenu-notification"
|
|
static let preferences = "sidemenu-preferences"
|
|
}
|
|
}
|
|
|
|
weak var input: SideMenuControllerInput?
|
|
private var output: SideMenuModuleOutput
|
|
private var hasUnreadNotifications: Bool = false
|
|
|
|
init(output: SideMenuModuleOutput) {
|
|
self.output = output
|
|
|
|
UserSettings.shared.isDockedEmitter.addReaction { [weak self] _ in
|
|
self?.input?.reloadBottomItems()
|
|
return self.isExist
|
|
}
|
|
|
|
self.output.hasUnreadEmitter?.addReaction({ [weak self] value in
|
|
self?.hasUnreadNotifications = value
|
|
self?.input?.reloadBottomItems()
|
|
return self.isExist
|
|
})
|
|
|
|
guard let session = try? Session.instance() else { return }
|
|
|
|
session.sessionEmitter.addReaction { [weak self] record -> ShouldContinueReceiveNotifications in
|
|
|
|
guard record.isExist else {
|
|
self?.output.closeSideMenu()
|
|
return self.isExist
|
|
}
|
|
|
|
return self.isExist
|
|
}
|
|
}
|
|
|
|
func itemSelectedAt(index: Int) {
|
|
guard index != -1 else { return }
|
|
let item = sideMenuItems[index].type
|
|
|
|
var route: Route?
|
|
switch item {
|
|
case .preferences:
|
|
route = .preference(tab: nil)
|
|
case .dock:
|
|
route = UserSettings.shared.isDocked ? .undock : .dock
|
|
case .notification:
|
|
route = .notification
|
|
}
|
|
|
|
route >>- { openApplicationRoute($0) }
|
|
}
|
|
|
|
private var sideMenuItems: [SideMenuItem] {
|
|
let dockImage = UserSettings.shared.isDocked ? Constants.Image.undock : Constants.Image.dock
|
|
|
|
return [
|
|
SideMenuItem(image: dockImage, type: .dock),
|
|
SideMenuItem(image: Constants.Image.notifications, type: .notification(unread: self.hasUnreadNotifications)),
|
|
SideMenuItem(image: Constants.Image.preferences, type: .preferences)
|
|
]
|
|
}
|
|
|
|
func sideMenuBottomItem(at index: Int) -> SideMenuItem {
|
|
return self.sideMenuItems[index]
|
|
}
|
|
|
|
func bottomItemscount() -> Int {
|
|
return self.sideMenuItems.count
|
|
}
|
|
|
|
}
|