PurchaseTrialViewController.swift 9.3 KB
Newer Older
Jose Blaya's avatar
WIP  
Jose Blaya committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
//  PurchaseTrialViewController.swift
//  PIALibrary-iOS
//
//  Created by Jose Antonio Blaya Garcia on 06/08/2019.
//  Copyright © 2019 London Trust Media. All rights reserved.
//

import UIKit

class PurchaseTrialViewController: AutolayoutViewController, BrandableNavigationBar, WelcomeChild {
    
    @IBOutlet private weak var scrollView: UIScrollView!
    @IBOutlet private weak var textAgreement: UITextView!
    @IBOutlet private weak var buttonPurchase: PIAButton!
    @IBOutlet private weak var buttonMorePlans: PIAButton!
17
    @IBOutlet private weak var buttonTrialTerms: PIAButton!
Jose Blaya's avatar
WIP  
Jose Blaya committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    @IBOutlet private weak var headerTitleLabel: UILabel!
    @IBOutlet private weak var subtitleLabel: UILabel!
    @IBOutlet private weak var smallTitleLabel: UILabel!

    @IBOutlet private weak var protectionImageView: UIImageView!
    @IBOutlet private weak var protectionTitleLabel: UILabel!
    @IBOutlet private weak var protectionSubtitleLabel: UILabel!

    @IBOutlet private weak var devicesImageView: UIImageView!
    @IBOutlet private weak var devicesTitleLabel: UILabel!
    @IBOutlet private weak var devicesSubtitleLabel: UILabel!

    @IBOutlet private weak var serversImageView: UIImageView!
    @IBOutlet private weak var serversTitleLabel: UILabel!
    @IBOutlet private weak var serversSubtitleLabel: UILabel!

    var preset: Preset?
    weak var completionDelegate: WelcomeCompletionDelegate?
    var omitsSiblingLink = false
    
    private var allPlans: [PurchasePlan] = [.dummy, .dummy]
    
    private var selectedPlanIndex: Int?
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
50 51
        styleButtons()

Jose Blaya's avatar
WIP  
Jose Blaya committed
52 53 54 55 56 57 58 59 60 61 62 63
        guard let _ = self.preset else {
            fatalError("Preset not propagated")
        }
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(
            image: Theme.current.palette.navigationBarBackIcon?.withRenderingMode(.alwaysOriginal),
            style: .plain,
            target: self,
            action: #selector(back(_:))
        )
        self.navigationItem.leftBarButtonItem?.accessibilityLabel = L10n.Welcome.Redeem.Accessibility.back

Jose Blaya's avatar
Jose Blaya committed
64 65 66
        headerTitleLabel.text = L10n.Signup.Purchase.Trials.intro
        subtitleLabel.text = L10n.Signup.Purchase.Trials.Price.after("")
        smallTitleLabel.text = L10n.Signup.Purchase.Trials.Money.back
Jose Blaya's avatar
WIP  
Jose Blaya committed
67
        
Jose Blaya's avatar
Jose Blaya committed
68 69
        protectionTitleLabel.text = L10n.Signup.Purchase.Trials._1year.protection
        protectionSubtitleLabel.text = L10n.Signup.Purchase.Trials.anonymous
Jose Blaya's avatar
WIP  
Jose Blaya committed
70 71
        protectionImageView.image = Asset.shieldIcon.image.withRenderingMode(.alwaysTemplate)
        
Jose Blaya's avatar
Jose Blaya committed
72 73
        devicesTitleLabel.text = L10n.Signup.Purchase.Trials.devices
        devicesSubtitleLabel.text = L10n.Signup.Purchase.Trials.Devices.description
Jose Blaya's avatar
WIP  
Jose Blaya committed
74 75
        devicesImageView.image = Asset.computerIcon.image.withRenderingMode(.alwaysTemplate)
        
Jose Blaya's avatar
Jose Blaya committed
76 77
        serversTitleLabel.text = L10n.Signup.Purchase.Trials.region
        serversSubtitleLabel.text = L10n.Signup.Purchase.Trials.servers
Jose Blaya's avatar
WIP  
Jose Blaya committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        serversImageView.image = Asset.globeIcon.image.withRenderingMode(.alwaysTemplate)
        
        textAgreement.attributedText = Theme.current.agreementText(
            withMessage: L10n.Welcome.Agreement.message,
            tos: L10n.Welcome.Agreement.Message.tos,
            tosUrl: Client.configuration.tosUrl,
            privacy: L10n.Welcome.Agreement.Message.privacy,
            privacyUrl: Client.configuration.privacyUrl
        )
        let nc = NotificationCenter.default
        nc.addObserver(self, selector: #selector(productsDidFetch(notification:)), name: .__InAppDidFetchProducts, object: nil)
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if let products = preset?.accountProvider.planProducts {
            refreshPlans(products)
        } else {
            disableInteractions(fully: false)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        NotificationCenter.default.removeObserver(self)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == StoryboardSegue.Welcome.confirmPurchaseVPNPlanSegue.rawValue) {
            if let vc = segue.destination as? ConfirmVPNPlanViewController,
                let selectedIndex = selectedPlanIndex {
                vc.preset = preset
                vc.completionDelegate = completionDelegate
                vc.populateViewWith(plans: allPlans,
                                    andSelectedPlanIndex: selectedIndex)
            }
        } else if (segue.identifier == StoryboardSegue.Welcome.viewMoreVPNPlansSegue.rawValue) {
            if let vc = segue.destination as? PurchaseViewController {
                vc.preset = preset
                vc.completionDelegate = completionDelegate
            }
122 123 124 125 126
        } else if (segue.identifier == StoryboardSegue.Welcome.showTermsAndConditionsSegue.rawValue) {
            if let vc = segue.destination as? TermsAndConditionsViewController {
                vc.termsAndConditionsTitle = L10n.Welcome.Agreement.Trials.title
                vc.termsAndConditions = L10n.Welcome.Agreement.Trials.message
            }
Jose Blaya's avatar
WIP  
Jose Blaya committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        }
    }
    
