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: Move authors filter button to navigation bar #21766

Merged
merged 2 commits into from
Oct 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
predicates.append(filterPredicate)
}

if filterSettings.shouldShowOnlyMyPosts() {
let myAuthorID = blogUserID() ?? 0

// Brand new local drafts have an authorID of 0.
let authorPredicate = NSPredicate(format: "authorID = %@ || authorID = 0", myAuthorID)
predicates.append(authorPredicate)
}

if let predicate = PostSearchViewModel.makeHomepagePredicate(for: blog) {
predicates.append(predicate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class AbstractPostListViewController: UIViewController,
}
}

private let buttonAuthorFilter = AuthorFilterButton()

let refreshControl = UIRefreshControl()

@objc lazy var tableViewHandler: WPTableViewHandler = {
Expand Down Expand Up @@ -252,18 +254,6 @@ class AbstractPostListViewController: UIViewController,
}
}

private func configureAuthorFilter() {
guard filterSettings.canFilterByAuthor() else {
return
}

let authorFilter = AuthorFilterButton()
authorFilter.addTarget(self, action: #selector(showAuthorSelectionPopover(_:)), for: .touchUpInside)
filterTabBar.accessoryView = authorFilter

updateAuthorFilter()
}

private func configureSearchController() {
searchController.delegate = self
searchController.searchResultsUpdater = searchResultsViewController
Expand Down Expand Up @@ -316,6 +306,46 @@ class AbstractPostListViewController: UIViewController,
return properties
}

// MARK: - Author Filter

private func configureAuthorFilter() {
guard filterSettings.canFilterByAuthor() else {
return
}
buttonAuthorFilter.sizeToFit()
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: buttonAuthorFilter)

buttonAuthorFilter.addTarget(self, action: #selector(showAuthorSelectionPopover(_:)), for: .touchUpInside)
updateAuthorFilter()
}

private func updateAuthorFilter() {
if filterSettings.currentPostAuthorFilter() == .everyone {
buttonAuthorFilter.filterType = .everyone
} else {
buttonAuthorFilter.filterType = .user(gravatarEmail: blog.account?.email)
}
}

@objc private func showAuthorSelectionPopover(_ sender: UIView) {
let filterController = AuthorFilterViewController(initialSelection: filterSettings.currentPostAuthorFilter(), gravatarEmail: blog.account?.email) { [weak self] filter in
if filter != self?.filterSettings.currentPostAuthorFilter() {
UIAccessibility.post(notification: UIAccessibility.Notification.screenChanged, argument: sender)
}

self?.filterSettings.setCurrentPostAuthorFilter(filter)
self?.updateAuthorFilter()
self?.refreshAndReload()
self?.syncItemsWithUserInteraction(false)
self?.dismiss(animated: true)
}

ForcePopoverPresenter.configurePresentationControllerForViewController(filterController, presentingFromView: sender)
filterController.popoverPresentationController?.permittedArrowDirections = .up

present(filterController, animated: true)
}

// MARK: - GUI: No results view logic

func hideNoResultsView() {
Expand Down Expand Up @@ -501,39 +531,6 @@ class AbstractPostListViewController: UIViewController,
WPAnalytics.track(.postListPullToRefresh, withProperties: propertiesForAnalytics())
}

@objc
private func showAuthorSelectionPopover(_ sender: UIView) {
let filterController = AuthorFilterViewController(initialSelection: filterSettings.currentPostAuthorFilter(),
gravatarEmail: blog.account?.email) { [weak self] filter in
if filter != self?.filterSettings.currentPostAuthorFilter() {
UIAccessibility.post(notification: UIAccessibility.Notification.screenChanged, argument: sender)
}

self?.filterSettings.setCurrentPostAuthorFilter(filter)
self?.updateAuthorFilter()
self?.refreshAndReload()
self?.syncItemsWithUserInteraction(false)
self?.dismiss(animated: true)
}

ForcePopoverPresenter.configurePresentationControllerForViewController(filterController, presentingFromView: sender)
filterController.popoverPresentationController?.permittedArrowDirections = .up

present(filterController, animated: true)
}

private func updateAuthorFilter() {
guard let accessoryView = filterTabBar.accessoryView as? AuthorFilterButton else {
return
}

if filterSettings.currentPostAuthorFilter() == .everyone {
accessoryView.filterType = .everyone
} else {
accessoryView.filterType = .user(gravatarEmail: blog.account?.email)
}
}

// MARK: - Synching

@objc func automaticallySyncIfAppropriate() {
Expand Down
65 changes: 8 additions & 57 deletions WordPress/Classes/ViewRelated/Post/AuthorFilterButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,15 @@ private extension AuthorFilterType {
}
}

/// Displays an author gravatar image with a dropdown arrow.
///
class AuthorFilterButton: UIControl {
final class AuthorFilterButton: UIControl {
private let authorImageView: CircularImageView = {
let imageView = CircularImageView(image: UIImage.gravatarPlaceholderImage)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.setContentHuggingPriority(.required, for: .horizontal)
imageView.backgroundColor = .neutral(.shade10)
imageView.tintColor = .neutral(.shade70)

return imageView
}()

private let chevronImageView: UIImageView = {
let imageView = UIImageView(image: UIImage.makeChevronDownImage(with: .neutral(.shade40), size: Metrics.chevronSize))
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.setContentHuggingPriority(.required, for: .horizontal)
imageView.contentMode = .bottom

return imageView
}()

private let stackView: UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.alignment = .center
stackView.spacing = Metrics.stackViewSpacing
stackView.isUserInteractionEnabled = false

return stackView
}()

override var intrinsicContentSize: CGSize {
return Metrics.contentSize
}
Expand All @@ -58,7 +34,7 @@ class AuthorFilterButton: UIControl {
didSet {
switch filterType {
case .everyone:
authorImageView.image = .gridicon(.multipleUsers, size: Metrics.multipleUsersGravatarSize)
authorImageView.image = UIImage(named: "icon-people")
authorImageView.contentMode = .center
case .user(let email):
authorImageView.contentMode = .scaleAspectFill
Expand Down Expand Up @@ -86,17 +62,14 @@ class AuthorFilterButton: UIControl {
}

private func commonInit() {
stackView.addArrangedSubview(authorImageView)
stackView.addArrangedSubview(chevronImageView)
addSubview(stackView)
addSubview(authorImageView)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Metrics.leadingPadding),
stackView.centerYAnchor.constraint(equalTo: centerYAnchor),
authorImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Metrics.leadingPadding),
authorImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
authorImageView.widthAnchor.constraint(equalToConstant: Metrics.gravatarSize.width),
authorImageView.heightAnchor.constraint(equalToConstant: Metrics.gravatarSize.height),
widthAnchor.constraint(equalToConstant: intrinsicContentSize.width),
chevronImageView.heightAnchor.constraint(equalToConstant: Metrics.chevronSize.height + Metrics.chevronVerticalPadding)
])
widthAnchor.constraint(equalToConstant: intrinsicContentSize.width)
])

