From 393e4632da334fa2fc79686ffb7d3acf929fdcf5 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Wed, 9 Nov 2022 16:33:54 -0500 Subject: [PATCH] Add secondary actions to notifications --- .../NotificationView+Configuration.swift | 1 + .../Transient/MastodonNotificationType.swift | 2 +- .../Content/NotificationView+ViewModel.swift | 56 ++++++++++++++++++- .../View/Content/NotificationView.swift | 31 ++++++++-- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/Mastodon/Scene/Share/View/Content/NotificationView+Configuration.swift b/Mastodon/Scene/Share/View/Content/NotificationView+Configuration.swift index 98d06fd92..daa37a93b 100644 --- a/Mastodon/Scene/Share/View/Content/NotificationView+Configuration.swift +++ b/Mastodon/Scene/Share/View/Content/NotificationView+Configuration.swift @@ -111,6 +111,7 @@ extension NotificationView { self.viewModel.notificationIndicatorText = nil return } + self.viewModel.type = type func createMetaContent(text: String, emojis: MastodonContent.Emojis) -> MetaContent { let content = MastodonContent(content: text, emojis: emojis) diff --git a/MastodonSDK/Sources/CoreDataStack/Entity/Transient/MastodonNotificationType.swift b/MastodonSDK/Sources/CoreDataStack/Entity/Transient/MastodonNotificationType.swift index 9e3029f26..455230d5e 100644 --- a/MastodonSDK/Sources/CoreDataStack/Entity/Transient/MastodonNotificationType.swift +++ b/MastodonSDK/Sources/CoreDataStack/Entity/Transient/MastodonNotificationType.swift @@ -7,7 +7,7 @@ import Foundation -public enum MastodonNotificationType: RawRepresentable { +public enum MastodonNotificationType: RawRepresentable, Equatable { case follow case followRequest case mention diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift index 524c92146..b80486e14 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift @@ -25,7 +25,8 @@ extension NotificationView { let logger = Logger(subsystem: "NotificationView", category: "ViewModel") @Published public var authContext: AuthContext? - + + @Published public var type: MastodonNotificationType? @Published public var notificationIndicatorText: MetaContent? @Published public var authorAvatarImage: UIImage? @@ -145,6 +146,55 @@ extension NotificationView.ViewModel { } } .store(in: &disposeBag) + + Publishers.CombineLatest( + $authorAvatarImage, + $type + ) + .sink { avatarImage, type in + var actions = [UIAccessibilityCustomAction]() + + // these notifications can be directly actioned to view the profile + if type != .follow, type != .followRequest { + actions.append( + UIAccessibilityCustomAction( + name: L10n.Common.Controls.Status.showUserProfile, + image: avatarImage + ) { [weak notificationView] _ in + guard let notificationView = notificationView, let delegate = notificationView.delegate else { return false } + delegate.notificationView(notificationView, authorAvatarButtonDidPressed: notificationView.avatarButton) + return true + } + ) + } + + if type == .followRequest { + actions.append( + UIAccessibilityCustomAction( + name: L10n.Common.Controls.Actions.confirm, + image: Asset.Editing.checkmark20.image + ) { [weak notificationView] _ in + guard let notificationView = notificationView, let delegate = notificationView.delegate else { return false } + delegate.notificationView(notificationView, acceptFollowRequestButtonDidPressed: notificationView.acceptFollowRequestButton) + return true + } + ) + + actions.append( + UIAccessibilityCustomAction( + name: L10n.Common.Controls.Actions.delete, + image: Asset.Circles.forbidden20.image + ) { [weak notificationView] _ in + guard let notificationView = notificationView, let delegate = notificationView.delegate else { return false } + delegate.notificationView(notificationView, rejectFollowRequestButtonDidPressed: notificationView.rejectFollowRequestButton) + return true + } + ) + } + + notificationView.notificationActions = actions + } + .store(in: &disposeBag) } private func bindAuthorMenu(notificationView: NotificationView) { @@ -167,7 +217,9 @@ extension NotificationView.ViewModel { isMyself: isMyself, isBookmarking: false // no bookmark action display for notification item ) - notificationView.menuButton.menu = notificationView.setupAuthorMenu(menuContext: menuContext) + let (menu, actions) = notificationView.setupAuthorMenu(menuContext: menuContext) + notificationView.menuButton.menu = menu + notificationView.authorActions = actions notificationView.menuButton.showsMenuAsPrimaryAction = true notificationView.menuButton.isHidden = menuContext.isMyself diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView.swift index f6821dec7..d4828875b 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView.swift @@ -46,7 +46,10 @@ public final class NotificationView: UIView { var _disposeBag = Set() public var disposeBag = Set() - + + var notificationActions = [UIAccessibilityCustomAction]() + var authorActions = [UIAccessibilityCustomAction]() + public private(set) lazy var viewModel: ViewModel = { let viewModel = ViewModel() viewModel.bind(notificationView: self) @@ -392,6 +395,21 @@ extension NotificationView { get { [] } set {} } + + public override var accessibilityCustomActions: [UIAccessibilityCustomAction]? { + get { + var actions = notificationActions + actions += authorActions + if !statusView.isHidden { + actions += statusView.accessibilityCustomActions ?? [] + } + if !quoteStatusViewContainerView.isHidden { + actions += quoteStatusView.accessibilityCustomActions ?? [] + } + return actions + } + set {} + } } extension NotificationView { @@ -449,7 +467,7 @@ extension NotificationView: AdaptiveContainerView { extension NotificationView { public typealias AuthorMenuContext = StatusAuthorView.AuthorMenuContext - public func setupAuthorMenu(menuContext: AuthorMenuContext) -> UIMenu { + public func setupAuthorMenu(menuContext: AuthorMenuContext) -> (UIMenu, [UIAccessibilityCustomAction]) { var actions: [MastodonMenu.Action] = [] actions = [ @@ -475,8 +493,13 @@ extension NotificationView { actions: actions, delegate: self ) - - return menu + + let accessibilityActions = MastodonMenu.setupAccessibilityActions( + actions: actions, + delegate: self + ) + + return (menu, accessibilityActions) } }