Skip to content
Snippets Groups Projects
Commit db586177 authored by Bruno Muniz's avatar Bruno Muniz :apple:
Browse files

Working on using collectionview library

parent 790bbfab
Branches
No related tags found
1 merge request!58Migrating lists to compositional layout
...@@ -678,6 +678,7 @@ let package = Package( ...@@ -678,6 +678,7 @@ let package = Package(
"Integration", "Integration",
"Presentation", "Presentation",
"ContactFeature", "ContactFeature",
"CollectionView",
"DependencyInjection", "DependencyInjection",
.product( .product(
name: "DifferenceKit", name: "DifferenceKit",
......
...@@ -3,6 +3,7 @@ import Theme ...@@ -3,6 +3,7 @@ import Theme
import Shared import Shared
import Combine import Combine
import XXModels import XXModels
import CollectionView
import DependencyInjection import DependencyInjection
public final class ContactListController: UIViewController { public final class ContactListController: UIViewController {
...@@ -36,23 +37,18 @@ public final class ContactListController: UIViewController { ...@@ -36,23 +37,18 @@ public final class ContactListController: UIViewController {
private func setupCollectionView() { private func setupCollectionView() {
screenView.collectionView.delegate = self screenView.collectionView.delegate = self
screenView.collectionView.register(AvatarCell.self)
screenView.collectionView.dataSource = dataSource screenView.collectionView.dataSource = dataSource
screenView.collectionView.tintColor = Asset.neutralDark.color screenView.collectionView.tintColor = Asset.neutralDark.color
CellFactory.contactListCellFactory.register(in: screenView.collectionView)
dataSource = IndexedContactList( dataSource = IndexedContactList(
collectionView: screenView.collectionView collectionView: screenView.collectionView
) { collectionView, indexPath, contact in ) { collectionView, indexPath, contact in
let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) CellFactory.contactListCellFactory.build(
let name = (contact.nickname ?? contact.username) ?? "Fetching username..." for: contact,
in: collectionView,
cell.set( at: indexPath
image: contact.photo,
h1Text: name,
showSeparator: false
) )
return cell
} }
} }
......
...@@ -4,6 +4,7 @@ import Models ...@@ -4,6 +4,7 @@ import Models
import Shared import Shared
import Combine import Combine
import XXModels import XXModels
import CollectionView
import DependencyInjection import DependencyInjection
public final class CreateGroupController: UIViewController { public final class CreateGroupController: UIViewController {
...@@ -65,36 +66,29 @@ public final class CreateGroupController: UIViewController { ...@@ -65,36 +66,29 @@ public final class CreateGroupController: UIViewController {
} }
private func setupCollectionViews() { private func setupCollectionViews() {
screenView.bottomCollectionView.register(AvatarCell.self) CellFactory.createGroupHeroCellFactory
screenView.topCollectionView.register(CreateGroupCollectionCell.self) .register(in: screenView.topCollectionView)
CellFactory.createGroupListCellFactory
.register(in: screenView.bottomCollectionView)
topCollectionDataSource = UICollectionViewDiffableDataSource<Int, Contact>( topCollectionDataSource = UICollectionViewDiffableDataSource<Int, Contact>(
collectionView: screenView.topCollectionView collectionView: screenView.topCollectionView
) { [weak viewModel] collectionView, indexPath, contact in ) { collectionView, indexPath, contact in
let cell: CreateGroupCollectionCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) CellFactory.createGroupHeroCellFactory.build(
for: contact,
let title = (contact.nickname ?? contact.username) ?? "" in: collectionView,
cell.setup(title: title, image: contact.photo) at: indexPath
cell.didTapRemove = { viewModel?.didSelect(contact: contact) } )
return cell
} }
bottomCollectionDataSource = UICollectionViewDiffableDataSource<Int, Contact>( bottomCollectionDataSource = UICollectionViewDiffableDataSource<Int, Contact>(
collectionView: screenView.bottomCollectionView collectionView: screenView.bottomCollectionView
) { [weak self] collectionView, indexPath, contact in ) { collectionView, indexPath, contact in
let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) CellFactory.createGroupListCellFactory.build(
let title = (contact.nickname ?? contact.username) ?? "" for: contact,
in: collectionView,
cell.set(image: contact.photo, h1Text: title) at: indexPath
)
if let selectedElements = self?.selectedElements, selectedElements.contains(contact) {
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
} else {
collectionView.deselectItem(at: indexPath, animated: true)
}
return cell
} }
screenView.bottomCollectionView.delegate = self screenView.bottomCollectionView.delegate = self
......
import Shared
import XXModels
import CollectionView
extension CellFactory where Model == Contact {
static let contactListCellFactory = CellFactory<Contact>(
register: .init(
register: { $0.register(AvatarCell.self) }
),
build: .init(build: { contact, collectionView, indexPath in
let cell: AvatarCell = collectionView.dequeueReusableCell(
forIndexPath: indexPath
)
cell.set(
image: contact.photo,
h1Text: (contact.nickname ?? contact.username) ?? "",
showSeparator: false
)
return cell
})
)
static let createGroupHeroCellFactory = CellFactory<Contact>(
register: .init(
register: { $0.register(CreateGroupHeroCollectionCell.self) }
),
build: .init(build: { contact, collectionView, indexPath in
let name = (contact.nickname ?? contact.username) ?? ""
let cell: CreateGroupHeroCollectionCell = collectionView.dequeueReusableCell(
forIndexPath: indexPath
)
cell.setup(
title: name,
image: contact.photo,
action: {
// viewModel?.didSelect(contact: contact)
}
)
return cell
})
)
static let createGroupListCellFactory = CellFactory<Contact>(
register: .init(
register: { $0.register(AvatarCell.self) }
),
build: .init(build: { contact, collectionView, indexPath in
let name = (contact.nickname ?? contact.username) ?? ""
let cell: AvatarCell = collectionView.dequeueReusableCell(
forIndexPath: indexPath
)
cell.set(
image: contact.photo,
h1Text: name
)
//if let selectedElements = self?.selectedElements, selectedElements.contains(contact) {
// collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
//} else {
// collectionView.deselectItem(at: indexPath, animated: true)
//}
return cell
})
)
}
...@@ -2,14 +2,13 @@ import UIKit ...@@ -2,14 +2,13 @@ import UIKit
import Shared import Shared
import Combine import Combine
final class CreateGroupCollectionCell: UICollectionViewCell { final class CreateGroupHeroCollectionCell: UICollectionViewCell {
let titleLabel = UILabel() private let upperView = UIView()
let removeButton = UIButton() private let titleLabel = UILabel()
let upperView = UIView() private let avatarView = AvatarView()
let avatarView = AvatarView() private let removeButton = UIButton()
private var didTapAction: (() -> Void)?
var didTapRemove: (() -> Void)? private var cancellables = Set<AnyCancellable>()
var cancellables = Set<AnyCancellable>()
override init(frame: CGRect) { override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
...@@ -29,6 +28,43 @@ final class CreateGroupCollectionCell: UICollectionViewCell { ...@@ -29,6 +28,43 @@ final class CreateGroupCollectionCell: UICollectionViewCell {
contentView.addSubview(upperView) contentView.addSubview(upperView)
contentView.addSubview(removeButton) contentView.addSubview(removeButton)
setupConstraints()
}
required init?(coder: NSCoder) { nil }
override func prepareForReuse() {
super.prepareForReuse()
didTapAction = nil
titleLabel.text = nil
avatarView.prepareForReuse()
cancellables.removeAll()
}
func setup(
title: String,
image: Data?,
action: @escaping () -> Void
) {
avatarView.setupProfile(
title: title,
image: image,
size: .large
)
titleLabel.text = title
didTapAction = action
cancellables.removeAll()
removeButton
.publisher(for: .touchUpInside)
.sink { [unowned self] in didTapAction?() }
.store(in: &cancellables)
}
private func setupConstraints() {
upperView.snp.makeConstraints { upperView.snp.makeConstraints {
$0.top.equalToSuperview() $0.top.equalToSuperview()
$0.left.equalToSuperview() $0.left.equalToSuperview()
...@@ -58,23 +94,4 @@ final class CreateGroupCollectionCell: UICollectionViewCell { ...@@ -58,23 +94,4 @@ final class CreateGroupCollectionCell: UICollectionViewCell {
$0.bottom.equalToSuperview() $0.bottom.equalToSuperview()
} }
} }
required init?(coder: NSCoder) { nil }
override func prepareForReuse() {
super.prepareForReuse()
titleLabel.text = nil
avatarView.prepareForReuse()
cancellables.removeAll()
}
func setup(title: String, image: Data?) {
titleLabel.text = title
avatarView.setupProfile(title: title, image: image, size: .large)
cancellables.removeAll()
removeButton.publisher(for: .touchUpInside)
.sink { [unowned self] in didTapRemove?() }
.store(in: &cancellables)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment