Skip to content

Commit

Permalink
Improve action handler spamming, fix nits & add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Dec 12, 2024
1 parent 46cdf57 commit a01109c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
12 changes: 11 additions & 1 deletion ios/brave-ios/Sources/BrowserMenu/BrowserMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Never>] = [:]

@Environment(\.dynamicTypeSize) private var dynamicTypeSize

private func handleAction(_ action: Binding<Action>) {
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):
Expand All @@ -34,6 +43,7 @@ public struct BrowserMenu: View {
break
}
}
activeActionHandlers[id] = actionTask
}

private var numberOfQuickActions: Int {
Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/BrowserMenu/BrowserMenuModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions ios/brave-ios/Sources/BrowserMenu/Identifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/BrowserMenu/VPNStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand Down

0 comments on commit a01109c

Please sign in to comment.