    // MARK: Actions
    
    private func refreshPlans(_ plans: [Plan: InAppProduct]) {
        if let yearly = plans[.yearly] {
            let purchase = PurchasePlan(
                plan: .yearly,
                product: yearly,
                monthlyFactor: 12.0
            )
            
            purchase.title = L10n.Welcome.Plan.Yearly.title
            let currencySymbol = purchase.product.priceLocale.currencySymbol ?? ""
            purchase.detail = L10n.Welcome.Plan.Yearly.detailFormat(currencySymbol, purchase.product.price.description)
            purchase.bestValue = true
Jose Blaya's avatar
Jose Blaya committed
144
            let price = L10n.Welcome.Plan.Yearly.detailFormat(currencySymbol, purchase.product.price.description)
Jose Blaya's avatar
Jose Blaya committed
145
            subtitleLabel.text = L10n.Signup.Purchase.Trials.Price.after(price)
Jose Blaya's avatar
Jose Blaya committed
146 147
            Theme.current.makeSmallLabelToStandOut(subtitleLabel,
                                                   withTextToStandOut: price)
Jose Blaya's avatar
WIP  
Jose Blaya committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            allPlans[0] = purchase
            selectedPlanIndex = 0
        }
    }
    
    private func disableInteractions(fully: Bool) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.showLoadingAnimation()
        }
        if fully {
            parent?.view.isUserInteractionEnabled = false
        }
    }
    
    private func enableInteractions() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.hideLoadingAnimation()
        }
        parent?.view.isUserInteractionEnabled = true
    }
    
    // MARK: Notifications
    
    @objc private func productsDidFetch(notification: Notification) {
        let products: [Plan: InAppProduct] = notification.userInfo(for: .products)
        refreshPlans(products)
        enableInteractions()
    }
    
    // MARK: Restylable
    
    override func viewShouldRestyle() {
        super.viewShouldRestyle()
Jose Blaya's avatar
Jose Blaya committed
181 182
        navigationItem.titleView = NavigationLogoView()
        Theme.current.applyNavigationBarStyle(to: self)
Jose Blaya's avatar
WIP  
Jose Blaya committed
183 184 185 186
        Theme.current.applyPrincipalBackground(view)
        Theme.current.applyLinkAttributes(textAgreement)
        Theme.current.applyBigTitle(headerTitleLabel, appearance: .dark)
        Theme.current.applySmallSubtitle(smallTitleLabel)
Jose Blaya's avatar
Jose Blaya committed
187
        Theme.current.applySubtitle(subtitleLabel)
Jose Blaya's avatar
wip  
Jose Blaya committed
188

Jose Blaya's avatar
Jose Blaya committed
189 190 191 192 193 194 195 196 197 198
        if Theme.current.palette.appearance == .light {
            protectionImageView.tintColor = Theme.current.palette.lineColor
            devicesImageView.tintColor = Theme.current.palette.lineColor
            serversImageView.tintColor = Theme.current.palette.lineColor
        } else {
            protectionImageView.tintColor = .white
            devicesImageView.tintColor = .white
            serversImageView.tintColor = .white
        }
        
Jose Blaya's avatar
WIP  
Jose Blaya committed
199 200
        Theme.current.applySettingsCellTitle(protectionTitleLabel, appearance: .dark)
        Theme.current.applySmallSubtitle(protectionSubtitleLabel)
Jose Blaya's avatar
Jose Blaya committed
201
        
Jose Blaya's avatar
WIP  
Jose Blaya committed
202 203 204 205 206 207 208 209 210 211 212 213 214
        Theme.current.applySettingsCellTitle(devicesTitleLabel, appearance: .dark)
        Theme.current.applySmallSubtitle(devicesSubtitleLabel)

        Theme.current.applySettingsCellTitle(serversTitleLabel, appearance: .dark)
        Theme.current.applySmallSubtitle(serversSubtitleLabel)

    }
    
    private func styleButtons() {
        buttonPurchase.setRounded()
        buttonMorePlans.setRounded()
        buttonPurchase.style(style: TextStyle.Buttons.piaGreenButton)
        buttonMorePlans.style(style: TextStyle.Buttons.piaPlainTextButton)
215
        buttonTrialTerms.style(style: TextStyle.Buttons.piaSmallPlainTextButton)
Jose Blaya's avatar
Jose Blaya committed
216
        buttonPurchase.setTitle(L10n.Signup.Purchase.Trials.start.uppercased(),
Jose Blaya's avatar
WIP  
Jose Blaya committed
217
                                for: [])
Jose Blaya's avatar
Jose Blaya committed
218
        buttonMorePlans.setTitle(L10n.Signup.Purchase.Trials.All.plans,
Jose Blaya's avatar
WIP  
Jose Blaya committed
219
                                for: [])
220 221
        buttonTrialTerms.setTitle(L10n.Welcome.Agreement.Trials.title,
                                 for: [])
Jose Blaya's avatar
Jose Blaya committed
222 223
        Theme.current.applyTransparentButton(buttonMorePlans,
                                             withSize: 1.0)
224 225
        Theme.current.applyTransparentButton(buttonTrialTerms,
                                             withSize: 0.0)
Jose Blaya's avatar
WIP  
Jose Blaya committed
226 227 228
    }
    
}