diff --git a/WordPress/Classes/ViewRelated/Post/Prepublishing/PrepublishingViewController.swift b/WordPress/Classes/ViewRelated/Post/Prepublishing/PrepublishingViewController.swift index e7d19341b279..417e2d688742 100644 --- a/WordPress/Classes/ViewRelated/Post/Prepublishing/PrepublishingViewController.swift +++ b/WordPress/Classes/ViewRelated/Post/Prepublishing/PrepublishingViewController.swift @@ -2,6 +2,7 @@ import UIKit import WordPressAuthenticator import Combine import WordPressUI +import SwiftUI enum PrepublishingIdentifier { case title @@ -57,15 +58,12 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource let tableView = UITableView(frame: .zero, style: .plain) private let footerSeparator = UIView() - let publishButton: NUXButton = { - let nuxButton = NUXButton() - nuxButton.isPrimary = true - nuxButton.accessibilityIdentifier = "publish" - return nuxButton - }() - private weak var titleField: UITextField? + private lazy var publishButtonViewModel = PublishButtonViewModel(title: "Publish") { [weak self] in + self?.buttonPublishTapped() + } + /// Determines whether the text has been first responder already. If it has, don't force it back on the user unless it's been selected by them. private var hasSelectedText: Bool = false @@ -151,8 +149,6 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource view.pinSubviewToSafeArea(stackView) view.backgroundColor = .systemBackground - - announcePublishButton() } private func configureHeader() { @@ -170,11 +166,13 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource private func setupPublishButton() -> UIView { let footerView = UIView() - footerView.addSubview(publishButton) - publishButton.translatesAutoresizingMaskIntoConstraints = false - footerView.pinSubviewToSafeArea(publishButton, insets: Constants.nuxButtonInsets) - publishButton.addTarget(self, action: #selector(publish), for: .touchUpInside) + let hostingViewController = UIHostingController(rootView: PublishButton(viewModel: publishButtonViewModel).tint(Color(uiColor: .primary))) + addChild(hostingViewController) + + footerView.addSubview(hostingViewController.view) + hostingViewController.view.translatesAutoresizingMaskIntoConstraints = false + footerView.pinSubviewToSafeArea(hostingViewController.view, insets: Constants.nuxButtonInsets) updatePublishButtonLabel() @@ -423,10 +421,10 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource // MARK: - Publish Button private func updatePublishButtonLabel() { - publishButton.setTitle(post.isScheduled() ? Strings.schedule : Strings.publish, for: .normal) + publishButtonViewModel.title = post.isScheduled() ? Strings.schedule : Strings.publish } - @objc func publish(_ sender: UIButton) { + private func buttonPublishTapped() { didTapPublish = true navigationController?.dismiss(animated: true) { WPAnalytics.track(.editorPostPublishNowTapped) @@ -460,12 +458,6 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource // MARK: - Accessibility - private func announcePublishButton() { - DispatchQueue.main.asyncAfter(deadline: .now()) { - UIAccessibility.post(notification: .screenChanged, argument: self.publishButton) - } - } - fileprivate enum Constants { static let reuseIdentifier = "wpTableViewCell" static let textFieldReuseIdentifier = "wpTextFieldCell" diff --git a/WordPress/Classes/ViewRelated/Post/Prepublishing/PublishButton.swift b/WordPress/Classes/ViewRelated/Post/Prepublishing/PublishButton.swift index 89094bd1225d..39ffb7707481 100644 --- a/WordPress/Classes/ViewRelated/Post/Prepublishing/PublishButton.swift +++ b/WordPress/Classes/ViewRelated/Post/Prepublishing/PublishButton.swift @@ -15,6 +15,7 @@ struct PublishButton: View { .controlSize(.large) .disabled(isDisabled) .buttonBorderShape(.roundedRectangle(radius: 8)) + .accessibilityIdentifier("publish") switch viewModel.state { case .default: @@ -80,11 +81,11 @@ struct PublishButton: View { } final class PublishButtonViewModel: ObservableObject { - let title: String - let onSubmitTapped: () -> Void + @Published var title: String @Published var state: PublishButtonState = .default + let onSubmitTapped: () -> Void - init(title: String, onSubmitTapped: @escaping () -> Void, state: PublishButtonState = .default) { + init(title: String, state: PublishButtonState = .default, onSubmitTapped: @escaping () -> Void) { self.title = title self.onSubmitTapped = onSubmitTapped self.state = state @@ -114,11 +115,11 @@ private enum Strings { #Preview { VStack(spacing: 16) { - PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .default)) - PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .loading)) - PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .uploading(title: "Uploading media...", progress: .init(completed: 100, total: 2000)))) - PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .failed(title: "Failed to upload media"))) - PublishButton(viewModel: .init(title: "Publish", onSubmitTapped: {}, state: .failed(title: "Failed to upload media", details: "Not connected to Internet", onRetryTapped: {}))) + PublishButton(viewModel: .init(title: "Publish", state: .default) {}) + PublishButton(viewModel: .init(title: "Publish", state: .loading) {}) + PublishButton(viewModel: .init(title: "Publish", state: .uploading(title: "Uploading media...", progress: .init(completed: 100, total: 2000))) {}) + PublishButton(viewModel: .init(title: "Publish", state: .failed(title: "Failed to upload media")) {}) + PublishButton(viewModel: .init(title: "Publish", state: .failed(title: "Failed to upload media", details: "Not connected to Internet", onRetryTapped: {})) {}) } .padding() }