Skip to content

Commit

Permalink
allow user to switch between accounts when the account number is ente…
Browse files Browse the repository at this point in the history
…red instead of voucher code
  • Loading branch information
mojganii committed Sep 6, 2023
1 parent 02715c2 commit ad46086
Show file tree
Hide file tree
Showing 17 changed files with 496 additions and 133 deletions.
53 changes: 32 additions & 21 deletions ios/MullvadVPN.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions ios/MullvadVPN/Coordinators/AccountCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ final class AccountCoordinator: Coordinator, Presentable, Presenting {
}

private func navigateToRedeemVoucher() {
let coordinator = SettingsRedeemVoucherCoordinator(
let coordinator = AccountVoucherCoordinator(
navigationController: CustomNavigationController(),
interactor: RedeemVoucherInteractor(tunnelManager: interactor.tunnelManager)
interactor: AccountVoucherInteractor(tunnelManager: interactor.tunnelManager)
)
coordinator.didFinish = { redeemVoucherCoordinator in
redeemVoucherCoordinator.dismiss(animated: true)
coordinator.didFinish = { coordinator in
coordinator.dismiss(animated: true)
}
coordinator.didCancel = { redeemVoucherCoordinator in
redeemVoucherCoordinator.dismiss(animated: true)
coordinator.didCancel = { coordinator in
coordinator.dismiss(animated: true)
}

coordinator.start()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SettingsRedeemVoucherCoordinator.swift
// AccountVoucherCoordinator.swift
// MullvadVPN
//
// Created by Mojgan on 2023-06-13.
Expand All @@ -11,15 +11,16 @@ import MullvadREST
import Routing
import UIKit

final class SettingsRedeemVoucherCoordinator: Coordinator, Presentable {
final class AccountVoucherCoordinator: Coordinator, Presentable {
private let navigationController: UINavigationController
private let viewController: RedeemVoucherViewController
var didFinish: ((SettingsRedeemVoucherCoordinator) -> Void)?
var didCancel: ((SettingsRedeemVoucherCoordinator) -> Void)?

var didFinish: ((AccountVoucherCoordinator) -> Void)?
var didCancel: ((AccountVoucherCoordinator) -> Void)?

init(
navigationController: UINavigationController,
interactor: RedeemVoucherInteractor
interactor: AccountVoucherInteractor
) {
self.navigationController = navigationController
viewController = RedeemVoucherViewController(interactor: interactor)
Expand All @@ -36,7 +37,7 @@ final class SettingsRedeemVoucherCoordinator: Coordinator, Presentable {
}
}

extension SettingsRedeemVoucherCoordinator: RedeemVoucherViewControllerDelegate {
extension AccountVoucherCoordinator: RedeemVoucherViewControllerDelegate {
func redeemVoucherDidSucceed(
_ controller: RedeemVoucherViewController,
with response: REST.SubmitVoucherResponse
Expand All @@ -49,17 +50,21 @@ extension SettingsRedeemVoucherCoordinator: RedeemVoucherViewControllerDelegate
func redeemVoucherDidCancel(_ controller: RedeemVoucherViewController) {
didCancel?(self)
}

func viewForInputingAccountNumberAsVoucher(_ controller: RedeemVoucherViewController) -> UIView? {
nil
}
}

extension SettingsRedeemVoucherCoordinator: AddCreditSucceededViewControllerDelegate {
extension AccountVoucherCoordinator: AddCreditSucceededViewControllerDelegate {
func addCreditSucceededViewControllerDidFinish(in controller: AddCreditSucceededViewController) {
didFinish?(self)
}

func header(in controller: AddCreditSucceededViewController) -> String {
NSLocalizedString(
"REDEEM_VOUCHER_SUCCESS_TITLE",
tableName: "RedeemVoucher",
tableName: "AccountRedeemVoucher",
value: "Voucher was successfully redeemed.",
comment: ""
)
Expand All @@ -68,7 +73,7 @@ extension SettingsRedeemVoucherCoordinator: AddCreditSucceededViewControllerDele
func titleForAction(in controller: AddCreditSucceededViewController) -> String {
NSLocalizedString(
"REDEEM_VOUCHER_DISMISS_BUTTON",
tableName: "RedeemVoucher",
tableName: "AccountRedeemVoucher",
value: "Got it!",
comment: ""
)
Expand Down
12 changes: 10 additions & 2 deletions ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ final class ApplicationCoordinator: Coordinator, Presenting, RootContainerViewCo

private let apiProxy: REST.APIProxy
private let devicesProxy: REST.DevicesProxy
private let accountsProxy: REST.AccountsProxy
private var tunnelObserver: TunnelObserver?
private var appPreferences: AppPreferencesDataSource

Expand All @@ -85,13 +86,15 @@ final class ApplicationCoordinator: Coordinator, Presenting, RootContainerViewCo
relayCacheTracker: RelayCacheTracker,
apiProxy: REST.APIProxy,
devicesProxy: REST.DevicesProxy,
accountsProxy: REST.AccountsProxy,
appPreferences: AppPreferencesDataSource
) {
self.tunnelManager = tunnelManager
self.storePaymentManager = storePaymentManager
self.relayCacheTracker = relayCacheTracker
self.apiProxy = apiProxy
self.devicesProxy = devicesProxy
self.accountsProxy = accountsProxy
self.appPreferences = appPreferences

super.init()
Expand Down Expand Up @@ -593,15 +596,20 @@ final class ApplicationCoordinator: Coordinator, Presenting, RootContainerViewCo
let coordinator = WelcomeCoordinator(
navigationController: horizontalFlowController,
storePaymentManager: storePaymentManager,
tunnelManager: tunnelManager
tunnelManager: tunnelManager,
accountsProxy: accountsProxy
)

coordinator.didFinish = { [weak self] _ in
guard let self else { return }
appPreferences.isShownOnboarding = true
router.dismiss(.welcome, animated: false)
continueFlow(animated: false)
}
coordinator.didLogout = { [weak self] _ in
guard let self else { return }
router.dismissAll(.primary, animated: true)
continueFlow(animated: true)
}

addChild(coordinator)
coordinator.start(animated: animated)
Expand Down
115 changes: 115 additions & 0 deletions ios/MullvadVPN/Coordinators/CreateAccountVoucherCoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// CreateAccountVoucherCoordinator.swift
// MullvadVPN
//
// Created by Mojgan on 2023-07-03.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

import MullvadREST
import Routing
import UIKit

public class CreateAccountVoucherCoordinator: Coordinator, Presentable {
private let navigationController: RootContainerViewController
private let viewController: RedeemVoucherViewController
private let interactor: CreateAccountVoucherInteractor

private var accountNumber: String?
private lazy var accountNumberInputErrorView = {
VerifiedAccountView {
self.logout()
}
}()

var didFinish: ((CreateAccountVoucherCoordinator) -> Void)?
var didCancel: ((CreateAccountVoucherCoordinator) -> Void)?
var didLogout: ((CreateAccountVoucherCoordinator) -> Void)?

/**
Name of notification posted when current account number changes.
*/
static let didChangePreferredAccountNumber = Notification
.Name(rawValue: "CreateAccountVoucherCoordinatorDidChangeAccountNumber")

/**
User info key passed along with `didChangePreferredAccountNumber` notification that contains string value that
indicates the new account number.
*/
static let preferredAccountNumberUserInfoKey = "preferredAccountNumber"

public var presentedViewController: UIViewController {
viewController
}

init(
navigationController: RootContainerViewController,
tunnelManager: TunnelManager,
accountsProxy: REST.AccountsProxy
) {
self.navigationController = navigationController
interactor = CreateAccountVoucherInteractor(tunnelManager: tunnelManager, accountsProxy: accountsProxy)
viewController = RedeemVoucherViewController(interactor: interactor)
}

func start() {
viewController.delegate = self
interactor.didInputAccountNumber = { [weak self] value in
guard let self else { return }
accountNumberInputErrorView.fadeIn()
accountNumber = value
}
navigationController.pushViewController(viewController, animated: true)
}

private func logout() {
let alertController = CustomAlertViewController(icon: .spinner)
presentedViewController.present(alertController, animated: true, completion: {
self.interactor.logout { [weak self] in
alertController.dismiss(animated: true, completion: {
self?.accountNumber.flatMap {
guard let self else { return }
self.didLogout?(self)
self.notify(accountNumber: $0)
}
})
}
})
}

/// Posts `didChangePreferredAccountNumber` notification.
private func notify(accountNumber: String) {
NotificationCenter.default.post(
name: Self.didChangePreferredAccountNumber,
object: self,
userInfo: [Self.preferredAccountNumberUserInfoKey: accountNumber]
)
}
}

extension CreateAccountVoucherCoordinator: RedeemVoucherViewControllerDelegate {
func redeemVoucherDidSucceed(_ controller: RedeemVoucherViewController, with response: REST.SubmitVoucherResponse) {
let coordinator = AddCreditSucceededCoordinator(
purchaseType: .redeemingVoucher,
timeAdded: response.timeAdded,
navigationController: navigationController
)

coordinator.didFinish = { [weak self] coordinator in
coordinator.removeFromParent()
guard let self else { return }
didFinish?(self)
}

addChild(coordinator)
coordinator.start()
}

func redeemVoucherDidCancel(_ controller: RedeemVoucherViewController) {
didCancel?(self)
}

func viewForInputingAccountNumberAsVoucher(_ controller: RedeemVoucherViewController) -> UIView? {
accountNumberInputErrorView
}
}
17 changes: 14 additions & 3 deletions ios/MullvadVPN/Coordinators/WelcomeCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ final class WelcomeCoordinator: Coordinator, Presentable, Presenting {
private let storePaymentManager: StorePaymentManager
private let tunnelManager: TunnelManager
private let inAppPurchaseInteractor: InAppPurchaseInteractor
private let accountsProxy: REST.AccountsProxy

private var viewController: WelcomeViewController?

var didFinish: ((WelcomeCoordinator) -> Void)?
var didLogout: ((WelcomeCoordinator) -> Void)?

var presentedViewController: UIViewController {
navigationController
Expand All @@ -33,11 +35,13 @@ final class WelcomeCoordinator: Coordinator, Presentable, Presenting {
init(
navigationController: RootContainerViewController,
storePaymentManager: StorePaymentManager,
tunnelManager: TunnelManager
tunnelManager: TunnelManager,
accountsProxy: REST.AccountsProxy
) {
self.navigationController = navigationController
self.storePaymentManager = storePaymentManager
self.tunnelManager = tunnelManager
self.accountsProxy = accountsProxy
self.inAppPurchaseInteractor = InAppPurchaseInteractor(storePaymentManager: storePaymentManager)
}

Expand Down Expand Up @@ -140,9 +144,10 @@ extension WelcomeCoordinator: WelcomeViewControllerDelegate {
}

func didRequestToRedeemVoucher(controller: WelcomeViewController) {
let coordinator = AccountRedeemingVoucherCoordinator(
let coordinator = CreateAccountVoucherCoordinator(
navigationController: navigationController,
interactor: RedeemVoucherInteractor(tunnelManager: tunnelManager)
tunnelManager: tunnelManager,
accountsProxy: accountsProxy
)

coordinator.didCancel = { [weak self] coordinator in
Expand All @@ -157,6 +162,12 @@ extension WelcomeCoordinator: WelcomeViewControllerDelegate {
didFinish?(self)
}

coordinator.didLogout = { [weak self] voucherCoordinator in
voucherCoordinator.removeFromParent()
guard let self else { return }
didLogout?(self)
}

addChild(coordinator)

coordinator.start()
Expand Down
1 change: 1 addition & 0 deletions ios/MullvadVPN/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, SettingsMigrationUIHand
relayCacheTracker: appDelegate.relayCacheTracker,
apiProxy: appDelegate.apiProxy,
devicesProxy: appDelegate.devicesProxy,
accountsProxy: appDelegate.accountsProxy,
appPreferences: AppPreferences()
)

Expand Down
Loading

0 comments on commit ad46086

Please sign in to comment.