-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathAddSiteController.swift
120 lines (106 loc) · 4.45 KB
/
AddSiteController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import UIKit
import SwiftUI
import WordPressAuthenticator
import WordPressData
/// Manages the site creation flows.
struct AddSiteController {
let viewController: UIViewController
let source: String
func showSiteCreationScreen(selection: AddSiteMenuViewModel.Selection) {
switch selection {
case .dotCom: showDotComSiteCreationScreen()
case .selfHosted: showSelfHostedSiteLoginScreen()
}
}
func showDotComSiteCreationScreen() {
JetpackFeaturesRemovalCoordinator.presentSiteCreationOverlayIfNeeded(in: viewController, source: source, onDidDismiss: { [weak viewController] in
guard JetpackFeaturesRemovalCoordinator.siteCreationPhase() != .two else {
return
}
// Display site creation flow if not in phase two
let wizardLauncher = SiteCreationWizardLauncher()
guard let wizard = wizardLauncher.ui else {
return
}
RootViewCoordinator.shared.isSiteCreationActive = true
viewController?.present(wizard, animated: true)
SiteCreationAnalyticsHelper.trackSiteCreationAccessed(source: source)
})
}
func showSelfHostedSiteLoginScreen() {
guard FeatureFlag.authenticateUsingApplicationPassword.enabled else {
WordPressAuthenticator.showLoginForSelfHostedSite(viewController)
return
}
showApplicationPasswordAuthenticationForSelfHostedSite()
}
private func showApplicationPasswordAuthenticationForSelfHostedSite() {
guard let window = viewController.view.window else {
return wpAssertionFailure("window missing")
}
let client = SelfHostedSiteAuthenticator(session: URLSession(configuration: .ephemeral))
let view = LoginWithUrlView(client: client, anchor: window) { [weak viewController] credentials in
viewController?.dismiss(animated: true)
WordPressAuthenticator.shared.delegate!.sync(credentials: .init(wporg: credentials)) {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: WordPressAuthenticator.WPSigninDidFinishNotification), object: nil)
}
}.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(SharedStrings.Button.cancel) { [weak viewController] in
viewController?.dismiss(animated: true)
}
}
}
let hostVC = UIHostingController(rootView: view)
let navigationVC = UINavigationController(rootViewController: hostVC)
navigationVC.modalPresentationStyle = .formSheet
viewController.present(navigationVC, animated: true)
}
}
struct AddSiteMenuViewModel {
let actions: [Action]
enum Selection: String {
case dotCom
case selfHosted
}
struct Action: Identifiable {
let id = UUID()
let title: String
let handler: () -> Void
var uiAction: UIAction {
UIAction(title: title, handler: { _ in handler() })
}
}
init(context: CoreDataStackSwift = ContextManager.shared, onSelection: @escaping (Selection) -> Void) {
let defaultAccount = try? WPAccount.lookupDefaultWordPressComAccount(in: context.mainContext)
let canAddSelfHostedSite = AppConfiguration.showAddSelfHostedSiteButton
var actions: [Action] = []
if defaultAccount != nil {
actions.append(Action(title: Strings.createDotComSite) {
onSelection(.dotCom)
})
}
if canAddSelfHostedSite {
actions.append(Action(title: Strings.addSelfHostedSite) {
onSelection(.selfHosted)
})
}
self.actions = actions
}
}
extension AddSiteMenuViewModel {
func makeBarButtonItem() -> UIBarButtonItem? {
let actions = self.actions
guard !actions.isEmpty else {
return nil
}
let item = UIBarButtonItem(systemItem: .add)
item.setAdaptiveActions(actions.map(\.uiAction))
item.accessibilityIdentifier = "add-site-button"
return item
}
}
private enum Strings {
static let createDotComSite = NSLocalizedString("button.createDotCoSite", value: "Create WordPress.com site", comment: "Create WordPress.com site button")
static let addSelfHostedSite = NSLocalizedString("button.addSelfHostedSite", value: "Add self-hosted site", comment: "Add self-hosted site button")
}