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 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -490,6 +490,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 {
@@ -642,6 +643,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()
}
@@ -766,6 +781,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 {
20 changes: 19 additions & 1 deletion Sources/FluentUI_iOS/Components/Navigation/NavigationBar.swift
Original file line number Diff line number Diff line change
@@ -126,6 +126,13 @@ open class NavigationBar: UINavigationBar, TokenizedControl, TwoLineTitleViewDel
}
}

@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)
}
@@ -306,6 +313,15 @@ open class NavigationBar: UINavigationBar, TokenizedControl, TwoLineTitleViewDel
// @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
}
@@ -735,7 +751,9 @@ open class NavigationBar: UINavigationBar, TokenizedControl, TwoLineTitleViewDel
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