Commit 687ec259 by Dmitriy Stepanets

Chnaged shorts overlay image rendering to fullscreen

parent 35fbac87
...@@ -70,10 +70,41 @@ class ShortsItemCell: UITableViewCell { ...@@ -70,10 +70,41 @@ class ShortsItemCell: UITableViewCell {
currentShorts = shortsItem currentShorts = shortsItem
if let backgroundImage = shortsItem.getOverlayImage(for: tableWidth) { if let backgroundImage = shortsItem.getOverlayImage(for: tableWidth) {
Nuke.loadImage(with: backgroundImage.url, into: backgroundImageView) Nuke.loadImage(with: backgroundImage.url, into: backgroundImageView) {[weak self] result in
guard let self = self else { return }
switch result {
case .success(let imageResponse):
onMain {
self.backgroundImageView.image = imageResponse.image
self.ctaButton.isHidden = false
self.likeButton.isHidden = false
//Update ImageView size
let aspect = imageResponse.image.size.width / imageResponse.image.size.height
self.backgroundImageView.snp.remakeConstraints { remake in
remake.left.top.right.equalToSuperview()
remake.bottom.equalToSuperview().priority(.medium)
remake.width.equalTo(self.backgroundImageView.snp.height).multipliedBy(aspect)
}
if let cachedColor = self.delegate?.averageColor(forImage: imageResponse.image,
identifier: backgroundImage.url.absoluteString) {
self.contentView.backgroundColor = cachedColor
}
else {
self.contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
}
}
default:
self.ctaButton.isHidden = true
self.likeButton.isHidden = true
}
}
} }
else { else {
backgroundImageView.backgroundColor = UIColor.lightGray self.ctaButton.isHidden = true
self.likeButton.isHidden = true
contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
} }
UIView.performWithoutAnimation { UIView.performWithoutAnimation {
...@@ -133,10 +164,13 @@ class ShortsItemCell: UITableViewCell { ...@@ -133,10 +164,13 @@ class ShortsItemCell: UITableViewCell {
//MARK:- Prepare //MARK:- Prepare
private extension ShortsItemCell { private extension ShortsItemCell {
func prepareForImageOnly() { func prepareForImageOnly() {
contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
backgroundImageView.contentMode = .scaleAspectFit backgroundImageView.contentMode = .scaleAspectFit
contentView.addSubview(backgroundImageView) contentView.addSubview(backgroundImageView)
backgroundImageView.snp.makeConstraints { make in backgroundImageView.snp.makeConstraints { make in
make.edges.equalToSuperview() make.left.top.right.equalToSuperview()
make.bottom.equalToSuperview().priority(.medium)
make.width.equalTo(backgroundImageView.snp.height).multipliedBy(1)
} }
} }
...@@ -228,6 +262,7 @@ private extension ShortsItemCell { ...@@ -228,6 +262,7 @@ private extension ShortsItemCell {
} }
func prepareActionButtons() { func prepareActionButtons() {
likeButton.isHidden = true
likeButton.addTarget(self, action: #selector(handleLikeButton), for: .touchUpInside) likeButton.addTarget(self, action: #selector(handleLikeButton), for: .touchUpInside)
likeButton.setImage(UIImage(named: "like_outline"), for: .normal) likeButton.setImage(UIImage(named: "like_outline"), for: .normal)
likeButton.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor likeButton.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
...@@ -239,6 +274,7 @@ private extension ShortsItemCell { ...@@ -239,6 +274,7 @@ private extension ShortsItemCell {
likeButton.layer.shadowOpacity = 0.3 likeButton.layer.shadowOpacity = 0.3
contentView.addSubview(likeButton) contentView.addSubview(likeButton)
ctaButton.isHidden = true
ctaButton.addTarget(self, action: #selector(handleCtaButton), for: .touchUpInside) ctaButton.addTarget(self, action: #selector(handleCtaButton), for: .touchUpInside)
ctaButton.backgroundColor = .clear ctaButton.backgroundColor = .clear
ctaButton.layer.borderColor = UIColor(hex: 0xDDDDDD).cgColor ctaButton.layer.borderColor = UIColor(hex: 0xDDDDDD).cgColor
......
...@@ -266,10 +266,10 @@ extension ShortsViewController: UITableViewDataSource { ...@@ -266,10 +266,10 @@ extension ShortsViewController: UITableViewDataSource {
//MARK:- UITableView Delegate //MARK:- UITableView Delegate
extension ShortsViewController: UITableViewDelegate { extension ShortsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let image = viewModel.shorts[indexPath.row].getOverlayImage(for: tableView.frame.width) { // if let image = viewModel.shorts[indexPath.row].getOverlayImage(for: tableView.frame.width) {
let aspectRatio = CGFloat(image.width) / CGFloat(image.height) // let aspectRatio = CGFloat(image.width) / CGFloat(image.height)
return tableView.bounds.width / aspectRatio // return tableView.bounds.width / aspectRatio
} // }
return tableView.bounds.height return tableView.bounds.height
} }
...@@ -355,13 +355,27 @@ extension ShortsViewController: ViewModelDelegate { ...@@ -355,13 +355,27 @@ extension ShortsViewController: ViewModelDelegate {
//MARK:- ShortsItemCell Delegate //MARK:- ShortsItemCell Delegate
extension ShortsViewController: ShortsItemCellDelegate { extension ShortsViewController: ShortsItemCellDelegate {
private func bottomPart(of sourceImage: UIImage) -> UIImage? {
let dy: CGFloat = 10 //10pt from bottom
let area = CGRect(x: 0,
y: sourceImage.size.height - dy,
width: sourceImage.size.width,
height: dy)
guard let crop = sourceImage.cgImage?.cropping(to: area) else {
return nil
}
return UIImage(cgImage: crop, scale: UIScreen.main.scale, orientation: .up)
}
func averageColor(forImage image: UIImage, identifier: String) -> UIColor? { func averageColor(forImage image: UIImage, identifier: String) -> UIColor? {
if let cachedColor = self.averageColorCache[identifier] { if let cachedColor = self.averageColorCache[identifier] {
return cachedColor return cachedColor
} }
else { else {
if let color = image.averageColor { if let bottomPartImage = self.bottomPart(of: image),
self.averageColorCache[identifier] = color.isDarkColor ? color : color.darker(by: 30) let color = bottomPartImage.averageColor {
self.averageColorCache[identifier] = color
return color return color
} }
else { else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment