2019-04-15 22:03:05 +02:00
|
|
|
//
|
|
|
|
// MasterUnreadCountView.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/22/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2019-04-23 15:00:27 +02:00
|
|
|
class MasterFeedUnreadCountView : UIView {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-10-01 03:01:02 +02:00
|
|
|
var padding: UIEdgeInsets {
|
2019-10-27 05:19:37 +01:00
|
|
|
return UIEdgeInsets(top: 1.0, left: 9.0, bottom: 1.0, right: 9.0)
|
2019-10-01 03:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let cornerRadius = 8.0
|
2019-10-28 21:16:56 +01:00
|
|
|
let bgColor = AppAssets.controlBackgroundColor
|
2019-10-01 03:01:02 +02:00
|
|
|
var textColor: UIColor {
|
|
|
|
return UIColor.white
|
|
|
|
}
|
|
|
|
|
|
|
|
var textAttributes: [NSAttributedString.Key: AnyObject] {
|
2019-10-28 20:15:04 +01:00
|
|
|
let textFont = UIFont.preferredFont(forTextStyle: .caption1).bold()
|
2019-04-28 22:44:35 +02:00
|
|
|
return [NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.font: textFont, NSAttributedString.Key.kern: NSNull()]
|
|
|
|
}
|
2019-10-01 03:01:02 +02:00
|
|
|
var textSizeCache = [Int: CGSize]()
|
2019-04-27 16:49:26 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
var unreadCount = 0 {
|
|
|
|
didSet {
|
2019-08-26 03:06:02 +02:00
|
|
|
contentSizeIsValid = false
|
2019-10-01 03:01:02 +02:00
|
|
|
invalidateIntrinsicContentSize()
|
2019-04-15 22:03:05 +02:00
|
|
|
setNeedsDisplay()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var unreadCountString: String {
|
|
|
|
return unreadCount < 1 ? "" : "\(unreadCount)"
|
|
|
|
}
|
|
|
|
|
2019-08-26 03:00:34 +02:00
|
|
|
private var contentSizeIsValid = false
|
|
|
|
private var _contentSize = CGSize.zero
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
self.isOpaque = false
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
super.init(coder: aDecoder)
|
|
|
|
self.isOpaque = false
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:44:35 +02:00
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
textSizeCache = [Int: CGSize]()
|
2019-08-26 03:00:34 +02:00
|
|
|
contentSizeIsValid = false
|
2019-04-28 22:44:35 +02:00
|
|
|
setNeedsDisplay()
|
|
|
|
}
|
|
|
|
|
2019-08-26 03:00:34 +02:00
|
|
|
var contentSize: CGSize {
|
|
|
|
if !contentSizeIsValid {
|
2019-04-15 22:03:05 +02:00
|
|
|
var size = CGSize.zero
|
|
|
|
if unreadCount > 0 {
|
|
|
|
size = textSize()
|
|
|
|
size.width += (padding.left + padding.right)
|
|
|
|
size.height += (padding.top + padding.bottom)
|
|
|
|
}
|
2019-08-26 03:00:34 +02:00
|
|
|
_contentSize = size
|
|
|
|
contentSizeIsValid = true
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-08-26 03:00:34 +02:00
|
|
|
return _contentSize
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 03:00:34 +02:00
|
|
|
// Prevent autolayout from messing around with our frame settings
|
|
|
|
override var intrinsicContentSize: CGSize {
|
|
|
|
return CGSize(width: UIView.noIntrinsicMetric, height: UIView.noIntrinsicMetric)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-10-01 03:01:02 +02:00
|
|
|
func textSize() -> CGSize {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
if unreadCount < 1 {
|
|
|
|
return CGSize.zero
|
|
|
|
}
|
|
|
|
|
|
|
|
if let cachedSize = textSizeCache[unreadCount] {
|
|
|
|
return cachedSize
|
|
|
|
}
|
|
|
|
|
|
|
|
var size = unreadCountString.size(withAttributes: textAttributes)
|
|
|
|
size.height = ceil(size.height)
|
|
|
|
size.width = ceil(size.width)
|
|
|
|
|
|
|
|
textSizeCache[unreadCount] = size
|
|
|
|
return size
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-01 03:01:02 +02:00
|
|
|
func textRect() -> CGRect {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
let size = textSize()
|
|
|
|
var r = CGRect.zero
|
|
|
|
r.size = size
|
|
|
|
r.origin.x = (bounds.maxX - padding.right) - r.size.width
|
|
|
|
r.origin.y = padding.top
|
|
|
|
return r
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override func draw(_ dirtyRect: CGRect) {
|
|
|
|
|
|
|
|
let cornerRadii = CGSize(width: cornerRadius, height: cornerRadius)
|
|
|
|
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: cornerRadii)
|
|
|
|
bgColor.setFill()
|
|
|
|
path.fill()
|
|
|
|
|
|
|
|
if unreadCount > 0 {
|
|
|
|
unreadCountString.draw(at: textRect().origin, withAttributes: textAttributes)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|