From 8a6fce53bcb1cacc0c816e794d9dbab5ff14d3bc Mon Sep 17 00:00:00 2001 From: Harry <109690906+harry-signal@users.noreply.github.com> Date: Fri, 27 Sep 2024 08:48:51 -0700 Subject: [PATCH] Kill OWSPaymentsLock.shared --- ...aymentsBiometryLockPromptViewController.swift | 2 +- ...mentsViewPassphraseSplashViewController.swift | 4 ++-- .../Privacy/PrivacySettingsViewController.swift | 10 +++++----- .../SendPaymentCompletionActionSheet.swift | 2 +- .../Dependencies/Dependencies+SSK.swift | 16 ++++++++++++++++ SignalServiceKit/Environment/AppSetup.swift | 3 +++ SignalServiceKit/SSKEnvironment.swift | 3 +++ SignalServiceKit/Util/OWSPaymentsLock.swift | 9 ++++----- SignalUI/Views/PaymentOnboarding.swift | 8 ++++---- 9 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Signal/src/ViewControllers/AppSettings/Payments/PaymentsBiometryLockPromptViewController.swift b/Signal/src/ViewControllers/AppSettings/Payments/PaymentsBiometryLockPromptViewController.swift index 14132b6c32..41fd6ffb32 100644 --- a/Signal/src/ViewControllers/AppSettings/Payments/PaymentsBiometryLockPromptViewController.swift +++ b/Signal/src/ViewControllers/AppSettings/Payments/PaymentsBiometryLockPromptViewController.swift @@ -180,7 +180,7 @@ public class PaymentsBiometryLockPromptViewController: OWSViewController { @objc private func didTapEnableButton() { databaseStorage.write { transaction in - OWSPaymentsLock.shared.setIsPaymentsLockEnabled(true, transaction: transaction) + Self.owsPaymentsLock.setIsPaymentsLockEnabled(true, transaction: transaction) } dismiss(animated: true, completion: nil) } diff --git a/Signal/src/ViewControllers/AppSettings/Payments/PaymentsViewPassphraseSplashViewController.swift b/Signal/src/ViewControllers/AppSettings/Payments/PaymentsViewPassphraseSplashViewController.swift index 55d7a51bc5..e1ebd71a12 100644 --- a/Signal/src/ViewControllers/AppSettings/Payments/PaymentsViewPassphraseSplashViewController.swift +++ b/Signal/src/ViewControllers/AppSettings/Payments/PaymentsViewPassphraseSplashViewController.swift @@ -202,8 +202,8 @@ public class PaymentsViewPassphraseSplashViewController: OWSViewController { return } - if OWSPaymentsLock.shared.isPaymentsLockEnabled() { - OWSPaymentsLock.shared.tryToUnlock { [weak self] outcome in + if Self.owsPaymentsLock.isPaymentsLockEnabled() { + Self.owsPaymentsLock.tryToUnlock { [weak self] outcome in guard let self = self else { return } guard outcome == OWSPaymentsLock.LocalAuthOutcome.success else { PaymentActionSheets.showBiometryAuthFailedActionSheet { _ in diff --git a/Signal/src/ViewControllers/AppSettings/Privacy/PrivacySettingsViewController.swift b/Signal/src/ViewControllers/AppSettings/Privacy/PrivacySettingsViewController.swift index aad8659ee7..725a07400d 100644 --- a/Signal/src/ViewControllers/AppSettings/Privacy/PrivacySettingsViewController.swift +++ b/Signal/src/ViewControllers/AppSettings/Privacy/PrivacySettingsViewController.swift @@ -194,7 +194,7 @@ class PrivacySettingsViewController: OWSTableViewController2 { "SETTINGS_PAYMENTS_LOCK_SWITCH_LABEL", comment: "Label for UISwitch based payments-lock setting that when enabled requires biometric-authentication (or passcode) to transfer funds or view the recovery phrase." ), - isOn: { OWSPaymentsLock.shared.isPaymentsLockEnabled() }, + isOn: { Self.owsPaymentsLock.isPaymentsLockEnabled() }, target: self, selector: #selector(didTogglePaymentsLockSwitch) )) @@ -266,8 +266,8 @@ class PrivacySettingsViewController: OWSTableViewController2 { @objc private func didTogglePaymentsLockSwitch(_ sender: UISwitch) { // Require unlock to disable payments lock - if OWSPaymentsLock.shared.isPaymentsLockEnabled() { - OWSPaymentsLock.shared.tryToUnlock { [weak self] outcome in + if Self.owsPaymentsLock.isPaymentsLockEnabled() { + Self.owsPaymentsLock.tryToUnlock { [weak self] outcome in guard let self = self else { return } guard case .success = outcome else { self.updateTableContents() @@ -275,13 +275,13 @@ class PrivacySettingsViewController: OWSTableViewController2 { return } self.databaseStorage.write { transaction in - OWSPaymentsLock.shared.setIsPaymentsLockEnabled(false, transaction: transaction) + Self.owsPaymentsLock.setIsPaymentsLockEnabled(false, transaction: transaction) } self.updateTableContents() } } else { databaseStorage.write { transaction in - OWSPaymentsLock.shared.setIsPaymentsLockEnabled(true, transaction: transaction) + Self.owsPaymentsLock.setIsPaymentsLockEnabled(true, transaction: transaction) } self.updateTableContents() } diff --git a/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift b/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift index ec3226823a..76e46a2efc 100644 --- a/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift +++ b/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift @@ -603,7 +603,7 @@ public class SendPaymentCompletionActionSheet: ActionSheetController { ModalActivityIndicatorViewController.presentAsInvisible(fromViewController: self) { [weak self] modalActivityIndicator in guard let self = self else { return } - OWSPaymentsLock.shared.tryToUnlockPromise().then(on: DispatchQueue.main) { (authOutcome: OWSPaymentsLock.LocalAuthOutcome) -> Promise in + Self.owsPaymentsLock.tryToUnlockPromise().then(on: DispatchQueue.main) { (authOutcome: OWSPaymentsLock.LocalAuthOutcome) -> Promise in switch authOutcome { case .failure(let error): throw PaymentsUIError.paymentsLockFailed(reason: "local authentication failed with error: \(error)") diff --git a/SignalServiceKit/Dependencies/Dependencies+SSK.swift b/SignalServiceKit/Dependencies/Dependencies+SSK.swift index 0c68e0d5fa..57dc79f2ee 100644 --- a/SignalServiceKit/Dependencies/Dependencies+SSK.swift +++ b/SignalServiceKit/Dependencies/Dependencies+SSK.swift @@ -314,6 +314,14 @@ public extension NSObject { SSKEnvironment.shared.paymentsEventsRef } + final var owsPaymentsLock: OWSPaymentsLock { + SSKEnvironment.shared.owsPaymentsLockRef + } + + static var owsPaymentsLock: OWSPaymentsLock { + SSKEnvironment.shared.owsPaymentsLockRef + } + final var spamChallengeResolver: SpamChallengeResolver { SSKEnvironment.shared.spamChallengeResolverRef } @@ -690,6 +698,14 @@ public extension Dependencies { SSKEnvironment.shared.paymentsEventsRef } + var owsPaymentsLock: OWSPaymentsLock { + SSKEnvironment.shared.owsPaymentsLockRef + } + + static var owsPaymentsLock: OWSPaymentsLock { + SSKEnvironment.shared.owsPaymentsLockRef + } + var mobileCoinHelper: MobileCoinHelper { SSKEnvironment.shared.mobileCoinHelperRef } diff --git a/SignalServiceKit/Environment/AppSetup.swift b/SignalServiceKit/Environment/AppSetup.swift index da891fa96e..a0fa6a56d5 100644 --- a/SignalServiceKit/Environment/AppSetup.swift +++ b/SignalServiceKit/Environment/AppSetup.swift @@ -1286,6 +1286,8 @@ public class AppSetup { groupCallPeekClient: groupCallPeekClient ) + let paymentsLock = OWSPaymentsLock() + let sskEnvironment = SSKEnvironment( contactManager: contactManager, messageSender: messageSender, @@ -1326,6 +1328,7 @@ public class AppSetup { paymentsHelper: paymentsHelper, paymentsCurrencies: paymentsCurrencies, paymentsEvents: paymentsEvents, + paymentsLock: paymentsLock, mobileCoinHelper: mobileCoinHelper, spamChallengeResolver: spamChallengeResolver, senderKeyStore: senderKeyStore, diff --git a/SignalServiceKit/SSKEnvironment.swift b/SignalServiceKit/SSKEnvironment.swift index c8f7a6266b..ae55db6b28 100644 --- a/SignalServiceKit/SSKEnvironment.swift +++ b/SignalServiceKit/SSKEnvironment.swift @@ -65,6 +65,7 @@ public class SSKEnvironment: NSObject { public let messageProcessorRef: MessageProcessor public let paymentsCurrenciesRef: PaymentsCurrenciesSwift public let paymentsEventsRef: PaymentsEvents + public let owsPaymentsLockRef: OWSPaymentsLock public let mobileCoinHelperRef: MobileCoinHelper public let spamChallengeResolverRef: SpamChallengeResolver public let senderKeyStoreRef: SenderKeyStore @@ -131,6 +132,7 @@ public class SSKEnvironment: NSObject { paymentsHelper: PaymentsHelperSwift, paymentsCurrencies: PaymentsCurrenciesSwift, paymentsEvents: PaymentsEvents, + paymentsLock: OWSPaymentsLock, mobileCoinHelper: MobileCoinHelper, spamChallengeResolver: SpamChallengeResolver, senderKeyStore: SenderKeyStore, @@ -192,6 +194,7 @@ public class SSKEnvironment: NSObject { self.paymentsHelperRef = paymentsHelper self.paymentsCurrenciesRef = paymentsCurrencies self.paymentsEventsRef = paymentsEvents + self.owsPaymentsLockRef = paymentsLock self.mobileCoinHelperRef = mobileCoinHelper self.spamChallengeResolverRef = spamChallengeResolver self.senderKeyStoreRef = senderKeyStore diff --git a/SignalServiceKit/Util/OWSPaymentsLock.swift b/SignalServiceKit/Util/OWSPaymentsLock.swift index 823c6a92c5..4ed9669ab8 100644 --- a/SignalServiceKit/Util/OWSPaymentsLock.swift +++ b/SignalServiceKit/Util/OWSPaymentsLock.swift @@ -6,7 +6,7 @@ import Foundation import LocalAuthentication -public class OWSPaymentsLock: Dependencies { +public class OWSPaymentsLock: NSObject, Dependencies { public enum LocalAuthOutcome: Equatable { case success @@ -18,9 +18,8 @@ public class OWSPaymentsLock: Dependencies { // MARK: - Singleton class - public static let shared = OWSPaymentsLock() - - init() { + override init() { + super.init() SwiftSingletons.register(self) } @@ -167,7 +166,7 @@ public class OWSPaymentsLock: Dependencies { public func tryToUnlockPromise() -> Promise { Promise(on: DispatchQueue.main) { future in - OWSPaymentsLock.shared.tryToUnlock { outcome in + self.tryToUnlock { outcome in future.resolve(outcome) } } diff --git a/SignalUI/Views/PaymentOnboarding.swift b/SignalUI/Views/PaymentOnboarding.swift index 72cb12ee35..7db15cb9e6 100644 --- a/SignalUI/Views/PaymentOnboarding.swift +++ b/SignalUI/Views/PaymentOnboarding.swift @@ -58,8 +58,8 @@ public class PaymentOnboarding { } public class func presentBiometricLockPromptIfNeeded(completion: @escaping () -> Void) { - guard OWSPaymentsLock.shared.isTimeToShowSuggestion() - && OWSPaymentsLock.shared.isPaymentsLockEnabled() == false + guard NSObject.owsPaymentsLock.isTimeToShowSuggestion() + && NSObject.owsPaymentsLock.isPaymentsLockEnabled() == false else { completion() return @@ -72,7 +72,7 @@ public class PaymentOnboarding { actionSheet.addAction(ActionSheetAction(title: ftPaymentsLockAffirmativeActionTitle(), accessibilityIdentifier: "payments.lock.first_time.affirmative_action", style: .default) { _ in - OWSPaymentsLock.shared.setIsPaymentsLockEnabledAndSnooze(true) + NSObject.owsPaymentsLock.setIsPaymentsLockEnabledAndSnooze(true) completion() }) @@ -82,7 +82,7 @@ public class PaymentOnboarding { style: .cancel ) { _ in Logger.debug("Not Now") - OWSPaymentsLock.shared.setIsPaymentsLockEnabledAndSnooze(false) + NSObject.owsPaymentsLock.setIsPaymentsLockEnabledAndSnooze(false) completion() })