Lock features with alert if beta

This commit is contained in:
Davide De Rosa 2021-01-07 23:37:32 +01:00
parent cc8c01a13a
commit d1cb70a5d9
7 changed files with 60 additions and 19 deletions

View File

@ -162,7 +162,7 @@ extension UISplitViewController {
extension AppDelegate { extension AppDelegate {
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
guard ProductManager.shared.isEligible(forFeature: .siriShortcuts) else { guard (try? ProductManager.shared.isEligible(forFeature: .siriShortcuts)) ?? false else {
return false return false
} }
guard let interaction = userActivity.interaction else { guard let interaction = userActivity.interaction else {

View File

@ -75,6 +75,12 @@ extension UIViewController {
present(nav, animated: true, completion: nil) present(nav, animated: true, completion: nil)
} }
func presentBetaFeatureUnavailable(_ title: String) {
let alert = UIAlertController.asAlert(title, "The requested feature is unavailable in beta.")
alert.addCancelAction("OK")
present(alert, animated: true, completion: nil)
}
} }
func visitURL(_ url: URL) { func visitURL(_ url: URL) {

View File

@ -46,7 +46,7 @@ extension ProductManager {
// review features and potentially revert them if they were used (Siri is handled in AppDelegate) // review features and potentially revert them if they were used (Siri is handled in AppDelegate)
log.debug("Checking 'Trusted networks'") log.debug("Checking 'Trusted networks'")
if !isEligible(forFeature: .trustedNetworks) { if !((try? isEligible(forFeature: .trustedNetworks)) ?? false) {
// reset trusted networks for ALL profiles (must load first) // reset trusted networks for ALL profiles (must load first)
for key in service.allProfileKeys() { for key in service.allProfileKeys() {
@ -76,7 +76,7 @@ extension ProductManager {
guard let metadata = InfrastructureFactory.shared.metadata(forName: name) else { guard let metadata = InfrastructureFactory.shared.metadata(forName: name) else {
continue continue
} }
if !isEligible(forProvider: metadata) { if !((try? isEligible(forProvider: metadata)) ?? false) {
service.removeProfile(ProfileKey(name)) service.removeProfile(ProfileKey(name))
log.debug("\tRefunded provider: \(name)") log.debug("\tRefunded provider: \(name)")
anyRefund = true anyRefund = true

View File

@ -241,8 +241,13 @@ class OrganizerViewController: UITableViewController, StrongTableHost {
} }
private func addShortcuts() { private func addShortcuts() {
guard ProductManager.shared.isEligible(forFeature: .siriShortcuts) else { do {
presentPurchaseScreen(forProduct: .siriShortcuts) guard try ProductManager.shared.isEligible(forFeature: .siriShortcuts) else {
presentPurchaseScreen(forProduct: .siriShortcuts)
return
}
} catch {
presentBetaFeatureUnavailable("Siri")
return return
} }
perform(segue: StoryboardSegue.Organizer.siriShortcutsSegueIdentifier) perform(segue: StoryboardSegue.Organizer.siriShortcutsSegueIdentifier)

View File

@ -70,11 +70,16 @@ class WizardProviderViewController: UITableViewController, StrongTableHost {
private func tryNext(withMetadata metadata: Infrastructure.Metadata, purchaseIfNecessary: Bool) { private func tryNext(withMetadata metadata: Infrastructure.Metadata, purchaseIfNecessary: Bool) {
selectedMetadata = metadata selectedMetadata = metadata
guard ProductManager.shared.isEligible(forProvider: metadata) else { do {
guard purchaseIfNecessary else { guard try ProductManager.shared.isEligible(forProvider: metadata) else {
guard purchaseIfNecessary else {
return
}
presentPurchaseScreen(forProduct: metadata.product, delegate: self)
return return
} }
presentPurchaseScreen(forProduct: metadata.product, delegate: self) } catch {
presentBetaFeatureUnavailable("Providers")
return return
} }

View File

@ -377,11 +377,19 @@ class ServiceViewController: UIViewController, StrongTableHost {
} }
private func trustMobileNetwork(cell: ToggleTableViewCell) { private func trustMobileNetwork(cell: ToggleTableViewCell) {
guard ProductManager.shared.isEligible(forFeature: .trustedNetworks) else { do {
guard try ProductManager.shared.isEligible(forFeature: .trustedNetworks) else {
delay {
cell.setOn(false, animated: true)
}
presentPurchaseScreen(forProduct: .trustedNetworks)
return
}
} catch {
delay { delay {
cell.setOn(false, animated: true) cell.setOn(false, animated: true)
} }
presentPurchaseScreen(forProduct: .trustedNetworks) presentBetaFeatureUnavailable("Trusted networks")
return return
} }
@ -394,8 +402,13 @@ class ServiceViewController: UIViewController, StrongTableHost {
} }
private func trustCurrentWiFi() { private func trustCurrentWiFi() {
guard ProductManager.shared.isEligible(forFeature: .trustedNetworks) else { do {
presentPurchaseScreen(forProduct: .trustedNetworks) guard try ProductManager.shared.isEligible(forFeature: .trustedNetworks) else {
presentPurchaseScreen(forProduct: .trustedNetworks)
return
}
} catch {
presentBetaFeatureUnavailable("Trusted networks")
return return
} }
@ -447,11 +460,19 @@ class ServiceViewController: UIViewController, StrongTableHost {
} }
private func toggleTrustWiFi(cell: ToggleTableViewCell, at row: Int) { private func toggleTrustWiFi(cell: ToggleTableViewCell, at row: Int) {
guard ProductManager.shared.isEligible(forFeature: .trustedNetworks) else { do {
guard try ProductManager.shared.isEligible(forFeature: .trustedNetworks) else {
delay {
cell.setOn(false, animated: true)
}
presentPurchaseScreen(forProduct: .trustedNetworks)
return
}
} catch {
delay { delay {
cell.setOn(false, animated: true) cell.setOn(false, animated: true)
} }
presentPurchaseScreen(forProduct: .trustedNetworks) presentBetaFeatureUnavailable("Trusted networks")
return return
} }

View File

@ -32,6 +32,10 @@ import TunnelKit
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
public enum ProductError: Error {
case beta
}
public class ProductManager: NSObject { public class ProductManager: NSObject {
public struct Configuration { public struct Configuration {
public let isBetaFullVersion: Bool public let isBetaFullVersion: Bool
@ -155,16 +159,16 @@ public class ProductManager: NSObject {
return purchasedFeatures.contains(.fullVersion) return purchasedFeatures.contains(.fullVersion)
} }
public func isEligible(forFeature feature: Product) -> Bool { public func isEligible(forFeature feature: Product) throws -> Bool {
guard !isBeta else { guard !isBeta else {
return false throw ProductError.beta
} }
return isFullVersion() || purchasedFeatures.contains(feature) return isFullVersion() || purchasedFeatures.contains(feature)
} }
public func isEligible(forProvider metadata: Infrastructure.Metadata) -> Bool { public func isEligible(forProvider metadata: Infrastructure.Metadata) throws -> Bool {
guard !isBeta else { guard !isBeta else {
return false throw ProductError.beta
} }
return isFullVersion() || purchasedFeatures.contains(metadata.product) return isFullVersion() || purchasedFeatures.contains(metadata.product)
} }