Refactor action-menu to better support sections (IOS-103)

This commit is contained in:
Nathan Mattes 2023-06-02 11:55:35 +02:00
parent 8b69ff3acd
commit 846fa44369
4 changed files with 29 additions and 24 deletions

View File

@ -428,7 +428,7 @@ extension ProfileViewController {
}
let menu = MastodonMenu.setupMenu(
actions: menuActions,
actions: [menuActions],
delegate: self
)
return menu

View File

@ -460,9 +460,10 @@ extension NotificationView {
public typealias AuthorMenuContext = StatusAuthorView.AuthorMenuContext
public func setupAuthorMenu(menuContext: AuthorMenuContext) -> (UIMenu, [UIAccessibilityCustomAction]) {
var actions: [MastodonMenu.Action] = []
actions = [
var actions: [[MastodonMenu.Action]] = []
var upperActions: [MastodonMenu.Action] = []
upperActions = [
.muteUser(.init(
name: menuContext.name,
isMuting: menuContext.isMuting
@ -473,11 +474,13 @@ extension NotificationView {
)),
.reportUser(
.init(name: menuContext.name)
),
)
]
actions.append(upperActions)
if menuContext.isMyself {
actions.append(.deleteStatus)
actions.append([.deleteStatus])
}

View File

@ -157,20 +157,21 @@ extension StatusAuthorView {
}
public func setupAuthorMenu(menuContext: AuthorMenuContext) -> (UIMenu, [UIAccessibilityCustomAction]) {
var actions = [MastodonMenu.Action]()
var actions: [[MastodonMenu.Action]] = []
var upperActions: [MastodonMenu.Action] = []
if menuContext.isMyself {
actions.append(.editStatus)
upperActions.append(.editStatus)
}
if !menuContext.isMyself {
if let statusLanguage = menuContext.statusLanguage, menuContext.isTranslationEnabled, !menuContext.isTranslated {
actions.append(
upperActions.append(
.translateStatus(.init(language: statusLanguage))
)
}
actions.append(contentsOf: [
upperActions.append(contentsOf: [
.muteUser(.init(
name: menuContext.name,
isMuting: menuContext.isMuting
@ -185,15 +186,17 @@ extension StatusAuthorView {
])
}
actions.append(contentsOf: [
upperActions.append(contentsOf: [
.bookmarkStatus(
.init(isBookmarking: menuContext.isBookmarking)
),
.shareStatus
])
actions.append(upperActions)
if menuContext.isMyself {
actions.append(.deleteStatus)
actions.append([.deleteStatus])
}

View File

@ -14,31 +14,30 @@ public protocol MastodonMenuDelegate: AnyObject {
public enum MastodonMenu {
public static func setupMenu(
actions: [Action],
actions: [[Action]],
delegate: MastodonMenuDelegate
) -> UIMenu {
var children: [UIMenuElement] = []
for action in actions {
let element: UIMenuElement
if case let .deleteStatus = action {
let deleteAction = action.build(delegate: delegate).menuElement
element = UIMenu(options: .displayInline, children: [deleteAction])
} else {
element = action.build(delegate: delegate).menuElement
for actionGroup in actions {
var submenuChildren: [UIMenuElement] = []
for action in actionGroup {
let element = action.build(delegate: delegate).menuElement
submenuChildren.append(element)
}
children.append(element)
let submenu = UIMenu(options: .displayInline, children: submenuChildren)
children.append(submenu)
}
return UIMenu(children: children)
}
public static func setupAccessibilityActions(
actions: [Action],
actions: [[Action]],
delegate: MastodonMenuDelegate
) -> [UIAccessibilityCustomAction] {
var accessibilityActions: [UIAccessibilityCustomAction] = []
for action in actions {
for action in actions.flatMap({ $0 }) {
let element = action.build(delegate: delegate)
accessibilityActions.append(element.accessibilityCustomAction)
}