Simplify iconImage:didSet. Move background color update to a private helper function.

This commit is contained in:
Brent Simmons 2021-04-24 17:55:48 -07:00
parent 954a676cbe
commit 1d4ef582a7

View File

@ -12,33 +12,24 @@ final class IconView: UIView {
var iconImage: IconImage? = nil { var iconImage: IconImage? = nil {
didSet { didSet {
if iconImage !== oldValue { guard iconImage !== oldValue else {
imageView.image = iconImage?.image return
if self.traitCollection.userInterfaceStyle == .dark {
if self.iconImage?.isDark ?? false {
self.isDiscernable = false
self.setNeedsLayout()
} else {
self.isDiscernable = true
self.setNeedsLayout()
}
} else {
if self.iconImage?.isBright ?? false {
self.isDiscernable = false
self.setNeedsLayout()
} else {
self.isDiscernable = true
self.setNeedsLayout()
}
}
self.setNeedsLayout()
} }
imageView.image = iconImage?.image
if traitCollection.userInterfaceStyle == .dark {
let isDark = iconImage?.isDark ?? false
isDiscernable = !isDark
}
else {
let isBright = iconImage?.isBright ?? false
isDiscernable = !isBright
}
setNeedsLayout()
} }
} }
private var isDiscernable = true private var isDiscernable = true
private let imageView: UIImageView = { private let imageView: UIImageView = {
let imageView = NonIntrinsicImageView(image: AppAssets.faviconTemplateImage) let imageView = NonIntrinsicImageView(image: AppAssets.faviconTemplateImage)
imageView.contentMode = .scaleAspectFit imageView.contentMode = .scaleAspectFit
@ -79,13 +70,8 @@ final class IconView: UIView {
override func layoutSubviews() { override func layoutSubviews() {
imageView.setFrameIfNotEqual(rectForImageView()) imageView.setFrameIfNotEqual(rectForImageView())
if !isBackgroundSuppressed && ((iconImage != nil && isVerticalBackgroundExposed) || !isDiscernable) { updateBackgroundColor()
backgroundColor = AppAssets.iconBackgroundColor
} else {
backgroundColor = nil
}
} }
} }
private extension IconView { private extension IconView {
@ -125,4 +111,11 @@ private extension IconView {
return CGRect(x: 0.0, y: originY, width: viewSize.width, height: height) return CGRect(x: 0.0, y: originY, width: viewSize.width, height: height)
} }
private func updateBackgroundColor() {
if !isBackgroundSuppressed && ((iconImage != nil && isVerticalBackgroundExposed) || !isDiscernable) {
backgroundColor = AppAssets.iconBackgroundColor
} else {
backgroundColor = nil
}
}
} }