Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posts & Pages: Add Settings context menu action #21965

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions WordPress/Classes/Extensions/UINavigationController+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ extension UINavigationController {
self.pushViewController(viewController, animated: animated)
}
}

extension UIViewController {
func configureDefaultNavigationBarAppearance() {
let standardAppearance = UINavigationBarAppearance()
standardAppearance.configureWithDefaultBackground()

let scrollEdgeAppearance = UINavigationBarAppearance()
scrollEdgeAppearance.configureWithTransparentBackground()

navigationItem.standardAppearance = standardAppearance
navigationItem.compactAppearance = standardAppearance
navigationItem.scrollEdgeAppearance = scrollEdgeAppearance
navigationItem.compactScrollEdgeAppearance = scrollEdgeAppearance
}
}
1 change: 0 additions & 1 deletion WordPress/Classes/Services/PostCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ class PostCoordinator: NSObject {

setPendingDeletion(true, post: post)

let originalStatus = post.status
let trashed = (post.status == .trash)

let repository = PostRepository(coreDataStack: ContextManager.shared)
Expand Down
5 changes: 5 additions & 0 deletions WordPress/Classes/Utility/Analytics/WPAnalyticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ import Foundation
case postListSetAsPostsPageAction
case postListSetHomePageAction
case postListSetAsRegularPageAction
case postListSettingsAction

// Page List
case pageListEditHomepageTapped
Expand Down Expand Up @@ -970,6 +971,8 @@ import Foundation
return "post_list_button_pressed"
case .postListSetAsRegularPageAction:
return "post_list_button_pressed"
case .postListSettingsAction:
return "post_list_button_pressed"

// Page List
case .pageListEditHomepageTapped:
Expand Down Expand Up @@ -1429,6 +1432,8 @@ import Foundation
return ["button": "set_homepage"]
case .postListSetAsRegularPageAction:
return ["button": "set_regular_page"]
case .postListSettingsAction:
return ["button": "settings"]
default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class FullScreenCommentReplyViewController: EditCommentViewController, Su
public override func viewDidLoad() {
super.viewDidLoad()
placeholderLabel.text = placeholder
setupNavigationItems()
setupStandaloneEditor()
configureNavigationAppearance()
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public class FullScreenCommentReplyViewController: EditCommentViewController, Su
}

/// Creates the `leftBarButtonItem` and the `rightBarButtonItem`
private func setupNavigationItems() {
private func setupStandaloneEditor() {
navigationItem.leftBarButtonItem = ({
let image = UIImage.gridicon(.chevronDown).imageWithTintColor(.primary)
let leftItem = UIBarButtonItem(image: image,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ extension PageListViewController: InteractivePostViewDelegate {
// Not available for pages
}

func setParent(for apost: AbstractPost) {
func showSettings(for post: AbstractPost) {
WPAnalytics.track(.postListSettingsAction, properties: propertiesForAnalytics())
PostSettingsViewController.showStandaloneEditor(for: post, from: self)
}

func setParent(for apost: AbstractPost, at indexPath: IndexPath) {
guard let page = apost as? Page else { return }
setParentPage(for: page)
}
Expand Down
10 changes: 5 additions & 5 deletions WordPress/Classes/ViewRelated/Pages/PageMenuViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.moveToDraft, .duplicate],
[.blaze],
[.setParent, .setHomepage, .setPostsPage],
[.setParent, .setHomepage, .setPostsPage, .settings],
[.trash]
]
expect(buttons).to(equal(expectedButtons))
Expand All @@ -53,7 +53,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft, .duplicate],
[.setParent, .setHomepage, .setPostsPage],
[.setParent, .setHomepage, .setPostsPage, .settings],
[.trash]
]
expect(buttons).to(equal(expectedButtons))
Expand All @@ -80,7 +80,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft, .duplicate],
[.setParent, .setHomepage, .setPostsPage],
[.setParent, .setHomepage, .setPostsPage, .settings],
[.trash]
]
expect(buttons).to(equal(expectedButtons))
Expand Down Expand Up @@ -108,7 +108,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.duplicate],
[.blaze],
[.setParent, .setPostsPage]
[.setParent, .setPostsPage, .settings]
]
expect(buttons).to(equal(expectedButtons))
}
Expand All @@ -135,7 +135,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.moveToDraft, .duplicate],
[.blaze],
[.setParent, .setHomepage, .setRegularPage],
[.setParent, .setHomepage, .setRegularPage, .settings],
[.trash]
]
expect(buttons).to(equal(expectedButtons))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class AbstractPostListViewController: UIViewController,
configureTableView()
configureSearchController()
configureAuthorFilter()
configureNavigationBarAppearance()
configureDefaultNavigationBarAppearance()

