Skip to content

Commit

Permalink
added badge observers
Browse files Browse the repository at this point in the history
  • Loading branch information
askopin committed Mar 31, 2019
1 parent 92d0dc8 commit 3db0b55
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
37 changes: 34 additions & 3 deletions CBTabBarController/Classes/CBBaseTabButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ class CBBaseTabButton: UIButton, CBTabBarButtonProtocol {
var badgeContainer = UIView()
var badgeLabel = UILabel()
var item: UITabBarItem? {
set {
guard let item = newValue else { return }
_item = item
}
get {
return _item
}
}

@objc dynamic var _item: UITabBarItem = UITabBarItem() {
didSet {
didUpdateItem()
}
Expand All @@ -25,19 +35,30 @@ class CBBaseTabButton: UIButton, CBTabBarButtonProtocol {
override init(frame: CGRect) {
super.init(frame: frame)
configure()
addObservers()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
addObservers()
}

required init(item: UITabBarItem) {
super.init(frame: .zero)
defer {
self.item = item
}
configure()
addObservers()
self.item = item
}

deinit {
removeObserver(self, forKeyPath: #keyPath(_item.badgeValue))
removeObserver(self, forKeyPath: #keyPath(_item.badgeColor))
}

func addObservers() {
addObserver(self, forKeyPath: #keyPath(_item.badgeValue), options: [.initial, .new], context: nil)
addObserver(self, forKeyPath: #keyPath(_item.badgeColor), options: [.initial, .new], context: nil)
}

func configure() {
Expand Down Expand Up @@ -107,5 +128,15 @@ class CBBaseTabButton: UIButton, CBTabBarButtonProtocol {
badgeContainer.backgroundColor = item?.badgeColor ?? tintColor
badgeLabel.text = item?.badgeValue
badgeContainer.isHidden = item?.badgeValue == nil
setNeedsLayout()
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case #keyPath(_item.badgeValue), #keyPath(_item.badgeColor):
didUpdateItem()
default:
break
}
}
}
1 change: 1 addition & 0 deletions CBTabBarController/Classes/Fade/CBFadeTabBarButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ class CBFadeTabBarButton: CBBaseTabButton {
override func didUpdateItem() {
super.didUpdateItem()
setImage(item?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
setNeedsLayout()
}
}
53 changes: 43 additions & 10 deletions CBTabBarController/Classes/Flashy/CBFlashyTabBarButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,45 @@ class CBFlashyTabBarButton: CBTabBarButton {
override init(frame: CGRect) {
super.init(frame: frame)
configureSubviews()
addObservers()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureSubviews()
addObservers()
}

required init(item: UITabBarItem) {
super.init(frame: .zero)
addObservers()
configureSubviews()
defer {
self.item = item
}
self.item = item
}


deinit {
removeObserver(self, forKeyPath: #keyPath(_item.badgeValue))
removeObserver(self, forKeyPath: #keyPath(_item.badgeColor))
}

private func addObservers() {
addObserver(self, forKeyPath: #keyPath(_item.badgeValue), options: [.initial, .new], context: nil)
addObserver(self, forKeyPath: #keyPath(_item.badgeColor), options: [.initial, .new], context: nil)
}

var item: UITabBarItem? {
set {
guard let item = newValue else { return }
_item = item
}
get {
return _item
}
}

@objc dynamic var _item: UITabBarItem = UITabBarItem(){
didSet {
tabImage.image = item?.image?.withRenderingMode(.alwaysTemplate)
tabLabel.attributedText = (item as? CBExtendedTabItem)?.attributedTitle ?? attributedText(fortitle: item?.title)
badgeContainer.backgroundColor = item?.badgeColor ?? tintColor
badgeLabel.text = item?.badgeValue
badgeContainer.isHidden = item?.badgeValue == nil
setNeedsLayout()
didUpdateItem()
}
}

Expand Down Expand Up @@ -175,5 +191,22 @@ class CBFlashyTabBarButton: CBTabBarButton {
}
}
}

private func didUpdateItem() {
tabImage.image = item?.image?.withRenderingMode(.alwaysTemplate)
tabLabel.attributedText = (item as? CBExtendedTabItem)?.attributedTitle ?? attributedText(fortitle: item?.title)
badgeContainer.backgroundColor = item?.badgeColor ?? tintColor
badgeLabel.text = item?.badgeValue
badgeContainer.isHidden = item?.badgeValue == nil
setNeedsLayout()
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case #keyPath(_item.badgeValue), #keyPath(_item.badgeColor):
didUpdateItem()
default:
break
}
}
}
6 changes: 6 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class ViewController: UIViewController {
let settingsVC = CBSampleViewController()
settingsVC.tabBarItem = SampleTabItem(title: "Settings", image: #imageLiteral(resourceName: "Settings"), tag: 0)
settingsVC.tabBarItem?.badgeColor = .red
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
settingsVC.tabBarItem?.badgeValue = "20"
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
settingsVC.tabBarItem?.badgeValue = "200"
}
settingsVC.tabBarItem?.badgeValue = "1"
settingsVC.inverseColor()

Expand Down

0 comments on commit 3db0b55

Please sign in to comment.