-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subscription refactoring - UI handler (#2853)
Task/Issue URL: https://app.asana.com/0/1205842942115003/1206805455884775/f All UI interactions have been extracted from Subscription business logic and isolated in a mockable UI handler
- Loading branch information
1 parent
f072edf
commit e688145
Showing
8 changed files
with
274 additions
and
118 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
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,111 @@ | ||
// | ||
// SubscriptionUIHandler.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import SubscriptionUI | ||
|
||
@MainActor | ||
final class SubscriptionUIHandler: SubscriptionUIHandling { | ||
|
||
fileprivate var currentWindow: NSWindow? { windowControllersManagerProvider().lastKeyMainWindowController?.window } | ||
fileprivate var currentMainViewController: MainViewController? { | ||
windowControllersManagerProvider().lastKeyMainWindowController?.mainViewController | ||
} | ||
fileprivate var windowControllersManager: WindowControllersManager { windowControllersManagerProvider() } | ||
typealias WindowControllersManagerProvider = () -> WindowControllersManager | ||
fileprivate nonisolated let windowControllersManagerProvider: WindowControllersManagerProvider | ||
fileprivate var progressViewController: ProgressViewController? | ||
|
||
nonisolated init(windowControllersManagerProvider: @escaping WindowControllersManagerProvider) { | ||
self.windowControllersManagerProvider = windowControllersManagerProvider | ||
} | ||
|
||
// MARK: - SubscriptionUIHandling | ||
|
||
func presentProgressViewController(withTitle: String) { | ||
progressViewController = ProgressViewController(title: UserText.purchasingSubscriptionTitle) | ||
currentMainViewController?.presentAsSheet(progressViewController!) | ||
} | ||
|
||
func dismissProgressViewController() { | ||
progressViewController?.dismiss() | ||
progressViewController = nil | ||
} | ||
|
||
func updateProgressViewController(title: String) { | ||
progressViewController?.updateTitleText(UserText.completingPurchaseTitle) | ||
} | ||
|
||
func presentSubscriptionAccessViewController(handler: any SubscriptionAccessActionHandling, message: WKScriptMessage) { | ||
let actionHandlers = SubscriptionAccessActionHandlers(restorePurchases: { | ||
handler.subscriptionAccessActionRestorePurchases(message: message) | ||
}, openURLHandler: { url in | ||
handler.subscriptionAccessActionOpenURLHandler(url: url) | ||
}, uiActionHandler: { event in | ||
handler.subscriptionAccessActionHandleAction(event: event) | ||
}) | ||
|
||
let newSubscriptionAccessViewController = SubscriptionAccessViewController(subscriptionManager: Application.appDelegate.subscriptionManager, | ||
actionHandlers: actionHandlers) | ||
currentMainViewController?.presentAsSheet(newSubscriptionAccessViewController) | ||
} | ||
|
||
func show(alertType: SubscriptionAlertType, text: String? = nil, firstButtonAction: (() -> Void)? = nil) { | ||
|
||
var alert: NSAlert? | ||
switch alertType { | ||
case .somethingWentWrong: | ||
alert = .somethingWentWrongAlert() | ||
case .subscriptionNotFound: | ||
alert = .subscriptionNotFoundAlert() | ||
case .subscriptionInactive: | ||
alert = .subscriptionInactiveAlert() | ||
case .subscriptionFound: | ||
alert = .subscriptionFoundAlert() | ||
case .appleIDSyncFailed: | ||
guard let text else { | ||
assertionFailure("Trying to present appleIDSyncFailed alert without required text") | ||
return | ||
} | ||
alert = .appleIDSyncFailedAlert(text: text) | ||
} | ||
|
||
guard let alert else { | ||
assertionFailure("Missing subscription alert") | ||
return | ||
} | ||
|
||
currentWindow?.show(alert, firstButtonAction: firstButtonAction) | ||
} | ||
|
||
func show(alertType: SubscriptionAlertType) { | ||
show(alertType: alertType, text: nil, firstButtonAction: nil) | ||
} | ||
|
||
func show(alertType: SubscriptionAlertType, firstButtonAction: (() -> Void)?) { | ||
show(alertType: alertType, text: nil, firstButtonAction: firstButtonAction) | ||
} | ||
|
||
func show(alertType: SubscriptionAlertType, text: String?) { | ||
show(alertType: alertType, text: text, firstButtonAction: nil) | ||
} | ||
|
||
func showTab(with content: Tab.TabContent) { | ||
self.windowControllersManager.showTab(with: content) | ||
} | ||
} |
Oops, something went wrong.