Answer the question
In order to leave comments, you need to log in
Badge icon, how to do?
I have a database of orders, which are displayed in the most common table view through customCell. The output is sorted by date.
Task: make the badge icon display the number of orders for today. This means this is a red circle with numbers in which it is usually displayed, for example, how many letters are unread or how many messages are unread in vatsap, for example. Here you also need to display the number of orders for today. Well, how the order was deleted - so that the number is updated. But regardless of whether the order entered or not, as long as it is not deleted and it has today's date, it is also considered. Please help, thanks)
Answer the question
In order to leave comments, you need to log in
Thank you all very much, here is the solution (this code is in VC):
let badgeCount: Int = 777
let application = UIApplication.shared
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound ]) { (granted, error) in }
application.registerForRemoteNotifications()
application.applicationIconBadgeNumber = badgeCount
But the code just outputs the number I specified.
Tell me how to calculate how many orders I have (well, that is, how many cells) for which the date coincides with today. That is, help with the code where let badgeCount: Int = how many orders have today's date.
date is displayed like this:
let dateString = dateFormatter.string(from: zakaz.zakazDate!)
cell.detailTextLabel?.text = dateString
or at least how to count how many orders there are for the entire table wiew?
I did something like this: (Maybe you can do better, maybe something ready is already there. For the sake of interest, I tried to do it myself)
class IconView: UIView {
var color: UIColor = .clear
var iconText: UILabel?
var popular: Datas.Popular?
var result: Result?
init(popular: Datas.Popular) {
super.init(frame: .zero)
initialize(with: popular)
}
init(result: Result) {
super.init(frame: .zero)
initializeResult(with: result)
}
// В принципе он тут не нужен вообще
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize(with popular: Datas.Popular? = nil) {
self.popular = popular
translatesAutoresizingMaskIntoConstraints = false
isOpaque = false
backgroundColor = .none
contentMode = .center
guard !(popular?.new == false && popular?.newPrice == nil) else { return }
if let isNew = popular?.new, isNew {
color = #colorLiteral(red: 0.5411764706, green: 0.6862745098, blue: 0.3411764706, alpha: 1)
} else {
color = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
}
var percent = Int()
if let stringWithNewPrice = popular?.newPrice, let stringWithOldPrice = popular?.price,
let newPrice = Int(stringWithNewPrice.removingWhitespaces()),
let oldPrice = Int(stringWithOldPrice.removingWhitespaces()) {
percent = Int(((oldPrice - newPrice) * 100) / oldPrice)
}
iconText = UILabel()
iconText?.text = (popular?.new)! ? "new" : "-\(percent)%"
iconText?.textColor = .white
iconText?.font = iconText?.font.withSize(8.5)
iconText?.isOpaque = false
iconText?.backgroundColor = .none
iconText?.frame.size = CGSize.zero
iconText?.sizeToFit()
addSubview(iconText!)
iconText?.translatesAutoresizingMaskIntoConstraints = false
iconText?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
iconText?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
private func initializeResult(with result: Result? = nil) {
self.result = result
translatesAutoresizingMaskIntoConstraints = false
isOpaque = false
backgroundColor = .none
contentMode = .center
guard !(result?.isNew == false && result?.oldPriceAmount == "") else { return }
if let isNew = result?.isNew, isNew {
color = #colorLiteral(red: 0.5411764706, green: 0.6862745098, blue: 0.3411764706, alpha: 1)
} else {
color = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
}
var percent = Int()
if let stringWithNewPrice = result?.priceAmount, let stringWithOldPrice = result?.oldPriceAmount,
let newPrice = Double(stringWithNewPrice),
let oldPrice = Double(stringWithOldPrice) {
percent = Int(((oldPrice - newPrice) * 100) / oldPrice)
}
contentMode = .redraw
iconText = UILabel()
iconText?.text = (result?.isNew)! ? "new" : "-\(percent)%"
iconText?.textColor = .white
iconText?.font = iconText?.font.withSize(8.5)
iconText?.isOpaque = false
iconText?.backgroundColor = .none
iconText?.frame.size = CGSize.zero
iconText?.sizeToFit()
addSubview(iconText!)
iconText?.translatesAutoresizingMaskIntoConstraints = false
iconText?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
iconText?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
override func draw(_ rect: CGRect) {
guard !(popular?.new == false && popular?.newPrice == nil) else { return }
let circle = UIBezierPath(
arcCenter: CGPoint(x: bounds.midX, y: bounds.midY),
radius: 10,
startAngle: 0,
endAngle: 2 * CGFloat.pi,
clockwise: true
)
circle.addClip()
color.setFill()
circle.fill()
}
}
private func updateUI() {
previewImage.image = popular.image
price.text = popular.price
name.text = popular.name
novelty = IconView(popular: popular)
addSubview(novelty)
let guide = layoutMarginsGuide
novelty.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1).isActive = true
novelty.leadingAnchor.constraintEqualToSystemSpacingAfter(guide.leadingAnchor, multiplier: 1).isActive = true
novelty.widthAnchor.constraint(equalToConstant: 20).isActive = true
novelty.heightAnchor.constraint(equalToConstant: 20).isActive = true
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question