From a01109c7226fac3f10cf951f463dbf18a4734d8e Mon Sep 17 00:00:00 2001 From: Kyle Hickinson Date: Thu, 12 Dec 2024 14:33:52 -0500 Subject: [PATCH] Improve action handler spamming, fix nits & add comments --- .../Sources/BrowserMenu/BrowserMenu.swift | 12 ++++++++++- .../BrowserMenu/BrowserMenuModel.swift | 2 +- .../Sources/BrowserMenu/Identifiers.swift | 21 +++++++++++++++++++ .../Sources/BrowserMenu/VPNStatus.swift | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/ios/brave-ios/Sources/BrowserMenu/BrowserMenu.swift b/ios/brave-ios/Sources/BrowserMenu/BrowserMenu.swift index 7f56595f0027..535d846e0eb6 100644 --- a/ios/brave-ios/Sources/BrowserMenu/BrowserMenu.swift +++ b/ios/brave-ios/Sources/BrowserMenu/BrowserMenu.swift @@ -18,11 +18,20 @@ public struct BrowserMenu: View { /// Whether or not we may be viewing on a device that doesn't have as much horizontal space /// available (such as when Display Zoom is enabled) @State private var isHorizontalSpaceRestricted: Bool = false + @State private var activeActionHandlers: [Action.ID: Task] = [:] @Environment(\.dynamicTypeSize) private var dynamicTypeSize private func handleAction(_ action: Binding) { - Task { + let id = action.wrappedValue.id + if activeActionHandlers[id] != nil { + // The action is in progress + return + } + let actionTask = Task { + defer { + activeActionHandlers[id] = nil + } let result = await action.wrappedValue.handler(action.wrappedValue) switch result { case .updateAction(let replacement): @@ -34,6 +43,7 @@ public struct BrowserMenu: View { break } } + activeActionHandlers[id] = actionTask } private var numberOfQuickActions: Int { diff --git a/ios/brave-ios/Sources/BrowserMenu/BrowserMenuModel.swift b/ios/brave-ios/Sources/BrowserMenu/BrowserMenuModel.swift index 281b1670fdee..930d47496e2d 100644 --- a/ios/brave-ios/Sources/BrowserMenu/BrowserMenuModel.swift +++ b/ios/brave-ios/Sources/BrowserMenu/BrowserMenuModel.swift @@ -86,7 +86,7 @@ import SwiftUI } func updateActionVisibility(_ action: Action, visibility: Action.Visibility) { - actionVisibility.value[action.id.id] = visibility == .visible ? true : false + actionVisibility.value[action.id.id] = visibility == .visible if visibility == .visible { // If the user is making a hidden item visible and the visible list is empty, we won't // give it an overridden rank to ensure it ends at the bottom diff --git a/ios/brave-ios/Sources/BrowserMenu/Identifiers.swift b/ios/brave-ios/Sources/BrowserMenu/Identifiers.swift index 77017a3e4912..e10561534734 100644 --- a/ios/brave-ios/Sources/BrowserMenu/Identifiers.swift +++ b/ios/brave-ios/Sources/BrowserMenu/Identifiers.swift @@ -6,6 +6,27 @@ import BraveWallet import Foundation +/// The list of action identifiers +/// +/// Every action that is shown in the menu must have an identifier created here so that it may be +/// properly displayed and ordered in the new menu customization. The new menu should use every +/// identifier that is usable even if the the action cannot be made in the current context so that +/// user ordering & visibility is consistent. +/// +/// **A note on rankings & visibility**: +/// The new menu relies on 2 things for default item display: default ranking & default visibility. +/// +/// Default rankings & visibility are currently based on a list in Figma and shouldn't be altered to +/// ensure items don't move around on the user. +/// +/// When a new menu item is added it should be given a new default ranking that isn't currently +/// used. Currently all rankings are incremented by 100 to allow for new items to be added in +/// between, so for example a new action identifier could be given a default rank of 175 if we +/// wanted it to be placed second in the list if no user customization has occured or third in the +/// list if it has (reordering sets overriden ranks based on the halfway point between items). +/// +/// Default visibility controls whether or not the item appears on the menu without the user tapping +/// "Show All…" to display all actions or explicitly adding it to the menu themselves. extension Action.Identifier { public static let vpn: Self = .init( diff --git a/ios/brave-ios/Sources/BrowserMenu/VPNStatus.swift b/ios/brave-ios/Sources/BrowserMenu/VPNStatus.swift index ad88fa030c92..2bbbbae0ee04 100644 --- a/ios/brave-ios/Sources/BrowserMenu/VPNStatus.swift +++ b/ios/brave-ios/Sources/BrowserMenu/VPNStatus.swift @@ -28,7 +28,7 @@ struct VPNRegion: Equatable { } private static func flagEmojiForCountryCode(code: String) -> String { - // Root Unicode flags index + // Regional indicator symbol root Unicode flags index let rootIndex: UInt32 = 127397 var unicodeScalarView = ""