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

Working on refactoring group creation

parent f13e0ba8
No related branches found
No related tags found
1 merge request!58Migrating lists to compositional layout
...@@ -15,12 +15,12 @@ public final class CreateGroupController: UIViewController { ...@@ -15,12 +15,12 @@ public final class CreateGroupController: UIViewController {
lazy private var screenView = CreateGroupView() lazy private var screenView = CreateGroupView()
private var selectedElements = [Contact]() { private var selectedElements = [Contact]() {
didSet { screenView.tableView.reloadData() } didSet { screenView.bottomCollectionView.reloadData() }
} }
private let viewModel = CreateGroupViewModel() private let viewModel = CreateGroupViewModel()
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private var tableDataSource: UITableViewDiffableDataSource<SectionId, Contact>! private var topCollectionDataSource: UICollectionViewDiffableDataSource<SectionId, Contact>!
private var collectionDataSource: UICollectionViewDiffableDataSource<SectionId, Contact>! private var bottomCollectionDataSource: UICollectionViewDiffableDataSource<SectionId, Contact>!
private var count = 0 { private var count = 0 {
didSet { didSet {
...@@ -43,7 +43,7 @@ public final class CreateGroupController: UIViewController { ...@@ -43,7 +43,7 @@ public final class CreateGroupController: UIViewController {
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
setupNavigationBar() setupNavigationBar()
setupTableAndCollection() setupCollectionViews()
setupBindings() setupBindings()
} }
...@@ -64,13 +64,12 @@ public final class CreateGroupController: UIViewController { ...@@ -64,13 +64,12 @@ public final class CreateGroupController: UIViewController {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: createButton) navigationItem.rightBarButtonItem = UIBarButtonItem(customView: createButton)
} }
private func setupTableAndCollection() { private func setupCollectionViews() {
screenView.tableView.rowHeight = 64.0 screenView.bottomCollectionView.register(AvatarCell.self)
screenView.tableView.register(AvatarCell.self) screenView.topCollectionView.register(CreateGroupCollectionCell.self)
screenView.collectionView.register(CreateGroupCollectionCell.self)
collectionDataSource = UICollectionViewDiffableDataSource<SectionId, Contact>( topCollectionDataSource = UICollectionViewDiffableDataSource<SectionId, Contact>(
collectionView: screenView.collectionView collectionView: screenView.topCollectionView
) { [weak viewModel] collectionView, indexPath, contact in ) { [weak viewModel] collectionView, indexPath, contact in
let cell: CreateGroupCollectionCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: CreateGroupCollectionCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -81,26 +80,28 @@ public final class CreateGroupController: UIViewController { ...@@ -81,26 +80,28 @@ public final class CreateGroupController: UIViewController {
return cell return cell
} }
tableDataSource = DiffEditableDataSource<SectionId, Contact>( bottomCollectionDataSource = UICollectionViewDiffableDataSource<SectionId, Contact>(
tableView: screenView.tableView collectionView: screenView.bottomCollectionView
) { [weak self] tableView, indexPath, contact in ) { [weak self] collectionView, indexPath, contact in
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath, ofType: AvatarCell.self) let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
let title = (contact.nickname ?? contact.username) ?? "" let title = (contact.nickname ?? contact.username) ?? ""
cell.set(image: contact.photo, h1Text: title) cell.set(image: contact.photo, h1Text: title)
if let selectedElements = self?.selectedElements, selectedElements.contains(contact) { if let selectedElements = self?.selectedElements, selectedElements.contains(contact) {
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none) collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
} else { } else {
tableView.deselectRow(at: indexPath, animated: true) collectionView.deselectItem(at: indexPath, animated: true)
} }
return cell return cell
} }
screenView.tableView.delegate = self screenView.bottomCollectionView.delegate = self
screenView.tableView.dataSource = tableDataSource screenView.topCollectionView.dataSource = topCollectionDataSource
screenView.collectionView.dataSource = collectionDataSource screenView.bottomCollectionView.tintColor = Asset.brandPrimary.color
screenView.bottomCollectionView.dataSource = bottomCollectionDataSource
screenView.bottomCollectionView.allowsMultipleSelectionDuringEditing = true
} }
private func setupBindings() { private func setupBindings() {
...@@ -114,7 +115,7 @@ public final class CreateGroupController: UIViewController { ...@@ -114,7 +115,7 @@ public final class CreateGroupController: UIViewController {
selected selected
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [unowned self] in .sink { [unowned self] in
screenView.collectionView.isHidden = $0.count < 1 screenView.topCollectionView.isHidden = $0.count < 1
count = $0.count count = $0.count
selectedElements = $0 selectedElements = $0
...@@ -128,11 +129,11 @@ public final class CreateGroupController: UIViewController { ...@@ -128,11 +129,11 @@ public final class CreateGroupController: UIViewController {
return snapshot return snapshot
} }
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [unowned self] in collectionDataSource.apply($0) } .sink { [unowned self] in topCollectionDataSource.apply($0) }
.store(in: &cancellables) .store(in: &cancellables)
viewModel.contacts viewModel.contacts
.map { contacts in .map { contacts -> NSDiffableDataSourceSnapshot<SectionId, Contact> in
var snapshot = NSDiffableDataSourceSnapshot<SectionId, Contact>() var snapshot = NSDiffableDataSourceSnapshot<SectionId, Contact>()
let sections = [SectionId()] let sections = [SectionId()]
snapshot.appendSections(sections) snapshot.appendSections(sections)
...@@ -141,7 +142,8 @@ public final class CreateGroupController: UIViewController { ...@@ -141,7 +142,8 @@ public final class CreateGroupController: UIViewController {
} }
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [unowned self] in .sink { [unowned self] in
tableDataSource.apply($0, animatingDifferences: tableDataSource.snapshot().numberOfItems > 0) let animating = bottomCollectionDataSource.snapshot().numberOfItems > 0
bottomCollectionDataSource.apply($0, animatingDifferences: animating)
}.store(in: &cancellables) }.store(in: &cancellables)
screenView.searchComponent.textPublisher screenView.searchComponent.textPublisher
...@@ -161,7 +163,7 @@ public final class CreateGroupController: UIViewController { ...@@ -161,7 +163,7 @@ public final class CreateGroupController: UIViewController {
coordinator.toGroupDrawer( coordinator.toGroupDrawer(
with: count + 1, with: count + 1,
from: self, { (name, welcome) in from: self, { (name, welcome) in
viewModel.create(name: name, welcome: welcome, members: selectedElements) self.viewModel.create(name: name, welcome: welcome, members: self.selectedElements)
} }
) )
}.store(in: &cancellables) }.store(in: &cancellables)
...@@ -172,15 +174,15 @@ public final class CreateGroupController: UIViewController { ...@@ -172,15 +174,15 @@ public final class CreateGroupController: UIViewController {
} }
} }
extension CreateGroupController: UITableViewDelegate { extension CreateGroupController: UICollectionViewDelegate {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let contact = tableDataSource.itemIdentifier(for: indexPath) { if let contact = bottomCollectionDataSource.itemIdentifier(for: indexPath) {
viewModel.didSelect(contact: contact) viewModel.didSelect(contact: contact)
} }
} }
public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let contact = tableDataSource.itemIdentifier(for: indexPath) { if let contact = bottomCollectionDataSource.itemIdentifier(for: indexPath) {
viewModel.didSelect(contact: contact) viewModel.didSelect(contact: contact)
} }
} }
......
...@@ -4,9 +4,20 @@ import SnapKit ...@@ -4,9 +4,20 @@ import SnapKit
final class CreateGroupView: UIView { final class CreateGroupView: UIView {
let stackView = UIStackView() let stackView = UIStackView()
let tableView = UITableView()
let searchComponent = SearchComponent() let searchComponent = SearchComponent()
lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) lazy var topCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
lazy var bottomCollectionView: UICollectionView = {
var config = UICollectionLayoutListConfiguration(appearance: .plain)
config.backgroundColor = Asset.neutralWhite.color
config.showsSeparators = false
let layout = UICollectionViewCompositionalLayout.list(using: config)
let collectionView = UICollectionView(frame: bounds, collectionViewLayout: layout)
collectionView.contentInset = .init(top: 15, left: 0, bottom: 0, right: 0)
return collectionView
//tableView.setEditing(true, animated: true)
}()
let layout: UICollectionViewFlowLayout = { let layout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout() let layout = UICollectionViewFlowLayout()
...@@ -20,24 +31,18 @@ final class CreateGroupView: UIView { ...@@ -20,24 +31,18 @@ final class CreateGroupView: UIView {
super.init(frame: .zero) super.init(frame: .zero)
backgroundColor = Asset.neutralWhite.color backgroundColor = Asset.neutralWhite.color
tableView.separatorStyle = .none
tableView.tintColor = Asset.brandPrimary.color
tableView.backgroundColor = Asset.neutralWhite.color
tableView.allowsMultipleSelectionDuringEditing = true
tableView.setEditing(true, animated: true)
searchComponent.set( searchComponent.set(
placeholder: "Search connections", placeholder: "Search connections",
imageAtRight: UIImage.color(.clear) imageAtRight: UIImage.color(.clear)
) )
collectionView.backgroundColor = Asset.neutralWhite.color topCollectionView.backgroundColor = Asset.neutralWhite.color
collectionView.contentInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30) topCollectionView.contentInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
stackView.spacing = 31 stackView.spacing = 31
stackView.axis = .vertical stackView.axis = .vertical
stackView.addArrangedSubview(collectionView) stackView.addArrangedSubview(topCollectionView)
stackView.addArrangedSubview(tableView) stackView.addArrangedSubview(bottomCollectionView)
addSubview(stackView) addSubview(stackView)
addSubview(searchComponent) addSubview(searchComponent)
...@@ -55,7 +60,7 @@ final class CreateGroupView: UIView { ...@@ -55,7 +60,7 @@ final class CreateGroupView: UIView {
make.bottom.equalToSuperview() make.bottom.equalToSuperview()
} }
collectionView.snp.makeConstraints { $0.height.equalTo(100) } topCollectionView.snp.makeConstraints { $0.height.equalTo(100) }
} }
required init?(coder: NSCoder) { nil } required init?(coder: NSCoder) { nil }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment