<commit_before>//
//  MasterTimelineAvatarView.swift
//  NetNewsWire-iOS
//
//  Created by <NAME> on 9/17/19.
//  Copyright © 2019 Ranchero Software. All rights reserved.
//

import UIKit

final class MasterTimelineAvatarView: UIView {

	var image: UIImage? = nil {
		didSet {
			if image !== oldValue {
				imageView.image = image
				setNeedsLayout()
				setNeedsDisplay()
			}
		}
	}

	private let imageView: UIImageView = {
		let imageView = NonIntrinsicImageView(image: AppAssets.faviconTemplateImage)
		imageView.contentMode = .scaleAspectFit
		return imageView
	}()

	private var hasExposedVerticalBackground: Bool {
		return imageView.frame.size.height < bounds.size.height
	}

	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()
		setNeedsDisplay()
	}

	override func layoutSubviews() {
		imageView.setFrameIfNotEqual(rectForImageView())
	}

	override func draw(_ dirtyRect: CGRect) {
		if hasExposedVerticalBackground {
			AppAssets.avatarBackgroundColor.set()
		} else {
			UIColor.systemBackground.set()
		}
		UIRectFill(dirtyRect)
	}
}

private extension MasterTimelineAvatarView {

	func commonInit() {
		addSubview(imageView)
	}

	func rectForImageView() -> CGRect {
		guard let image = image else {
			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)
			}<commit_msg>Fix avatar background composition<commit_after>//
//  MasterTimelineAvatarView.swift
//  NetNewsWire-iOS
//
//  Created by <NAME> on 9/17/19.
//  Copyright © 2019 Ranchero Software. All rights reserved.
//

import UIKit

final class MasterTimelineAvatarView: UIView {

	var image: UIImage? = nil {
		didSet {
			if image !== oldValue {
				imageView.image = image
				setNeedsLayout()
			}
		}
	}

	private let imageView: UIImageView = {
		let imageView = NonIntrinsicImageView(image: AppAssets.faviconTemplateImage)
		imageView.contentMode = .scaleAspectFit
		return imageView
	}()

	private var hasExposedVerticalBackground: Bool {
		return imageView.frame.size.height < bounds.size.height
	}

	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())
		if hasExposedVerticalBackground {
			backgroundColor = AppAssets.avatarBackgroundColor
		} else {
			backgroundColor = nil
		}
	}

}

private extension MasterTimelineAvatarView {

	func commonInit() {
		addSubview(imageView)
	}

	func rectForImageView() -> CGRect {
		guard let image = image else {
			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)
			}
{'fraction_non_alphanumeric': 0.1047425080011638, 'fraction_numerical': 0.009310445155659005, 'mean_word_length': 3.639676113360324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3951241', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1137, 'n_words': 269}