Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add property to override badge style in navigation bar #1977

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ class RootViewController: UIViewController, UITableViewDataSource, UITableViewDe
var showSearchProgressSpinner: Bool = true
var showRainbowRingForAvatar: Bool = false
var showBadgeOnBarButtonItem: Bool = false
var updateBadgeLabelStyle: Bool = false

var allowsCellSelection: Bool = false {
didSet {
Expand Down Expand Up @@ -624,6 +625,20 @@ class RootViewController: UIViewController, UITableViewDataSource, UITableViewDe
}

if indexPath.row == 3 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: BooleanCell.identifier, for: indexPath) as? BooleanCell else {
return UITableViewCell()
}
cell.setup(title: "Override Badge Label Style",
isOn: updateBadgeLabelStyle,
isSwitchEnabled: navigationItem.titleStyle == .largeLeading)
cell.titleNumberOfLines = 0
cell.onValueChanged = { [weak self, weak cell] in
self?.updateBadgeStyle(isOn: cell?.isOn ?? false)
}
return cell
}

if indexPath.row == 4 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: ActionsCell.identifier, for: indexPath) as? ActionsCell else {
return UITableViewCell()
}
Expand Down Expand Up @@ -748,6 +763,12 @@ class RootViewController: UIViewController, UITableViewDataSource, UITableViewDe
showBadgeOnBarButtonItem = isOn
}

@objc private func updateBadgeStyle(isOn: Bool) {
let navigationBar = msfNavigationController?.msfNavigationBar
navigationBar?.overriddenBadgeLabelStyle = isOn ? NavigationBar.BadgeLabelStyleWrapper(style: .system) : nil
updateBadgeLabelStyle = isOn
}

@objc private func showTooltipButtonPressed() {
let navigationBar = msfNavigationController?.msfNavigationBar
guard let view = navigationBar?.barButtonItemView(with: BarButtonItemTag.threeDay.rawValue) else {
Expand Down
2 changes: 1 addition & 1 deletion ios/FluentUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@
FD41C87022DD13230086F899 /* ShyHeaderController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShyHeaderController.swift; sourceTree = "<group>"; };
FD41C87122DD13230086F899 /* ShyHeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShyHeaderView.swift; sourceTree = "<group>"; };
FD41C87A22DD13230086F899 /* AvatarTitleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarTitleView.swift; sourceTree = "<group>"; };
FD41C87B22DD13230086F899 /* NavigationBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationBar.swift; sourceTree = "<group>"; };
FD41C87B22DD13230086F899 /* NavigationBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationBar.swift; sourceTree = "<group>"; usesTabs = 0; };
FD41C87E22DD13230086F899 /* SearchBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
FD41C87F22DD13230086F899 /* NavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationController.swift; sourceTree = "<group>"; };
FD41C88022DD13230086F899 /* NavigationAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationAnimator.swift; sourceTree = "<group>"; };
Expand Down
20 changes: 19 additions & 1 deletion ios/FluentUI/Navigation/NavigationBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ open class NavigationBar: UINavigationBar, TokenizedControlInternal, TwoLineTitl
}
}

@objc public class BadgeLabelStyleWrapper: NSObject {
public var style: BadgeLabelStyle
@objc public init(style: BadgeLabelStyle) {
self.style = style
}
}

@objc public static func navigationBarBackgroundColor(fluentTheme: FluentTheme?) -> UIColor {
return backgroundColor(for: .system, theme: fluentTheme)
}
Expand Down Expand Up @@ -302,6 +309,15 @@ open class NavigationBar: UINavigationBar, TokenizedControlInternal, TwoLineTitl
// @objc dynamic - so we can do KVO on this
@objc dynamic private(set) var style: Style = defaultStyle

// Override value for BadgeLabel Style. We can set to nil when we don't wanna override.
@objc public var overriddenBadgeLabelStyle: BadgeLabelStyleWrapper? {
didSet {
if let navigationItem = topItem {
updateBarButtonItems(with: navigationItem)
}
}
}

private var systemWantsCompactNavigationBar: Bool {
return traitCollection.horizontalSizeClass == .compact && traitCollection.verticalSizeClass == .compact
}
Expand Down Expand Up @@ -724,7 +740,9 @@ open class NavigationBar: UINavigationBar, TokenizedControlInternal, TwoLineTitl
private func createBarButtonItemButton(with item: UIBarButtonItem, isLeftItem: Bool) -> UIButton {
let button = BadgeLabelButton(type: .system)
button.item = item
if style == .system {
if let badgeLabelStyle = overriddenBadgeLabelStyle?.style {
button.badgeLabelStyle = badgeLabelStyle
} else if style == .system {
button.badgeLabelStyle = .system
} else if style == .gradient {
button.badgeLabelStyle = .brand
Expand Down
Loading