Add secondary actions to notifications

This commit is contained in:
Jed Fox 2022-11-09 16:33:54 -05:00
parent b3bc6dc273
commit 393e4632da
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
4 changed files with 83 additions and 7 deletions

View File

@ -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)

View File

@ -7,7 +7,7 @@
import Foundation
public enum MastodonNotificationType: RawRepresentable {
public enum MastodonNotificationType: RawRepresentable, Equatable {
case follow
case followRequest
case mention

View File

@ -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

View File

@ -46,7 +46,10 @@ public final class NotificationView: UIView {
var _disposeBag = Set<AnyCancellable>()
public var disposeBag = Set<AnyCancellable>()
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)
}
}