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: Remove cell switcher from Posts List screen #21806

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
147 changes: 18 additions & 129 deletions WordPress/Classes/ViewRelated/Post/PostListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ import UIKit

class PostListViewController: AbstractPostListViewController, UIViewControllerRestoration, InteractivePostViewDelegate {

private let postCompactCellIdentifier = "PostCompactCellIdentifier"
private let postCardTextCellIdentifier = "PostCardTextCellIdentifier"
private let postCardRestoreCellIdentifier = "PostCardRestoreCellIdentifier"
private let postCompactCellNibName = "PostCompactCell"
private let postCardTextCellNibName = "PostCardCell"
private let postCardRestoreCellNibName = "RestorePostTableViewCell"
private let statsStoryboardName = "SiteStats"
private let currentPostListStatusFilterKey = "CurrentPostListStatusFilterKey"
private var postCellIdentifier: String {
return isCompact ? postCompactCellIdentifier : postCardTextCellIdentifier
}

static private let postsViewControllerRestorationKey = "PostsViewControllerRestorationKey"

Expand All @@ -25,8 +18,6 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
private let postCardEstimatedRowHeight = CGFloat(300.0)
private let postListHeightForFooterView = CGFloat(50.0)

private var database: UserPersistentRepository = UserPersistentStoreFactory.instance()

private lazy var _tableViewHandler: PostListTableViewHandler = {
let tableViewHandler = PostListTableViewHandler(tableView: tableView)
tableViewHandler.cacheRowHeights = false
Expand All @@ -43,29 +34,14 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
}
}

private var postViewIcon: UIImage? {
return isCompact ? UIImage(named: "icon-post-view-card") : .gridicon(.listUnordered)
}

private lazy var postActionSheet: PostActionSheet = {
return PostActionSheet(viewController: self, interactivePostViewDelegate: self)
}()

private lazy var postsViewButtonItem: UIBarButtonItem = {
return UIBarButtonItem(image: postViewIcon, style: .done, target: self, action: #selector(togglePostsView))
}()

private var showingJustMyPosts: Bool {
return filterSettings.currentPostAuthorFilter() == .mine
}

private var isCompact: Bool = false {
didSet {
database.set(isCompact, forKey: Constants.exhibitionModeKey)
showCompactOrDefault()
}
}

/// If set, when the post list appear it will show the tab for this status
var initialFilterWithPostStatus: BasePost.Status?

Expand Down Expand Up @@ -123,10 +99,6 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe

title = NSLocalizedString("Posts", comment: "Title of the screen showing the list of posts for a blog.")

updateGhostableTableViewOptions()

configureNavigationButtons()

configureInitialFilterIfNeeded()
listenForAppComingToForeground()

Expand Down Expand Up @@ -155,12 +127,6 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

configureCompactOrDefault()
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
toggleCreateButton()
Expand All @@ -176,47 +142,12 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
}
}

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

@objc func togglePostsView() {
isCompact.toggle()

WPAppAnalytics.track(.postListToggleButtonPressed, withProperties: ["mode": isCompact ? Constants.compact: Constants.card])
}

// MARK: - Configuration

override func heightForFooterView() -> CGFloat {
return postListHeightForFooterView
}

override func selectedFilterDidChange(_ filterBar: FilterTabBar) {
updateGhostableTableViewOptions()
super.selectedFilterDidChange(filterBar)
}

override func refresh(_ sender: AnyObject) {
updateGhostableTableViewOptions()
super.refresh(sender)
}

/// Update the `GhostOptions` to correctly show compact or default cells
private func updateGhostableTableViewOptions() {
let ghostOptions = GhostOptions(displaysSectionHeader: false, reuseIdentifier: postCellIdentifier, rowsPerSection: [50])
let style = GhostStyle(beatDuration: GhostStyle.Defaults.beatDuration,
beatStartColor: .placeholderElement,
beatEndColor: .placeholderElementFaded)
ghostableTableView.removeGhostContent()
ghostableTableView.displayGhostContent(options: ghostOptions, style: style)
}

private func configureCompactOrDefault() {
isCompact = database.object(forKey: Constants.exhibitionModeKey) as? Bool ?? false
}

override func configureTableView() {
tableView.accessibilityIdentifier = "PostsTable"
tableView.separatorStyle = .none
Expand All @@ -227,11 +158,7 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
let bundle = Bundle.main

// Register the cells
let postCardTextCellNib = UINib(nibName: postCardTextCellNibName, bundle: bundle)
tableView.register(postCardTextCellNib, forCellReuseIdentifier: postCardTextCellIdentifier)

let postCompactCellNib = UINib(nibName: postCompactCellNibName, bundle: bundle)
tableView.register(postCompactCellNib, forCellReuseIdentifier: postCompactCellIdentifier)
tableView.register(PostListCell.self, forCellReuseIdentifier: PostListCell.defaultReuseID)

let postCardRestoreCellNib = UINib(nibName: postCardRestoreCellNibName, bundle: bundle)
tableView.register(postCardRestoreCellNib, forCellReuseIdentifier: postCardRestoreCellIdentifier)
Expand All @@ -240,32 +167,6 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
tableView.register(headerNib, forHeaderFooterViewReuseIdentifier: ActivityListSectionHeaderView.identifier)
}

override func configureGhostableTableView() {
super.configureGhostableTableView()

ghostingEnabled = true

// Register the cells
let postCardTextCellNib = UINib(nibName: postCardTextCellNibName, bundle: Bundle.main)
ghostableTableView.register(postCardTextCellNib, forCellReuseIdentifier: postCardTextCellIdentifier)

let postCompactCellNib = UINib(nibName: postCompactCellNibName, bundle: Bundle.main)
ghostableTableView.register(postCompactCellNib, forCellReuseIdentifier: postCompactCellIdentifier)
}

