Skip to content

Commit

Permalink
Integrate PublishButton in the prepublishing sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Feb 20, 2024
1 parent ce3fea5 commit df7a67e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UIKit
import WordPressAuthenticator
import Combine
import WordPressUI
import SwiftUI

enum PrepublishingIdentifier {
case title
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -151,8 +149,6 @@ final class PrepublishingViewController: UIViewController, UITableViewDataSource
view.pinSubviewToSafeArea(stackView)

view.backgroundColor = .systemBackground

announcePublishButton()
}

private func configureHeader() {
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct PublishButton: View {
.controlSize(.large)
.disabled(isDisabled)
.buttonBorderShape(.roundedRectangle(radius: 8))
.accessibilityIdentifier("publish")

switch viewModel.state {
case .default:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

0 comments on commit df7a67e

Please sign in to comment.