authorImageView.image = gravatarPlaceholder

Expand All @@ -106,12 +79,8 @@ class AuthorFilterButton: UIControl {
private let gravatarPlaceholder: UIImage = .gridicon(.user, size: Metrics.gravatarSize)

private enum Metrics {
static let chevronSize = CGSize(width: 10.0, height: 5.0)
static let contentSize = CGSize(width: 72.0, height: 44.0)
static let contentSize = CGSize(width: 44.0, height: 44.0)
static let gravatarSize = CGSize(width: 28.0, height: 28.0)
static let multipleUsersGravatarSize = CGSize(width: 20.0, height: 20.0)
static let stackViewSpacing: CGFloat = 7.0
static let chevronVerticalPadding: CGFloat = 2.0
static let leadingPadding: CGFloat = 12.0
}
}
Expand All @@ -125,21 +94,3 @@ extension AuthorFilterButton: Accessible {
accessibilityValue = filterType.accessibilityValue
}
}

private extension UIImage {
// Draws a small down facing arrow
static func makeChevronDownImage(with color: UIColor, size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
color.setFill()
color.setStroke()
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: size.width, y: 0))
path.addLine(to: CGPoint(x: size.width / 2, y: size.height))
path.close()
path.fill()
}
return image
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ private class AuthorFilterCell: UITableViewCell {
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.setContentHuggingPriority(.required, for: .horizontal)
imageView.setContentCompressionResistancePriority(.required, for: .horizontal)
imageView.backgroundColor = Appearance.placeholderBackgroundColor
imageView.tintColor = Appearance.placeholderTintColor
return imageView
}()
Expand Down Expand Up @@ -231,7 +230,7 @@ private class AuthorFilterCell: UITableViewCell {
didSet {
switch filterType {
case .everyone:
gravatarImageView.image = .gridicon(.multipleUsers, size: Metrics.multipleGravatarSize)
gravatarImageView.image = UIImage(named: "icon-people")
gravatarImageView.contentMode = .center
accessibilityHint = NSLocalizedString("Select to show everyone's posts.", comment: "Voiceover accessibility hint, informing the user they can select an item to show posts written by all users on the site")
case .user(let email):
Expand All @@ -258,7 +257,6 @@ private class AuthorFilterCell: UITableViewCell {
private enum Appearance {
static let textColor = UIColor.neutral(.shade70)
static let placeholderTintColor = UIColor.neutral(.shade70)
static let placeholderBackgroundColor = UIColor.neutral(.shade10)
}

private enum Metrics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ class PostListFilterSettings: NSObject {
return .everyone
}

if let filter = UserPersistentStoreFactory.instance().object(forKey: type(of: self).currentPostAuthorFilterKey) {
if (filter as AnyObject).uintValue == AuthorFilter.everyone.rawValue {
return .everyone
}
if let rawValue = UserPersistentStoreFactory.instance().object(forKey: type(of: self).currentPostAuthorFilterKey),
let filter = AuthorFilter(rawValue: (rawValue as AnyObject).uintValue) {
return filter
}

return .mine
return .everyone
}

/// currentPostListFilter: stores the last active AuthorFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
}

func configureNavigationButtons() {
navigationItem.rightBarButtonItems = [postsViewButtonItem]
// TODO: Remove
// navigationItem.rightBarButtonItems = [postsViewButtonItem]
}

@objc func togglePostsView() {
Expand Down
15 changes: 0 additions & 15 deletions WordPress/Classes/ViewRelated/System/FilterTabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,6 @@ class FilterTabBar: UIControl {
}
}

/// Accessory view displayed on the leading end of the tab bar.
///
var accessoryView: UIView? = nil {
didSet {
if let oldValue = oldValue {
oldValue.removeFromSuperview()
}

if let accessoryView = accessoryView {
accessoryView.setContentCompressionResistancePriority(.required, for: .horizontal)
stackView.insertArrangedSubview(accessoryView, at: 0)
}
}
}

// MARK: - Tab Sizing

private var stackViewEdgeConstraints: [NSLayoutConstraint]! {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "icon-people.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.