func showCompactOrDefault() {
updateGhostableTableViewOptions()

postsViewButtonItem.accessibilityLabel = NSLocalizedString("List style", comment: "The accessibility label for the list style button in the Post List.")
postsViewButtonItem.accessibilityValue = isCompact ? NSLocalizedString("Compact", comment: "Accessibility indication that the current Post List style is currently Compact.") : NSLocalizedString("Expanded", comment: "Accessibility indication that the current Post List style is currently Expanded.")
postsViewButtonItem.image = postViewIcon

if isViewOnScreen() {
tableView.reloadSections([0], with: .automatic)
ghostableTableView.reloadSections([0], with: .automatic)
}
}

private func configureInitialFilterIfNeeded() {
guard let initialFilterWithPostStatus = initialFilterWithPostStatus else {
return
Expand Down Expand Up @@ -400,18 +301,22 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe

let post = postAtIndexPath(indexPath)

guard let interactivePostView = cell as? InteractivePostView,
let configurablePostView = cell as? ConfigurablePostView else {
fatalError("Cell does not implement the required protocols")
// TODO: Remove later
// guard let interactivePostView = cell as? InteractivePostView,
// let configurablePostView = cell as? ConfigurablePostView else {
// fatalError("Cell does not implement the required protocols")
// }
//
// interactivePostView.setInteractionDelegate(self)
// interactivePostView.setActionSheetDelegate(self)
//
// configurablePostView.configure(with: post)

// TODO: Hide author if only showing my posts?
guard let cell = cell as? PostListCell else {
return
}

interactivePostView.setInteractionDelegate(self)
interactivePostView.setActionSheetDelegate(self)

configurablePostView.configure(with: post)

configurePostCell(cell)
configureRestoreCell(cell)
cell.configure(with: PostListItemViewModel(post: post))
}

fileprivate func cellIdentifierForPost(_ post: Post) -> String {
Expand All @@ -420,28 +325,12 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
if recentlyTrashedPostObjectIDs.contains(post.objectID) == true && filterSettings.currentPostListFilter().filterType != .trashed {
identifier = postCardRestoreCellIdentifier
} else {
identifier = postCellIdentifier
identifier = PostListCell.defaultReuseID
}

return identifier
}

private func configurePostCell(_ cell: UITableViewCell) {
guard let cell = cell as? PostCardCell else {
return
}

cell.shouldHideAuthor = showingJustMyPosts
}

private func configureRestoreCell(_ cell: UITableViewCell) {
guard let cell = cell as? RestorePostTableViewCell else {
return
}

cell.isCompact = isCompact
}

// MARK: - Post Actions

override func createPost() {
Expand Down Expand Up @@ -704,6 +593,6 @@ private extension PostListViewController {

extension PostListViewController: PostActionSheetDelegate {
func showActionSheet(_ postCardStatusViewModel: PostCardStatusViewModel, from view: UIView) {
postActionSheet.show(for: postCardStatusViewModel, from: view, isCompactOrSearching: isCompact)
postActionSheet.show(for: postCardStatusViewModel, from: view)
}
}
13 changes: 0 additions & 13 deletions WordPress/Classes/ViewRelated/Post/RestorePostTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ class RestorePostTableViewCell: UITableViewCell, ConfigurablePostView, Interacti

private weak var delegate: InteractivePostViewDelegate?

var isCompact: Bool = false {
didSet {
isCompact ? configureCompact() : configureDefault()
}
}
var post: Post?

func configure(with post: Post) {
Expand Down Expand Up @@ -45,14 +40,7 @@ class RestorePostTableViewCell: UITableViewCell, ConfigurablePostView, Interacti
restoreButton.setTitle(buttonTitle, for: .normal)
restoreButton.setImage(.gridicon(.undo, size: CGSize(width: Constants.imageSize,
height: Constants.imageSize)), for: .normal)
}

private func configureCompact() {
topMargin.constant = Constants.compactMargin
postContentView.layer.borderWidth = 0
}

private func configureDefault() {
topMargin.constant = Constants.defaultMargin
postContentView.layer.borderColor = WPStyleGuide.postCardBorderColor.cgColor
postContentView.layer.borderWidth = .hairlineBorderWidth
Expand Down Expand Up @@ -82,7 +70,6 @@ class RestorePostTableViewCell: UITableViewCell, ConfigurablePostView, Interacti

private enum Constants {
static let defaultMargin: CGFloat = 16
static let compactMargin: CGFloat = 0
static let imageSize: CGFloat = 18.0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,6 @@ import Nimble

class PostListViewControllerTests: CoreDataTestCase {

func testShowsGhostableTableView() {
let blog = BlogBuilder(mainContext).build()
let postListViewController = PostListViewController.controllerWithBlog(blog)
let _ = postListViewController.view

postListViewController.startGhost()

expect(postListViewController.ghostableTableView.isHidden).to(beFalse())
}

func testHidesGhostableTableView() {
let blog = BlogBuilder(mainContext).build()
let postListViewController = PostListViewController.controllerWithBlog(blog)
let _ = postListViewController.view

postListViewController.stopGhost()

expect(postListViewController.ghostableTableView.isHidden).to(beTrue())
}

func testShowTenMockedItemsInGhostableTableView() {
let blog = BlogBuilder(mainContext).build()
let postListViewController = PostListViewController.controllerWithBlog(blog)
let _ = postListViewController.view

postListViewController.startGhost()

expect(postListViewController.ghostableTableView.numberOfRows(inSection: 0)).to(equal(50))
}

func testItCanHandleNewPostUpdatesEvenIfTheGhostViewIsStillVisible() throws {
// This test simulates and proves that the app will no longer crash on these conditions:
//
Expand Down