Skip to content

Commit

Permalink
Add convenience API for adding dashboard card menus
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed May 30, 2023
1 parent 55865bb commit 81be484
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ final class DashboardActivityLogCardCell: DashboardCollectionViewCell {
let activitySubmenu = UIMenu(title: String(), options: .displayInline, children: [activityAction])


let hideThisAction = BlogDashboardHelpers.makeHideCardAction(for: .activityLog,
siteID: blog.dotComID?.intValue ?? 0)
let hideThisAction = BlogDashboardHelpers.makeHideCardAction(for: .activityLog, blog: blog)

cardFrameView.ellipsisButton.menu = UIMenu(title: String(), options: .displayInline, children: [
activitySubmenu,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ extension DashboardPagesListCardCell {
}
cardFrameView.ellipsisButton.showsMenuAsPrimaryAction = true

let children = [makeAllPagesAction(), makeHideCardAction(blog: blog)].compactMap { $0 }
let children = [
makeAllPagesAction(),
BlogDashboardHelpers.makeHideCardAction(for: .pages, blog: blog)
].compactMap { $0 }

cardFrameView.ellipsisButton.menu = UIMenu(title: String(), options: .displayInline, children: children)
}
Expand All @@ -119,13 +122,6 @@ extension DashboardPagesListCardCell {
return allPagesSubmenu
}

private func makeHideCardAction(blog: Blog) -> UIMenuElement? {
guard let siteID = blog.dotComID?.intValue else {
return nil
}
return BlogDashboardHelpers.makeHideCardAction(for: .pages, siteID: siteID)
}

// MARK: Actions

private func showPagesList(source: PagesListSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ class BlogDashboardCardFrameView: UIView {
buttonContainerStackView.removeFromSuperview()
}

/// Adds the "more" button with the given actions to the corner of the cell.
func addMoreMenu(items: [UIMenuElement], card: DashboardCard) {
onEllipsisButtonTap = {
BlogDashboardAnalytics.trackContextualMenuAccessed(for: card)
}
ellipsisButton.showsMenuAsPrimaryAction = true
ellipsisButton.menu = UIMenu(title: "", options: .displayInline, children: items)
}

private func updateEllipsisButtonState() {
ellipsisButton.isHidden = onEllipsisButtonTap == nil
let headerPadding = ellipsisButton.isHidden ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,9 @@ extension DashboardPostsListCardCell {
private func addContextMenu(card: DashboardCard, blog: Blog) {
guard FeatureFlag.personalizeHomeTab.enabled else { return }

frameView.onEllipsisButtonTap = {
BlogDashboardAnalytics.trackContextualMenuAccessed(for: card)
}
frameView.ellipsisButton.showsMenuAsPrimaryAction = true
frameView.ellipsisButton.menu = UIMenu(title: "", options: .displayInline, children: [
BlogDashboardHelpers.makeHideCardAction(for: card, siteID: blog.dotComID?.intValue ?? 0)
])
frameView.addMoreMenu(items: [
BlogDashboardHelpers.makeHideCardAction(for: card, blog: blog)
], card: card)
}

private func configureDraftsList(blog: Blog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ final class DashboardQuickStartCardCell: UICollectionViewCell, Reusable, BlogDas

private func configureOnEllipsisButtonTap(sourceRect: CGRect, blog: Blog) {
if FeatureFlag.personalizeHomeTab.enabled {
cardFrameView.onEllipsisButtonTap = {
BlogDashboardAnalytics.trackContextualMenuAccessed(for: .quickStart)
}
cardFrameView.ellipsisButton.showsMenuAsPrimaryAction = true
cardFrameView.ellipsisButton.menu = UIMenu(title: "", options: .displayInline, children: [
BlogDashboardHelpers.makeHideCardAction(for: .quickStart, siteID: blog.dotComID?.intValue ?? 0)
])
cardFrameView.addMoreMenu(items: [
BlogDashboardHelpers.makeHideCardAction(for: .quickStart, blog: blog)
], card: .quickStart)
} else {
cardFrameView.onEllipsisButtonTap = { [weak self] in
guard let self = self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ extension DashboardStatsCardCell: BlogDashboardCardConfigurable {
}

if FeatureFlag.personalizeHomeTab.enabled {
frameView.onEllipsisButtonTap = {
BlogDashboardAnalytics.trackContextualMenuAccessed(for: .todaysStats)
}
frameView.ellipsisButton.showsMenuAsPrimaryAction = true
frameView.ellipsisButton.menu = UIMenu(title: "", options: .displayInline, children: [
BlogDashboardHelpers.makeHideCardAction(for: .todaysStats, siteID: blog.dotComID?.intValue ?? 0)
])
frameView.addMoreMenu(items: [
BlogDashboardHelpers.makeHideCardAction(for: .todaysStats, blog: blog)
], card: .todaysStats)
}

statsStackView?.views = viewModel?.todaysViews
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ enum DashboardCard: String, CaseIterable {
.scheduledPosts,
.blaze,
.prompts,
.quickStart,
.pages,
.activityLog
.quickStart
]

/// Includes all cards that should be fetched from the backend
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation

struct BlogDashboardHelpers {
static func makeHideCardAction(for card: DashboardCard, siteID: Int) -> UIAction {
static func makeHideCardAction(for card: DashboardCard, blog: Blog) -> UIAction {
UIAction(
title: Strings.hideThis,
image: UIImage(systemName: "minus.circle"),
attributes: [.destructive],
handler: { _ in
BlogDashboardAnalytics.trackHideTapped(for: card)
BlogDashboardPersonalizationService(siteID: siteID)
BlogDashboardPersonalizationService(siteID: blog.dotComID?.intValue ?? 0)
.setEnabled(false, for: card)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private extension DashboardCard {
case .newSite:
return NSLocalizedString("personalizeHome.dashboardCard.nextSteps", value: "Next steps", comment: "Card title for the pesonalization menu")
}
case .nextPost, .createPost, .ghost, .failure, .personalize, .jetpackBadge, .jetpackInstall, .empty, .domainsDashboardCard:
case .nextPost, .createPost, .ghost, .failure, .personalize, .jetpackBadge, .jetpackInstall, .empty, .domainsDashboardCard, .freeToPaidPlansDashboardCard, .domainRegistration:
assertionFailure("\(self) card should not appear in the personalization menus")
return "" // These cards don't appear in the personalization menus
}
Expand Down

0 comments on commit 81be484

Please sign in to comment.