Skip to content

Commit

Permalink
feat(List): Add mising functionality to List
Browse files Browse the repository at this point in the history
BREAKING CHANGE: List asset .image has been renamed to .custom. This custom case has as parameter .image(UIImage) or .url(URL)

* https://jira.tid.es/browse/IOS-9421: Add support for url and image gesture

---------

Co-authored-by: WanaldinoTelefonica <WanaldinoTelefonica@users.noreply.github.com>
  • Loading branch information
WanaldinoTelefonica and WanaldinoTelefonica committed Jan 9, 2024
1 parent 9058967 commit 5a81b74
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class UICatalogListsViewController: UITableViewController {
cell.segmentedControl.insertSegment(withTitle: "Large Icon", at: 1, animated: false)
cell.segmentedControl.insertSegment(withTitle: "Small Icon", at: 2, animated: false)
cell.segmentedControl.insertSegment(withTitle: "Image", at: 3, animated: false)
cell.segmentedControl.insertSegment(withTitle: "URL", at: 4, animated: false)
cell.segmentedControl.selectedSegmentIndex = 0
return cell
}()
Expand Down Expand Up @@ -163,7 +164,9 @@ extension UICatalogListsViewController {
case 2:
sampleVC.assetType = .smallIcon(.imageIcon)
case 3:
sampleVC.assetType = .image(.netflixLogo, size: CGSize(width: 140, height: 80))
sampleVC.assetType = .custom(.image(.netflixLogo), size: CGSize(width: 140, height: 80))
case 4:
sampleVC.assetType = .custom(.url(URL(string: "https://www.svgrepo.com/show/19461/url-link.svg")!), size: CGSize(width: 64, height: 64))
default:
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ private enum ImageSize {
static let small: CGFloat = 24
}

public protocol ListCellContentAssetDelegate: AnyObject {
func listCellContentDidTapOnAsset()
}

class CellLeftSectionView: UIStackView {
private lazy var heightConstraint = containerView.heightAnchor.constraint(equalToConstant: assetType.viewSize.height)
private lazy var widthConstraint = containerView.widthAnchor.constraint(equalToConstant: assetType.viewSize.width)
Expand All @@ -25,16 +29,23 @@ class CellLeftSectionView: UIStackView {

private let imageView = IntrinsictImageView()

weak var delegate: ListCellContentAssetDelegate? {
didSet {
imageView.isUserInteractionEnabled = delegate != nil
}
}

var assetType: ListCellContentView.CellAssetType = .none {
didSet {
heightConstraint.constant = assetType.viewSize.height
widthConstraint.constant = assetType.viewSize.width
imageView.intrinsicWidth = assetType.assetSize.width
imageView.intrinsicHeight = assetType.assetSize.height
imageView.image = assetType.image
imageView.contentMode = assetType.contentMode
containerView.makeRounded(cornerRadius: assetType.cornerRadius)
containerView.backgroundColor = assetType.backgroundColor

load()
}
}

Expand All @@ -58,6 +69,26 @@ class CellLeftSectionView: UIStackView {
fatalError("init(coder:) has not been implemented")
}

func load() {
switch assetType {
case .none:
imageView.image = nil
case .custom(let asset, _):
load(asset: asset)
case .smallIcon(let image), .largeIcon(let image, _):
imageView.image = image
}
}

func load(asset: ListCellContentView.CellAssetType.Asset) {
switch asset {
case .image(let image):
imageView.image = image
case .url(let url):
imageView.load(url: url)
}
}

func centerAlignment() {
alignment = .center
isLayoutMarginsRelativeArrangement = false
Expand All @@ -80,6 +111,15 @@ private extension CellLeftSectionView {
func commonInit() {
addArrangedSubview(containerView)
NSLayoutConstraint.activate([heightConstraint, widthConstraint])

let gesture = UITapGestureRecognizer(target: self, action: #selector(didTapAsset))
imageView.addGestureRecognizer(gesture)
imageView.isUserInteractionEnabled = delegate != nil
}

@objc
func didTapAsset() {
delegate?.listCellContentDidTapOnAsset()
}
}

Expand All @@ -88,7 +128,7 @@ private extension ListCellContentView.CellAssetType {
switch self {
case .none:
return CGSize.zero
case let .image(_, size):
case let .custom(_, size):
if let size = size { return size }
return CGSize(width: ImageSize.large, height: ImageSize.large)
case .smallIcon, .largeIcon:
Expand All @@ -100,7 +140,7 @@ private extension ListCellContentView.CellAssetType {
switch self {
case .none:
return CGSize.zero
case let .image(_, size):
case let .custom(_, size):
if let size = size { return size }
return CGSize(width: ImageSize.large, height: ImageSize.large)

Expand All @@ -116,7 +156,7 @@ private extension ListCellContentView.CellAssetType {
switch self {
case .none, .smallIcon:
return 0
case .image(_, let size):
case .custom(_, let size):
return (size != nil ? MisticaConfig.currentCornerRadius.container : viewSize.height / 2)
case .largeIcon:
return viewSize.height / 2
Expand All @@ -125,7 +165,10 @@ private extension ListCellContentView.CellAssetType {

var image: UIImage? {
switch self {
case .smallIcon(let image), .largeIcon(let image, _), .image(let image, _):
case .custom(let asset, _):
guard case let .image(image) = asset else { return nil }
return image
case .smallIcon(let image), .largeIcon(let image, _):
return image
case .none:
return nil
Expand All @@ -134,7 +177,7 @@ private extension ListCellContentView.CellAssetType {

var contentMode: UIView.ContentMode {
switch self {
case .image:
case .custom:
return .scaleAspectFill
case .none, .smallIcon, .largeIcon:
return .scaleAspectFit
Expand All @@ -145,7 +188,7 @@ private extension ListCellContentView.CellAssetType {
switch self {
case .largeIcon(_, let backgroundColor):
return backgroundColor
case .none, .smallIcon, .image:
case .none, .smallIcon, .custom:
return .clear
}
}
Expand Down
16 changes: 15 additions & 1 deletion Sources/Mistica/Components/Lists/ListCellContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ open class ListCellContentView: UIView {

@frozen
public enum CellAssetType: Equatable {
public enum Asset: Equatable {
case image(UIImage)
case url(URL)
}

case none
case image(UIImage, size: CGSize? = nil)
case custom(Asset, size: CGSize? = nil)
case smallIcon(UIImage)
case largeIcon(UIImage, backgroundColor: UIColor)
}
Expand Down Expand Up @@ -195,6 +200,15 @@ open class ListCellContentView: UIView {
}
}

public var assetDelegate: ListCellContentAssetDelegate? {
get {
leftSection.delegate
}
set {
leftSection.delegate = newValue
}
}

public var controlView: UIView? {
didSet {
oldValue?.removeFromSuperview()
Expand Down
2 changes: 1 addition & 1 deletion Tests/MisticaTests/UI/CarouselTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class CarouselTests: XCTestCase {
func testCellWithTitleAndImage() {
let carouselTestsViewController = makeCarouselTestsViewController(
title: AnyValues.title,
assetType: .image(AnyValues.image)
assetType: .custom(.image(AnyValues.image))
)

assertSnapshot(
Expand Down
38 changes: 19 additions & 19 deletions Tests/MisticaTests/UI/ListsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ListsTests: XCTestCase {
// MARK: - Default config

func testCellDefaultConfigAndImage() {
let listTestsViewController = makeListTestsViewController(assetType: .image(AnyValues.image))
let listTestsViewController = makeListTestsViewController(assetType: .custom(.image(AnyValues.image)))

assertSnapshot(
matching: listTestsViewController,
Expand All @@ -32,7 +32,7 @@ final class ListsTests: XCTestCase {
}

func testCellDefaultConfigAndImageWithCustomSize() {
let listTestsViewController = makeListTestsViewController(assetType: .image(AnyValues.image, size: CGSize(width: 100, height: 40)))
let listTestsViewController = makeListTestsViewController(assetType: .custom(.image(AnyValues.image), size: CGSize(width: 100, height: 40)))

assertSnapshot(
matching: listTestsViewController,
Expand Down Expand Up @@ -84,7 +84,7 @@ final class ListsTests: XCTestCase {
func testCellWithTitleAndImage() {
let listTestsViewController = makeListTestsViewController(
title: AnyValues.title,
assetType: .image(AnyValues.image)
assetType: .custom(.image(AnyValues.image))
)

assertSnapshot(
Expand All @@ -96,7 +96,7 @@ final class ListsTests: XCTestCase {
func testCellWithTitleAndImageWithCustomSize() {
let listTestsViewController = makeListTestsViewController(
title: AnyValues.title,
assetType: .image(AnyValues.image, size: CGSize(width: 100, height: 40))
assetType: .custom(.image(AnyValues.image), size: CGSize(width: 100, height: 40))
)

assertSnapshot(
Expand Down Expand Up @@ -160,7 +160,7 @@ final class ListsTests: XCTestCase {
let listTestsViewController = makeListTestsViewController(
title: AnyValues.title,
subtitle: AnyValues.subtitle,
assetType: .image(AnyValues.image)
assetType: .custom(.image(AnyValues.image))
)

assertSnapshot(
Expand All @@ -173,7 +173,7 @@ final class ListsTests: XCTestCase {
let listTestsViewController = makeListTestsViewController(
title: AnyValues.title,
subtitle: AnyValues.subtitle,
assetType: .image(AnyValues.image, size: CGSize(width: 100, height: 40))
assetType: .custom(.image(AnyValues.image), size: CGSize(width: 100, height: 40))
)

assertSnapshot(
Expand Down Expand Up @@ -240,7 +240,7 @@ final class ListsTests: XCTestCase {
let listTestsViewController = makeListTestsViewController(
title: AnyValues.title,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image)
assetType: .custom(.image(AnyValues.image))
)

assertSnapshot(
Expand All @@ -253,7 +253,7 @@ final class ListsTests: XCTestCase {
let listTestsViewController = makeListTestsViewController(
title: AnyValues.title,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image, size: CGSize(width: 100, height: 40))
assetType: .custom(.image(AnyValues.image), size: CGSize(width: 100, height: 40))
)

assertSnapshot(
Expand Down Expand Up @@ -322,7 +322,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image)
assetType: .custom(.image(AnyValues.image))
)

assertSnapshot(
Expand All @@ -336,7 +336,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image, size: CGSize(width: 100, height: 40))
assetType: .custom(.image(AnyValues.image), size: CGSize(width: 100, height: 40))
)

assertSnapshot(
Expand Down Expand Up @@ -421,7 +421,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
showHeadline: true
)

Expand All @@ -436,7 +436,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image, size: CGSize(width: 100, height: 40)),
assetType: .custom(.image(AnyValues.image), size: CGSize(width: 100, height: 40)),
showHeadline: true
)

Expand All @@ -451,7 +451,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
customControl: .custom(makeCustomControlView),
showHeadline: true
)
Expand All @@ -467,7 +467,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image, size: CGSize(width: 60, height: 20)),
assetType: .custom(.image(AnyValues.image), size: CGSize(width: 60, height: 20)),
customControl: .custom(makeCustomControlView),
showHeadline: true
)
Expand Down Expand Up @@ -625,7 +625,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.titleMultiline,
subtitle: AnyValues.subtitleMultiline,
detailText: AnyValues.detailTextMultiline,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
customControl: .navigation(makeNavigationPresetViewWithoutBagde),
showHeadline: true
)
Expand All @@ -641,7 +641,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitleMultiline,
detailText: AnyValues.detailTextMultiline,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
customControl: .navigation(makeNavigationPresetViewWithoutBagde),
showHeadline: true,
numberOfRows: 3
Expand Down Expand Up @@ -703,7 +703,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitleMultiline,
detailText: AnyValues.detailTextMultiline,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
customControl: .navigation(makeNavigationPresetViewWithoutBagde),
showHeadline: true,
cellLayoutStyle: .boxed,
Expand Down Expand Up @@ -766,7 +766,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitleMultiline,
detailText: AnyValues.detailTextMultiline,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
customControl: .navigation(makeNavigationPresetViewWithoutBagde),
showHeadline: true,
cellLayoutStyle: .boxedInverse,
Expand All @@ -789,7 +789,7 @@ final class ListsTests: XCTestCase {
title: AnyValues.title,
subtitle: AnyValues.subtitle,
detailText: AnyValues.detailText,
assetType: .image(AnyValues.image),
assetType: .custom(.image(AnyValues.image)),
customControl: .custom(makeCustomControlView),
showHeadline: true,
cellLayoutStyle: .fullWidth
Expand Down

0 comments on commit 5a81b74

Please sign in to comment.