Commit df903fd7 authored by Bruno Muniz's avatar Bruno Muniz 🍎
Browse files

Using CollectionView library

parent db586177
......@@ -39,16 +39,14 @@ public final class ContactListController: UIViewController {
screenView.collectionView.delegate = self
screenView.collectionView.dataSource = dataSource
screenView.collectionView.tintColor = Asset.neutralDark.color
CellFactory.contactListCellFactory.register(in: screenView.collectionView)
CellFactory.avatarCellFactory()
.register(in: screenView.collectionView)
dataSource = IndexedContactList(
collectionView: screenView.collectionView
) { collectionView, indexPath, contact in
CellFactory.contactListCellFactory.build(
for: contact,
in: collectionView,
at: indexPath
)
CellFactory.avatarCellFactory().build(for: contact, in: collectionView, at: indexPath)
}
}
......
......@@ -66,29 +66,26 @@ public final class CreateGroupController: UIViewController {
}
private func setupCollectionViews() {
CellFactory.createGroupHeroCellFactory
.register(in: screenView.topCollectionView)
CellFactory.createGroupListCellFactory
CellFactory.avatarCellFactory()
.register(in: screenView.bottomCollectionView)
CellFactory.createGroupHeroCellFactory(action: { _ in })
.register(in: screenView.topCollectionView)
topCollectionDataSource = UICollectionViewDiffableDataSource<Int, Contact>(
collectionView: screenView.topCollectionView
) { collectionView, indexPath, contact in
CellFactory.createGroupHeroCellFactory.build(
for: contact,
in: collectionView,
at: indexPath
)
) { [weak self] collectionView, indexPath, contact in
CellFactory.createGroupHeroCellFactory(
action: {
guard let self = self else { return }
self.viewModel.didSelect(contact: $0)
}
).build(for: contact, in: collectionView, at: indexPath)
}
bottomCollectionDataSource = UICollectionViewDiffableDataSource<Int, Contact>(
collectionView: screenView.bottomCollectionView
) { collectionView, indexPath, contact in
CellFactory.createGroupListCellFactory.build(
for: contact,
in: collectionView,
at: indexPath
)
CellFactory.avatarCellFactory().build(for: contact, in: collectionView, at: indexPath)
}
screenView.bottomCollectionView.delegate = self
......
......@@ -3,71 +3,37 @@ 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
})
)
static func avatarCellFactory(showSeparator: Bool = true) -> Self {
.init(
register: .init { $0.register(AvatarCell.self) },
build: .init { contact, collectionView, indexPath in
let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
cell.set(
image: contact.photo,
h1Text: (contact.nickname ?? contact.username) ?? "",
showSeparator: showSeparator
)
return cell
}
)
}
static func createGroupHeroCellFactory(action: @escaping (Contact) -> Void) -> Self {
.init(
register: .init { $0.register(CreateGroupHeroCollectionCell.self) },
build: .init { contact, collectionView, indexPath in
let cell: CreateGroupHeroCollectionCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
cell.setup(
title: (contact.nickname ?? contact.username) ?? "",
image: contact.photo,
action: { action(contact) }
)
return cell
}
)
}
}
......@@ -2,6 +2,7 @@ import Theme
import UIKit
import Shared
import Combine
import CollectionView
import DependencyInjection
public final class CountryListController: UIViewController {
......@@ -71,19 +72,13 @@ public final class CountryListController: UIViewController {
}
private func setupCollectionView() {
screenView.collectionView.register(CountryListCell.self)
CellFactory.countryListCellFactory
.register(in: screenView.collectionView)
dataSource = UICollectionViewDiffableDataSource<Int, Country>(
collectionView: screenView.collectionView
) { collectionView, indexPath, country in
let cell: CountryListCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
cell.set(
flag: country.flag,
name: country.name,
prefix: country.prefix
)
return cell
CellFactory.countryListCellFactory.build(for: country, in: collectionView, at: indexPath)
}
screenView.collectionView.delegate = self
......@@ -103,3 +98,20 @@ extension CountryListController: UICollectionViewDelegate {
}
}
}
extension CellFactory where Model == Country {
static let countryListCellFactory = Self.init(
register: .init { $0.register(CountryListCell.self) },
build: .init { country, collectionView, indexPath in
let cell: CountryListCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
cell.set(
flag: country.flag,
name: country.name,
prefix: country.prefix
)
return cell
}
)
}
......@@ -6,6 +6,7 @@ import XXModels
import Defaults
import Countries
import DrawerFeature
import CollectionView
import DependencyInjection
final class SearchLeftController: UIViewController {
......@@ -44,71 +45,20 @@ final class SearchLeftController: UIViewController {
private func setupCollectionView() {
screenView.collectionView.delegate = self
screenView.collectionView.dataSource = dataSource
screenView.collectionView.register(AvatarCell.self)
screenView.collectionView.registerSectionHeader(SearchLeftSectionHeader.self)
dataSource = UICollectionViewDiffableDataSource<SearchSection, SearchItem>(
collectionView: screenView.collectionView
) { collectionView, indexPath, item in
let contact: Contact
let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
let h1Text: String
var h2Text: String?
switch item {
case .stranger(let stranger):
contact = stranger
h1Text = stranger.username ?? ""
if stranger.authStatus == .requested {
h2Text = "Request pending"
} else if stranger.authStatus == .requestFailed {
h2Text = "Request failed"
}
case .connection(let connection):
contact = connection
h1Text = (connection.nickname ?? contact.username) ?? ""
if connection.nickname != nil {
h2Text = contact.username ?? ""
}
}
var action: AvatarCell.Action?
if contact.authStatus == .requested {
action = .init(
title: Localized.Requests.Cell.requested,
color: Asset.brandPrimary.color,
image: Asset.requestsResend.image,
action: { [weak self] in
guard let self = self else { return }
CellFactory.avatarCellFactory(resend: { _ in })
.register(in: screenView.collectionView)
self.viewModel.didTapResend(contact: contact)
cell.update(action: .init(
title: Localized.Requests.Cell.resent,
color: Asset.neutralWeak.color,
image: Asset.requestsResent.image,
action: {}
))
}
)
}
cell.set(
image: contact.photo,
h1Text: h1Text,
h2Text: h2Text,
h3Text: contact.email,
h4Text: contact.phone,
showSeparator: false,
action: action
)
screenView.collectionView
.registerSectionHeader(SearchLeftSectionHeader.self)
return cell
dataSource = UICollectionViewDiffableDataSource<SearchSection, SearchItem>(
collectionView: screenView.collectionView
) { collectionView, indexPath, searchItem in
CellFactory.avatarCellFactory { [weak self] in
guard let self = self else { return }
self.viewModel.didTapResend(contact: $0)
}.build(for: searchItem, in: collectionView, at: indexPath)
}
dataSource.supplementaryViewProvider = { [weak self] collectionView, kind, indexPath in
......@@ -458,3 +408,70 @@ extension SearchLeftController: UICollectionViewDelegate {
presentRequestDrawer(forContact: contact)
}
}
extension CellFactory where Model == SearchItem {
static func avatarCellFactory(resend: @escaping (Contact) -> Void) -> Self {
.init(
register: .init { $0.register(AvatarCell.self) },
build: .init { searchItem, collectionView, indexPath in
let contact: Contact
let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
let h1Text: String
var h2Text: String?
switch searchItem {
case .stranger(let stranger):
contact = stranger
h1Text = stranger.username ?? ""
if stranger.authStatus == .requested {
h2Text = "Request pending"
} else if stranger.authStatus == .requestFailed {
h2Text = "Request failed"
}
case .connection(let connection):
contact = connection
h1Text = (connection.nickname ?? contact.username) ?? ""
if connection.nickname != nil {
h2Text = contact.username ?? ""
}
}
var action: AvatarCell.Action?
if contact.authStatus == .requested {
action = .init(
title: Localized.Requests.Cell.requested,
color: Asset.brandPrimary.color,
image: Asset.requestsResend.image,
action: {
resend(contact)
cell.update(action: .init(
title: Localized.Requests.Cell.resent,
color: Asset.neutralWeak.color,
image: Asset.requestsResent.image,
action: {}
))
}
)
}
cell.set(
image: contact.photo,
h1Text: h1Text,
h2Text: h2Text,
h3Text: contact.email,
h4Text: contact.phone,
showSeparator: false,
action: action
)
return cell
}
)
}
}
Supports Markdown
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