updateAndPerformFetchRequest()

Expand Down Expand Up @@ -203,19 +203,6 @@ class AbstractPostListViewController: UIViewController,
}
}

private func configureNavigationBarAppearance() {
let standardAppearance = UINavigationBarAppearance()
standardAppearance.configureWithDefaultBackground()

let scrollEdgeAppearance = UINavigationBarAppearance()
scrollEdgeAppearance.configureWithTransparentBackground()

navigationItem.standardAppearance = standardAppearance
navigationItem.compactAppearance = standardAppearance
navigationItem.scrollEdgeAppearance = scrollEdgeAppearance
navigationItem.compactScrollEdgeAppearance = scrollEdgeAppearance
}

func propertiesForAnalytics() -> [String: AnyObject] {
var properties = [String: AnyObject]()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
case .share: return UIImage(systemName: "square.and.arrow.up")
case .blaze: return UIImage(systemName: "flame")
case .comments: return UIImage(systemName: "bubble")
case .settings: return UIImage(systemName: "gearshape")
case .setParent: return UIImage(systemName: "text.append")
case .setHomepage: return UIImage(systemName: "house")
case .setPostsPage: return UIImage(systemName: "text.word.spacing")
Expand Down Expand Up @@ -121,6 +122,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
case .share: return Strings.share
case .blaze: return Strings.blaze
case .comments: return Strings.comments
case .settings: return Strings.settings
case .setParent: return Strings.setParent
case .setHomepage: return Strings.setHomepage
case .setPostsPage: return Strings.setPostsPage
Expand Down Expand Up @@ -153,6 +155,8 @@ extension AbstractPostButton: AbstractPostMenuAction {
delegate.blaze(post)
case .comments:
delegate.comments(post)
case .settings:
delegate.showSettings(for: post)
case .setParent:
delegate.setParent(for: post)
case .setHomepage:
Expand All @@ -170,6 +174,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
static let cancelAutoUpload = NSLocalizedString("posts.cancelUpload.actionTitle", value: "Cancel upload", comment: "Label for the Post List option that cancels automatic uploading of a post.")
static let stats = NSLocalizedString("posts.stats.actionTitle", value: "Stats", comment: "Label for post stats option. Tapping displays statistics for a post.")
static let comments = NSLocalizedString("posts.comments.actionTitle", value: "Comments", comment: "Label for post comments option. Tapping displays comments for a post.")
static let settings = NSLocalizedString("posts.settings.actionTitle", value: "Settings", comment: "Label for post settings option. Tapping displays settings for a post.")
static let duplicate = NSLocalizedString("posts.duplicate.actionTitle", value: "Duplicate", comment: "Label for post duplicate option. Tapping creates a copy of the post.")
static let publish = NSLocalizedString("posts.publish.actionTitle", value: "Publish now", comment: "Label for an option that moves a publishes a post immediately")
static let draft = NSLocalizedString("posts.draft.actionTitle", value: "Move to draft", comment: "Label for an option that moves a post to the draft folder")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum AbstractPostButton: Equatable {
case share
case blaze
case comments
case settings

/// Specific to pages
case pageAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protocol InteractivePostViewDelegate: AnyObject {
func share(_ post: AbstractPost, fromView view: UIView)
func blaze(_ post: AbstractPost)
func comments(_ post: AbstractPost)
func showSettings(for post: AbstractPost)
func setParent(for post: AbstractPost)
func setHomepage(for post: AbstractPost)
func setPostsPage(for post: AbstractPost)
Expand Down
3 changes: 3 additions & 0 deletions WordPress/Classes/ViewRelated/Post/PageMenuViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ final class PageMenuViewModel: AbstractPostMenuViewModel {
} else {
buttons.append(.setRegularPage)
}
if page.status != .trash {
buttons.append(.settings)
}

return AbstractPostButtonSection(buttons: buttons, submenuButton: .pageAttributes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class PostCardStatusViewModel: NSObject, AbstractPostMenuViewModel {
if isJetpackFeaturesEnabled, post.status == .publish && post.hasRemote() {
buttons.append(contentsOf: [.stats, .comments])
}
if post.status != .trash {
buttons.append(.settings)
}

return AbstractPostButtonSection(buttons: buttons)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Gridicons
import UIKit

class PostListViewController: AbstractPostListViewController, UIViewControllerRestoration, InteractivePostViewDelegate {

private let statsStoryboardName = "SiteStats"
private let currentPostListStatusFilterKey = "CurrentPostListStatusFilterKey"

Expand Down Expand Up @@ -426,6 +425,11 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
try? contentCoordinator.displayCommentsWithPostId(post.postID, siteID: blog.dotComID, commentID: nil, source: .postsList)
}

func showSettings(for post: AbstractPost) {
WPAnalytics.track(.postListSettingsAction, properties: propertiesForAnalytics())
PostSettingsViewController.showStandaloneEditor(for: post, from: self)
}

// MARK: - NetworkAwareUI

override func noConnectionMessage() -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import UIKit
import CoreData
import Combine

extension PostSettingsViewController {
static func showStandaloneEditor(for post: AbstractPost, from presentingViewController: UIViewController) {
let viewController: PostSettingsViewController
if let post = post as? Post {
viewController = PostSettingsViewController(post: post.latest())
} else {
viewController = PageSettingsViewController(post: post)
}
viewController.isStandalone = true
let navigation = UINavigationController(rootViewController: viewController)
navigation.navigationBar.isTranslucent = true // Reset to default
presentingViewController.present(navigation, animated: true)
}

@objc func setupStandaloneEditor() {
guard isStandalone else { return }

configureDefaultNavigationBarAppearance()

refreshNavigationBarButtons()
navigationItem.rightBarButtonItem?.isEnabled = false

var cancellables: [AnyCancellable] = []
apost.objectWillChange.sink { [weak self] in
self?.navigationItem.rightBarButtonItem?.isEnabled = true
}.store(in: &cancellables)
objc_setAssociatedObject(self, &PostSettingsViewController.cancellablesKey, cancellables, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}

private func refreshNavigationBarButtons() {
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(buttonCancelTapped))

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(buttonDoneTapped))
}

@objc private func buttonCancelTapped() {
presentingViewController?.dismiss(animated: true)
}

@objc private func buttonDoneTapped() {
let activityIndicator = UIActivityIndicatorView(style: .medium)
activityIndicator.startAnimating()

navigationItem.rightBarButtonItem = UIBarButtonItem(customView: activityIndicator)

setEnabled(false)

PostCoordinator.shared.save(apost) { [weak self] result in
switch result {
case .success:
self?.isStandaloneEditorDismissingAfterSave = true
self?.presentingViewController?.dismiss(animated: true)
case .failure:
self?.setEnabled(true)
SVProgressHUD.showError(withStatus: Strings.errorMessage)
self?.refreshNavigationBarButtons()
}
}
}

private func setEnabled(_ isEnabled: Bool) {
tableView.tintAdjustmentMode = isEnabled ? .automatic : .dimmed
tableView.isUserInteractionEnabled = isEnabled
}

private static var cancellablesKey: UInt8 = 0
}

private enum Strings {
static let errorMessage = NSLocalizedString("postSettings.updateFailedMessage", value: "Failed to update the post settings", comment: "Error message on post/page settings screen")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- (void)endEditingAction:(nullable id)sender;

@property (nonnull, nonatomic, strong, readonly) AbstractPost *apost;
@property (nonatomic) BOOL isStandalone;
@property (nonatomic) BOOL isStandaloneEditorDismissingAfterSave;
@property (nonnull, nonatomic, strong, readonly) NSArray *publicizeConnections;
@property (nonnull, nonatomic, strong, readonly) NSArray *unsupportedConnections;

Expand Down
17 changes: 13 additions & 4 deletions WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ - (void)viewDidLoad
// reachability callbacks to trigger before such initial setup completes.
//
[self setupReachability];
[self setupStandaloneEditor];
}

- (void)viewWillAppear:(BOOL)animated
Expand All @@ -177,9 +178,15 @@ - (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

[self.apost.managedObjectContext performBlock:^{
[self.apost.managedObjectContext save:nil];
}];
if (self.isStandalone) {
if (!self.isStandaloneEditorDismissingAfterSave) {
[self.apost.managedObjectContext refreshObject:self.apost mergeChanges:NO];
}
} else {
[self.apost.managedObjectContext performBlock:^{
[self.apost.managedObjectContext save:nil];
}];
}
}

- (void)didReceiveMemoryWarning
Expand Down Expand Up @@ -1373,7 +1380,9 @@ - (void)postCategoriesViewController:(PostCategoriesViewController *)controller

// Save changes.
self.post.categories = [categories mutableCopy];
[self.post save];
if (!self.isStandalone) {
[self.post save];
}
}

#pragma mark - PostFeaturedImageCellDelegate
Expand Down
Loading