-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIA-882: Decoupled PIALibrary from presentation and domain layers
- Loading branch information
1 parent
9eca3a8
commit 8ce2fc4
Showing
17 changed files
with
363 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// UserAccountMapper.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Said Rehouni on 12/12/23. | ||
// Copyright © 2023 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import PIALibrary | ||
|
||
class UserAccountMapper { | ||
func map(userAccount: PIALibrary.UserAccount) -> UserAccount { | ||
let credentials = Credentials(username: userAccount.credentials.username, | ||
password: userAccount.credentials.password) | ||
|
||
guard let info = userAccount.info else { | ||
return UserAccount(credentials: credentials, info: nil) | ||
} | ||
|
||
let accountInfo = AccountInfo(email: info.email, | ||
username: info.username, | ||
plan: Plan.map(plan: info.plan), | ||
productId: info.productId, | ||
isRenewable: info.isRenewable, | ||
isRecurring: info.isRecurring, | ||
expirationDate: info.expirationDate, | ||
canInvite: info.canInvite, | ||
shouldPresentExpirationAlert: info.shouldPresentExpirationAlert, | ||
renewUrl: info.renewUrl) | ||
|
||
return UserAccount(credentials: credentials, info: accountInfo) | ||
} | ||
} | ||
|
||
extension Plan { | ||
static func map(plan: PIALibrary.Plan) -> Plan { | ||
switch plan { | ||
case .monthly: | ||
return .monthly | ||
case .yearly: | ||
return .yearly | ||
case .trial: | ||
return .trial | ||
case .other: | ||
return .other | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// AccountInfo.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Said Rehouni on 12/12/23. | ||
// Copyright © 2023 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Plan: String { | ||
case monthly | ||
case yearly | ||
case trial | ||
case other | ||
} | ||
|
||
struct AccountInfo { | ||
let email: String? | ||
let username: String | ||
let plan: Plan | ||
let productId: String? | ||
let isRenewable: Bool | ||
let isRecurring: Bool | ||
let expirationDate: Date | ||
let canInvite: Bool | ||
|
||
public var isExpired: Bool { | ||
return (expirationDate.timeIntervalSinceNow < 0) | ||
} | ||
|
||
public var dateComponentsBeforeExpiration: DateComponents { | ||
return Calendar.current.dateComponents([.day, .hour], from: Date(), to: expirationDate) | ||
} | ||
|
||
public let shouldPresentExpirationAlert: Bool | ||
public let renewUrl: URL? | ||
|
||
public func humanReadableExpirationDate(usingLocale locale: Locale = Locale.current) -> String { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateStyle = .long | ||
dateFormatter.timeStyle = .none | ||
dateFormatter.locale = locale | ||
return dateFormatter.string(from: self.expirationDate) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Credentials.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Said Rehouni on 12/12/23. | ||
// Copyright © 2023 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Credentials { | ||
let username: String | ||
let password: String | ||
|
||
init(username: String, password: String) { | ||
self.username = username | ||
self.password = password | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// UserAccount.swift | ||
// PIA VPN-tvOS | ||
// | ||
// Created by Said Rehouni on 12/12/23. | ||
// Copyright © 2023 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct UserAccount { | ||
let credentials: Credentials | ||
let info: AccountInfo? | ||
|
||
var isRenewable: Bool { | ||
return info?.isRenewable ?? false | ||
} | ||
|
||
init(credentials: Credentials, info: AccountInfo?) { | ||
self.credentials = credentials | ||
self.info = info | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// AccountProviderMock.swift | ||
// PIA VPN-tvOSTests | ||
// | ||
// Created by Said Rehouni on 12/12/23. | ||
// Copyright © 2023 Private Internet Access Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import PIALibrary | ||
|
||
class AccountProviderMock: AccountProvider { | ||
var planProducts: [PIALibrary.Plan : PIALibrary.InAppProduct]? | ||
var shouldCleanAccount: Bool = true | ||
var isLoggedIn: Bool = true | ||
var currentUser: PIALibrary.UserAccount? | ||
var oldToken: String? | ||
var apiToken: String? | ||
var vpnToken: String? | ||
var vpnTokenUsername: String? | ||
var vpnTokenPassword: String? | ||
var publicUsername: String? | ||
var currentPasswordReference: Data? | ||
var lastSignupRequest: PIALibrary.SignupRequest? | ||
func migrateOldTokenIfNeeded(_ callback: PIALibrary.SuccessLibraryCallback?) {} | ||
|
||
private let userResult: PIALibrary.UserAccount? | ||
private let errorResult: Error? | ||
|
||
init(userResult: PIALibrary.UserAccount?, errorResult: Error?) { | ||
self.userResult = userResult | ||
self.errorResult = errorResult | ||
} | ||
|
||
func login(with request: PIALibrary.LoginRequest, _ callback: PIALibrary.LibraryCallback<PIALibrary.UserAccount>?) { | ||
callback?(userResult, errorResult) | ||
} | ||
|
||
func login(with receiptRequest: PIALibrary.LoginReceiptRequest, _ callback: PIALibrary.LibraryCallback<PIALibrary.UserAccount>?) {} | ||
func login(with linkToken: String, _ callback: ((PIALibrary.UserAccount?, Error?) -> Void)?) {} | ||
func refreshAccountInfo(_ callback: PIALibrary.LibraryCallback<PIALibrary.AccountInfo>?) {} | ||
func accountInformation(_ callback: ((PIALibrary.AccountInfo?, Error?) -> Void)?) {} | ||
func update(with request: PIALibrary.UpdateAccountRequest, resetPassword reset: Bool, andPassword password: String, _ callback: PIALibrary.LibraryCallback<PIALibrary.AccountInfo>?) {} | ||
func logout(_ callback: PIALibrary.SuccessLibraryCallback?) {} | ||
func deleteAccount(_ callback: PIALibrary.SuccessLibraryCallback?) {} | ||
func cleanDatabase() {} | ||
func featureFlags(_ callback: PIALibrary.SuccessLibraryCallback?) {} | ||
func listPlanProducts(_ callback: PIALibrary.LibraryCallback<[PIALibrary.Plan : PIALibrary.InAppProduct]>?) {} | ||
func purchase(plan: PIALibrary.Plan, _ callback: PIALibrary.LibraryCallback<PIALibrary.InAppTransaction>?) {} | ||
func isAPIEndpointAvailable(_ callback: PIALibrary.LibraryCallback<Bool>?) {} | ||
func restorePurchases(_ callback: PIALibrary.SuccessLibraryCallback?) {} | ||
func loginUsingMagicLink(withEmail email: String, _ callback: PIALibrary.SuccessLibraryCallback?) {} | ||
func signup(with request: PIALibrary.SignupRequest, _ callback: PIALibrary.LibraryCallback<PIALibrary.UserAccount>?) {} | ||
func listRenewablePlans(_ callback: PIALibrary.LibraryCallback<[PIALibrary.Plan]>?) {} | ||
func subscriptionInformation(_ callback: PIALibrary.LibraryCallback<PIALibrary.AppStoreInformation>?) {} | ||
func renew(with request: PIALibrary.RenewRequest, _ callback: PIALibrary.LibraryCallback<PIALibrary.UserAccount>?) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.