From 09664e543d6bbbe7d419700195413a2c216e49a3 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Fri, 22 Jul 2022 21:44:01 -0300
Subject: [PATCH] Working on refactoring group creation

---
 .../Controllers/CreateGroupController.swift   | 60 ++++++++++---------
 .../Views/CreateGroupView.swift               | 31 ++++++----
 2 files changed, 49 insertions(+), 42 deletions(-)

diff --git a/Sources/ContactListFeature/Controllers/CreateGroupController.swift b/Sources/ContactListFeature/Controllers/CreateGroupController.swift
index c6415566..fdca1638 100644
--- a/Sources/ContactListFeature/Controllers/CreateGroupController.swift
+++ b/Sources/ContactListFeature/Controllers/CreateGroupController.swift
@@ -15,12 +15,12 @@ public final class CreateGroupController: UIViewController {
     lazy private var screenView = CreateGroupView()
 
     private var selectedElements = [Contact]() {
-        didSet { screenView.tableView.reloadData() }
+        didSet { screenView.bottomCollectionView.reloadData() }
     }
     private let viewModel = CreateGroupViewModel()
     private var cancellables = Set<AnyCancellable>()
-    private var tableDataSource: UITableViewDiffableDataSource<SectionId, Contact>!
-    private var collectionDataSource: UICollectionViewDiffableDataSource<SectionId, Contact>!
+    private var topCollectionDataSource: UICollectionViewDiffableDataSource<SectionId, Contact>!
+    private var bottomCollectionDataSource: UICollectionViewDiffableDataSource<SectionId, Contact>!
 
     private var count = 0 {
         didSet {
@@ -43,7 +43,7 @@ public final class CreateGroupController: UIViewController {
     public override func viewDidLoad() {
         super.viewDidLoad()
         setupNavigationBar()
-        setupTableAndCollection()
+        setupCollectionViews()
         setupBindings()
     }
 
@@ -64,13 +64,12 @@ public final class CreateGroupController: UIViewController {
         navigationItem.rightBarButtonItem = UIBarButtonItem(customView: createButton)
     }
 
-    private func setupTableAndCollection() {
-        screenView.tableView.rowHeight = 64.0
-        screenView.tableView.register(AvatarCell.self)
-        screenView.collectionView.register(CreateGroupCollectionCell.self)
+    private func setupCollectionViews() {
+        screenView.bottomCollectionView.register(AvatarCell.self)
+        screenView.topCollectionView.register(CreateGroupCollectionCell.self)
 
-        collectionDataSource = UICollectionViewDiffableDataSource<SectionId, Contact>(
-            collectionView: screenView.collectionView
+        topCollectionDataSource = UICollectionViewDiffableDataSource<SectionId, Contact>(
+            collectionView: screenView.topCollectionView
         ) { [weak viewModel] collectionView, indexPath, contact in
             let cell: CreateGroupCollectionCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
 
@@ -81,26 +80,28 @@ public final class CreateGroupController: UIViewController {
             return cell
         }
 
-        tableDataSource = DiffEditableDataSource<SectionId, Contact>(
-            tableView: screenView.tableView
-        ) { [weak self] tableView, indexPath, contact in
-            let cell = tableView.dequeueReusableCell(forIndexPath: indexPath, ofType: AvatarCell.self)
+        bottomCollectionDataSource = UICollectionViewDiffableDataSource<SectionId, Contact>(
+            collectionView: screenView.bottomCollectionView
+        ) { [weak self] collectionView, indexPath, contact in
+            let cell: AvatarCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
             let title = (contact.nickname ?? contact.username) ?? ""
 
             cell.set(image: contact.photo, h1Text: title)
 
             if let selectedElements = self?.selectedElements, selectedElements.contains(contact) {
-                tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
+                collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
             } else {
-                tableView.deselectRow(at: indexPath, animated: true)
+                collectionView.deselectItem(at: indexPath, animated: true)
             }
 
             return cell
         }
 
-        screenView.tableView.delegate = self
-        screenView.tableView.dataSource = tableDataSource
-        screenView.collectionView.dataSource = collectionDataSource
+        screenView.bottomCollectionView.delegate = self
+        screenView.topCollectionView.dataSource = topCollectionDataSource
+        screenView.bottomCollectionView.tintColor = Asset.brandPrimary.color
+        screenView.bottomCollectionView.dataSource = bottomCollectionDataSource
+        screenView.bottomCollectionView.allowsMultipleSelectionDuringEditing = true
     }
 
     private func setupBindings() {
@@ -114,7 +115,7 @@ public final class CreateGroupController: UIViewController {
         selected
             .receive(on: DispatchQueue.main)
             .sink { [unowned self] in
-                screenView.collectionView.isHidden = $0.count < 1
+                screenView.topCollectionView.isHidden = $0.count < 1
 
                 count = $0.count
                 selectedElements = $0
@@ -128,11 +129,11 @@ public final class CreateGroupController: UIViewController {
             return snapshot
         }
         .receive(on: DispatchQueue.main)
-        .sink { [unowned self] in collectionDataSource.apply($0) }
+        .sink { [unowned self] in topCollectionDataSource.apply($0) }
         .store(in: &cancellables)
 
         viewModel.contacts
-            .map { contacts in
+            .map { contacts -> NSDiffableDataSourceSnapshot<SectionId, Contact> in
                 var snapshot = NSDiffableDataSourceSnapshot<SectionId, Contact>()
                 let sections = [SectionId()]
                 snapshot.appendSections(sections)
@@ -141,7 +142,8 @@ public final class CreateGroupController: UIViewController {
             }
             .receive(on: DispatchQueue.main)
             .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)
 
         screenView.searchComponent.textPublisher
@@ -161,7 +163,7 @@ public final class CreateGroupController: UIViewController {
                 coordinator.toGroupDrawer(
                     with: count + 1,
                     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)
@@ -172,15 +174,15 @@ public final class CreateGroupController: UIViewController {
     }
 }
 
-extension CreateGroupController: UITableViewDelegate {
-    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-        if let contact = tableDataSource.itemIdentifier(for: indexPath) {
+extension CreateGroupController: UICollectionViewDelegate {
+    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
+        if let contact = bottomCollectionDataSource.itemIdentifier(for: indexPath) {
             viewModel.didSelect(contact: contact)
         }
     }
 
-    public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
-        if let contact = tableDataSource.itemIdentifier(for: indexPath) {
+    public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
+        if let contact = bottomCollectionDataSource.itemIdentifier(for: indexPath) {
             viewModel.didSelect(contact: contact)
         }
     }
diff --git a/Sources/ContactListFeature/Views/CreateGroupView.swift b/Sources/ContactListFeature/Views/CreateGroupView.swift
index 0f7bd579..b333956b 100644
--- a/Sources/ContactListFeature/Views/CreateGroupView.swift
+++ b/Sources/ContactListFeature/Views/CreateGroupView.swift
@@ -4,9 +4,20 @@ import SnapKit
 
 final class CreateGroupView: UIView {
     let stackView = UIStackView()
-    let tableView = UITableView()
     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()
@@ -20,24 +31,18 @@ final class CreateGroupView: UIView {
         super.init(frame: .zero)
         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(
             placeholder: "Search connections",
             imageAtRight: UIImage.color(.clear)
         )
 
-        collectionView.backgroundColor = Asset.neutralWhite.color
-        collectionView.contentInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
+        topCollectionView.backgroundColor = Asset.neutralWhite.color
+        topCollectionView.contentInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
 
         stackView.spacing = 31
         stackView.axis = .vertical
-        stackView.addArrangedSubview(collectionView)
-        stackView.addArrangedSubview(tableView)
+        stackView.addArrangedSubview(topCollectionView)
+        stackView.addArrangedSubview(bottomCollectionView)
 
         addSubview(stackView)
         addSubview(searchComponent)
@@ -55,7 +60,7 @@ final class CreateGroupView: UIView {
             make.bottom.equalToSuperview()
         }
 
-        collectionView.snp.makeConstraints { $0.height.equalTo(100) }
+        topCollectionView.snp.makeConstraints { $0.height.equalTo(100) }
     }
 
     required init?(coder: NSCoder) { nil }
-- 
GitLab