summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2020-03-30 12:40:45 +0200
committerAndrej Mihajlov <and@mullvad.net>2020-03-30 14:39:19 +0200
commitda700859e6a3d3a22f1784e520c650be067e5c19 (patch)
tree23a846045773aa74a295f32f0d844002caa7d474 /ios/MullvadVPN
parenteb58ceec08e3c387e055a0d10fad5c4dfb2711c7 (diff)
downloadmullvadvpn-da700859e6a3d3a22f1784e520c650be067e5c19.tar.xz
mullvadvpn-da700859e6a3d3a22f1784e520c650be067e5c19.zip
Format account token in Account view
Diffstat (limited to 'ios/MullvadVPN')
-rw-r--r--ios/MullvadVPN/Account.swift4
-rw-r--r--ios/MullvadVPN/AccountViewController.swift9
-rw-r--r--ios/MullvadVPN/String+Split.swift22
3 files changed, 30 insertions, 5 deletions
diff --git a/ios/MullvadVPN/Account.swift b/ios/MullvadVPN/Account.swift
index d05dea3cbd..cc1f10e8e6 100644
--- a/ios/MullvadVPN/Account.swift
+++ b/ios/MullvadVPN/Account.swift
@@ -97,6 +97,10 @@ class Account {
return UserDefaults.standard.string(forKey: UserDefaultsKeys.accountToken.rawValue)
}
+ var formattedToken: String? {
+ return token?.split(every: 4).joined(separator: " ")
+ }
+
/// Returns the account expiry for the currently used account token
var expiry: Date? {
return UserDefaults.standard.object(forKey: UserDefaultsKeys.accountExpiry.rawValue) as? Date
diff --git a/ios/MullvadVPN/AccountViewController.swift b/ios/MullvadVPN/AccountViewController.swift
index e7ed9b7ef3..dcd631d275 100644
--- a/ios/MullvadVPN/AccountViewController.swift
+++ b/ios/MullvadVPN/AccountViewController.swift
@@ -66,7 +66,7 @@ class AccountViewController: UIViewController {
purchaseButton.titleLabel?.adjustsFontSizeToFitWidth = true
purchaseButton.titleLabel?.baselineAdjustment = .alignCenters
- accountTokenButton.setTitle(Account.shared.token, for: .normal)
+ accountTokenButton.setTitle(Account.shared.formattedToken, for: .normal)
if let expiryDate = Account.shared.expiry {
updateAccountExpiry(expiryDate: expiryDate)
@@ -182,16 +182,15 @@ class AccountViewController: UIViewController {
}
@IBAction func copyAccountToken() {
- let accountToken = Account.shared.token
-
- UIPasteboard.general.string = accountToken
+ UIPasteboard.general.string = Account.shared.token
accountTokenButton.setTitle(
NSLocalizedString("COPIED TO PASTEBOARD!", comment: ""),
for: .normal)
copyToPasteboardSubscriber =
- Just(accountToken).cancellableDelay(for: .seconds(3), scheduler: DispatchQueue.main)
+ Just(Account.shared.formattedToken)
+ .cancellableDelay(for: .seconds(3), scheduler: DispatchQueue.main)
.sink(receiveValue: { [weak self] (accountToken) in
self?.accountTokenButton.setTitle(accountToken, for: .normal)
})
diff --git a/ios/MullvadVPN/String+Split.swift b/ios/MullvadVPN/String+Split.swift
new file mode 100644
index 0000000000..82352280fc
--- /dev/null
+++ b/ios/MullvadVPN/String+Split.swift
@@ -0,0 +1,22 @@
+//
+// String+Split.swift
+// MullvadVPN
+//
+// Created by pronebird on 27/03/2020.
+// Copyright © 2020 Mullvad VPN AB. All rights reserved.
+//
+
+import Foundation
+
+extension String {
+
+ /// Returns the array of the longest possible subsequences of the given length.
+ func split(every length: Int) -> [Substring] {
+ guard length > 0 else { return [prefix(upTo: endIndex)] }
+
+ let resultCount = Int((Float(count) / Float(length)).rounded(.up))
+
+ return (0 ..< resultCount)
+ .map { dropFirst($0 * length).prefix(length) }
+ }
+}