2019-09-18 00:00:23 +02:00
|
|
|
//
|
2019-11-06 01:05:57 +01:00
|
|
|
// IconView.swift
|
2019-09-18 00:00:23 +02:00
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 9/17/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
final class IconView: UIView {
|
2019-09-18 00:00:23 +02:00
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
var iconImage: IconImage? = nil {
|
2019-09-18 00:00:23 +02:00
|
|
|
didSet {
|
2019-11-06 01:05:57 +01:00
|
|
|
if iconImage !== oldValue {
|
|
|
|
imageView.image = iconImage?.image
|
2019-10-29 02:57:26 +01:00
|
|
|
|
|
|
|
if self.traitCollection.userInterfaceStyle == .dark {
|
2020-03-13 14:21:38 +01:00
|
|
|
if self.iconImage?.isDark ?? false {
|
2021-03-06 23:25:10 +01:00
|
|
|
self.isDiscernable = false
|
2020-03-13 14:21:38 +01:00
|
|
|
self.setNeedsLayout()
|
|
|
|
} else {
|
2021-03-06 23:25:10 +01:00
|
|
|
self.isDiscernable = true
|
2020-03-13 14:21:38 +01:00
|
|
|
self.setNeedsLayout()
|
|
|
|
}
|
2019-10-29 02:57:26 +01:00
|
|
|
} else {
|
2020-03-13 14:21:38 +01:00
|
|
|
if self.iconImage?.isBright ?? false {
|
2021-03-06 23:25:10 +01:00
|
|
|
self.isDiscernable = false
|
2020-03-13 14:21:38 +01:00
|
|
|
self.setNeedsLayout()
|
|
|
|
} else {
|
2021-03-06 23:25:10 +01:00
|
|
|
self.isDiscernable = true
|
2020-03-13 14:21:38 +01:00
|
|
|
self.setNeedsLayout()
|
|
|
|
}
|
2019-10-29 02:57:26 +01:00
|
|
|
}
|
2020-03-13 11:54:31 +01:00
|
|
|
self.setNeedsLayout()
|
2019-09-18 00:00:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:25:10 +01:00
|
|
|
private var isDiscernable = true
|
2019-10-29 02:57:26 +01:00
|
|
|
|
2019-09-18 00:00:23 +02:00
|
|
|
private let imageView: UIImageView = {
|
2019-09-18 03:26:49 +02:00
|
|
|
let imageView = NonIntrinsicImageView(image: AppAssets.faviconTemplateImage)
|
2019-09-18 00:00:23 +02:00
|
|
|
imageView.contentMode = .scaleAspectFit
|
2019-09-26 22:50:05 +02:00
|
|
|
imageView.clipsToBounds = true
|
|
|
|
imageView.layer.cornerRadius = 2.0
|
2019-09-18 00:00:23 +02:00
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
2019-10-29 02:57:26 +01:00
|
|
|
private var isVerticalBackgroundExposed: Bool {
|
2019-09-18 00:00:23 +02:00
|
|
|
return imageView.frame.size.height < bounds.size.height
|
|
|
|
}
|
|
|
|
|
2019-10-29 02:57:26 +01:00
|
|
|
private var isSymbolImage: Bool {
|
2020-10-25 01:15:59 +02:00
|
|
|
return iconImage?.isSymbol ?? false
|
2019-10-29 02:57:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:25:22 +01:00
|
|
|
private var isBackgroundSuppressed: Bool {
|
|
|
|
return iconImage?.isBackgroundSupressed ?? false
|
|
|
|
}
|
|
|
|
|
2019-09-18 00:00:23 +02:00
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(frame: .zero)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didMoveToSuperview() {
|
|
|
|
setNeedsLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
imageView.setFrameIfNotEqual(rectForImageView())
|
2021-03-06 23:25:10 +01:00
|
|
|
if !isBackgroundSuppressed && ((iconImage != nil && isVerticalBackgroundExposed) || !isDiscernable) {
|
2019-11-06 01:05:57 +01:00
|
|
|
backgroundColor = AppAssets.iconBackgroundColor
|
2019-09-18 00:00:23 +02:00
|
|
|
} else {
|
2019-09-19 20:41:07 +02:00
|
|
|
backgroundColor = nil
|
2019-09-18 00:00:23 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 20:41:07 +02:00
|
|
|
|
2019-09-18 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
private extension IconView {
|
2019-09-18 00:00:23 +02:00
|
|
|
|
|
|
|
func commonInit() {
|
2019-11-06 01:05:57 +01:00
|
|
|
layer.cornerRadius = MasterTimelineDefaultCellLayout.iconCornerRadius
|
2019-10-29 02:01:15 +01:00
|
|
|
clipsToBounds = true
|
2019-09-18 00:00:23 +02:00
|
|
|
addSubview(imageView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rectForImageView() -> CGRect {
|
2019-11-06 01:05:57 +01:00
|
|
|
guard let image = iconImage?.image else {
|
2019-09-18 00:00:23 +02:00
|
|
|
return CGRect.zero
|
|
|
|
}
|
|
|
|
|
|
|
|
let imageSize = image.size
|
|
|
|
let viewSize = bounds.size
|
|
|
|
if imageSize.height == imageSize.width {
|
|
|
|
if imageSize.height >= viewSize.height * 0.75 {
|
|
|
|
// Close enough to viewSize to scale up the image.
|
|
|
|
return CGRect(x: 0.0, y: 0.0, width: viewSize.width, height: viewSize.height)
|
|
|
|
}
|
|
|
|
let offset = floor((viewSize.height - imageSize.height) / 2.0)
|
|
|
|
return CGRect(x: offset, y: offset, width: imageSize.width, height: imageSize.height)
|
|
|
|
}
|
|
|
|
else if imageSize.height > imageSize.width {
|
|
|
|
let factor = viewSize.height / imageSize.height
|
|
|
|
let width = imageSize.width * factor
|
|
|
|
let originX = floor((viewSize.width - width) / 2.0)
|
|
|
|
return CGRect(x: originX, y: 0.0, width: width, height: viewSize.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wider than tall: imageSize.width > imageSize.height
|
|
|
|
let factor = viewSize.width / imageSize.width
|
|
|
|
let height = imageSize.height * factor
|
|
|
|
let originY = floor((viewSize.height - height) / 2.0)
|
|
|
|
return CGRect(x: 0.0, y: originY, width: viewSize.width, height: height)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|