Files

171 lines
5.8 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// UpgradePresenter.swift
// PrivadoVPN
//
// Created by Zhandos Bolatbekov on 29.01.2021.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import Foundation
class UpgradePresenter: UpgradeViewOutput {
private enum Constants {
enum LocalizedString {
static let outPeriodTitle = "upgrade.outPeriod.title"
static let outPeriodBody = "upgrade.outPeriod.body"
static let outTrafficTitle = "upgrade.outTraffic.title"
static let outTrafficBody = "upgrade.outTraffic.body"
static let actionButton = "modal.traffic.button"
}
}
weak var viewInput: UpgradeViewInput?
// Modal view used for iPad
weak var modalViewInput: ModalControllerInput?
private weak var output: UpgradeModuleOutput?
private let interactor: UpgradeInteractorInput
private let customerRepo: CustomerRepo
private let session: Session
// MARK: - Init
init(interactor: UpgradeInteractorInput,
customerRepo: CustomerRepo,
session: Session,
output: UpgradeModuleOutput?) {
self.interactor = interactor
self.customerRepo = customerRepo
self.session = session
self.output = output
self.subscribeToNotifications()
customerRepo.customerEmitter.addReaction { [weak self] customer -> ShouldContinueReceiveNotifications in
guard let self = self else { return false }
self.update(customer: customer)
return true
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - UpgradeViewOutput
func viewIsReady() {
self.viewInput?.hide()
if let currentCustomer = customerRepo.currentCustomer {
self.update(customer: currentCustomer)
}
}
func didTapActionButton() {
guard let username = self.session.currentRecord?.username
, let token = self.session.currentRecord?.token.value else { return }
self.modalViewInput?.modalLoader(show: true)
self.viewInput?.showLoader(true)
self.interactor.reactivate(token: token, username: username) { [weak self] result in
guard let self = self else { return }
self.viewInput?.showLoader(false)
self.modalViewInput?.modalLoader(show: false)
result
.onSuccess { [weak self] success in
if success {
self?.viewInput?.hide()
self?.customerRepo.getCustomerData { _ in }
}
}
.onFail { error in
print("error: \(error)")
}
}
}
// MARK: - Private
private func update(customer: Customer?) {
guard let customer = customer,
let isActiveVpn = customer.isVPNActive,
let plan = customer.plan,
!customer.isEmpty() else { return }
switch plan {
case .premium:
self.viewInput?.hide()
self.modalViewInput?.modalHide()
if !isActiveVpn {
self.output?.navigate(to: .disconnect)
}
default:
if isActiveVpn {
self.viewInput?.hide()
self.modalViewInput?.modalHide()
} else {
self.output?.navigate(to: .disconnect)
self.displayActionTexts(for: .outPeriod)
}
}
}
private func subscribeToNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveAppAction), name: .appAction, object: nil)
}
private func displayActionTexts(for action: AppAction) {
var title: String?
var body: String?
switch action {
case .outPeriod:
title = NSLocalizedString(Constants.LocalizedString.outPeriodTitle,
comment: "You hit the unrestricted data limit for this month")
body = NSLocalizedString(Constants.LocalizedString.outPeriodBody,
comment: "You can get unlimited data, premium server access, and more today.")
case .outTraffic:
title = NSLocalizedString(Constants.LocalizedString.outTrafficTitle,
comment: "Today’s the day!")
body = NSLocalizedString(Constants.LocalizedString.outTrafficBody,
comment: "Your free account is ready to be refilled to 10 gigs. Click Get More Data below to refresh your account or upgrade for unlimited data.")
}
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if DeviceInfoProvider.isIPad {
let buttonTitle = NSLocalizedString(Constants.LocalizedString.actionButton, comment: "Get More Data")
self.modalViewInput?.modalUpdate(closable: false, imageName: nil, title: title, description: body, action: buttonTitle)
} else {
self.viewInput?.set(title: title, body: body)
}
}
}
@objc
private func didReceiveAppAction(notification: Notification) {
let handledActions: [AppAction] = [.outPeriod, .outTraffic]
guard let action = notification.userInfo?["action"] as? AppAction
, handledActions.contains(action) else { return }
self.output?.navigate(to: .disconnect)
self.displayActionTexts(for: action)
}
}
extension UpgradePresenter: ModalControllerOutput {
func didTapClose() { }
func didTapAction() {
self.didTapActionButton()
}
}