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 tags sync #21918

Merged
merged 3 commits into from
Oct 31, 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 @@ -7,7 +7,6 @@ import WordPressFlux

class AbstractPostListViewController: UIViewController,
WPContentSyncHelperDelegate,
UISearchControllerDelegate,
WPTableViewHandlerDelegate,
NetworkAwareUI // This protocol is not in an extension so that subclasses can override noConnectionMessage()
{
Expand Down Expand Up @@ -201,14 +200,10 @@ class AbstractPostListViewController: UIViewController,
}

private func configureSearchController() {
searchController.delegate = self
searchController.searchResultsUpdater = searchResultsViewController
searchController.showsSearchResultsController = true
searchResultsViewController.searchController = searchController
searchResultsViewController.configure(searchController)
searchResultsViewController.listViewController = self

definesPresentationContext = true

navigationItem.searchController = searchController
}

Expand Down Expand Up @@ -851,12 +846,6 @@ class AbstractPostListViewController: UIViewController,
WPAnalytics.track(.postListStatusFilterChanged, withProperties: propertiesForAnalytics())
}

// MARK: - UISearchControllerDelegate

func willPresentSearchController(_ searchController: UISearchController) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to offload some a bit of code from this vc

WPAnalytics.track(.postListSearchOpened, withProperties: propertiesForAnalytics())
}

// MARK: - NetworkAwareUI

func contentIsEmpty() -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final class PostSearchTokenTableCell: UITableViewCell {
private lazy var stackView = UIStackView(arrangedSubviews: [
iconView, titleLabel, UIView()
])
let separator = UIView()
private let separator = UIView()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
Expand All @@ -18,7 +18,7 @@ final class PostSearchTokenTableCell: UITableViewCell {
stackView.spacing = 8
stackView.alignment = .center
stackView.isLayoutMarginsRelativeArrangement = true
stackView.layoutMargins = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
stackView.layoutMargins = UIEdgeInsets(top: 4, left: 16, bottom: 14, right: 16)
stackView.translatesAutoresizingMaskIntoConstraints = false

contentView.addSubview(stackView)
Expand All @@ -42,7 +42,10 @@ final class PostSearchTokenTableCell: UITableViewCell {
func configure(with token: any PostSearchToken, isLast: Bool) {
iconView.image = token.icon
titleLabel.text = token.value
stackView.layoutMargins.bottom = isLast ? 14 : 8
configure(isLast: isLast)
}

func configure(isLast: Bool) {
separator.isHidden = !isLast
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Combine

final class PostSearchViewController: UIViewController, UITableViewDelegate, UISearchResultsUpdating {
final class PostSearchViewController: UIViewController, UITableViewDelegate, UISearchControllerDelegate, UISearchResultsUpdating {
weak var searchController: UISearchController?
weak var listViewController: AbstractPostListViewController?

Expand All @@ -28,6 +28,14 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS
fatalError("init(coder:) has not been implemented")
}

func configure(_ searchController: UISearchController) {
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.showsSearchResultsController = true

self.searchController = searchController
}

override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -120,7 +128,7 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS
for indexPath in tableView.indexPathsForVisibleRows ?? [] {
if let cell = tableView.cellForRow(at: indexPath) as? PostSearchTokenTableCell {
let isLast = indexPath.row == viewModel.suggestedTokens.count - 1
cell.separator.isHidden = !isLast
cell.configure(isLast: isLast)
}
}
}
Expand Down Expand Up @@ -175,6 +183,12 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS
}
}

// MARK: - UISearchControllerDelegate

func willPresentSearchController(_ searchController: UISearchController) {
viewModel.willStartSearching()
}

// MARK: - UISearchResultsUpdating

func updateSearchResults(for searchController: UISearchController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ final class PostSearchViewModel: NSObject, PostSearchServiceDelegate {
reload()
}

func willStartSearching() {
WPAnalytics.track(.postListSearchOpened, withProperties: propertiesForAnalytics())

syncTags()
}

// MARK: - Search (Remote)

private func performRemoteSearch() {
Expand Down Expand Up @@ -223,4 +229,20 @@ final class PostSearchViewModel: NSObject, PostSearchServiceDelegate {
self.reload()
}
}

// MARK: - Misc

private func syncTags() {
let tagsService = PostTagService(managedObjectContext: coreData.mainContext)
tagsService.syncTags(for: blog, success: { _ in }, failure: { _ in })
}

private func propertiesForAnalytics() -> [String: AnyObject] {
var properties = [String: AnyObject]()
properties["type"] = settings.postType.rawValue as AnyObject?
if let dotComID = blog.dotComID {
properties[WPAppAnalyticsKeyBlogID] = dotComID
}
return properties
}
}