diff --git a/solutions/devsprint-caio-santos-7/FinanceApp/Screens/ActivityDetails/ActivityListView.swift b/solutions/devsprint-caio-santos-7/FinanceApp/Screens/ActivityDetails/ActivityListView.swift new file mode 100644 index 0000000..0911d54 --- /dev/null +++ b/solutions/devsprint-caio-santos-7/FinanceApp/Screens/ActivityDetails/ActivityListView.swift @@ -0,0 +1,58 @@ +// +// ActivityListView.swift +// FinanceApp +// +// Created by Mauricio on 18/10/22. +// + +import UIKit + +class ActivityListView: UIView { + private lazy var tableView: UITableView = { + let tableView = UITableView(frame: .zero) + tableView.translatesAutoresizingMaskIntoConstraints = false + tableView.register(ActivityCellView.self, forCellReuseIdentifier: ActivityCellView.reuseIdentifier) + tableView.separatorStyle = .none + return tableView + }() + + override init(frame: CGRect) { + super.init(frame: frame) + self.configViews() + self.buildHierarchy() + self.setupConstraints() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func reloadData() { + tableView.reloadData() + } + + public func configTableViewProtocol(delegate: UITableViewDelegate, + dataSource: UITableViewDataSource) { + self.tableView.delegate = delegate + self.tableView.dataSource = dataSource + } +} + +extension ActivityListView: ViewCodable { + func configViews (){ + backgroundColor = .white + } + + func buildHierarchy(){ + addSubview(tableView) + } + + func setupConstraints(){ + NSLayoutConstraint.activate([ + tableView.topAnchor.constraint(equalTo: self.topAnchor), + tableView.leadingAnchor.constraint(equalTo: self.leadingAnchor), + tableView.trailingAnchor.constraint(equalTo: self.trailingAnchor), + tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor) + ]) + } +} diff --git a/solutions/devsprint-caio-santos-7/FinanceApp/Screens/Home/HomeView.swift b/solutions/devsprint-caio-santos-7/FinanceApp/Screens/Home/HomeView.swift index 0e0af5f..22cbaf0 100644 --- a/solutions/devsprint-caio-santos-7/FinanceApp/Screens/Home/HomeView.swift +++ b/solutions/devsprint-caio-santos-7/FinanceApp/Screens/Home/HomeView.swift @@ -25,18 +25,15 @@ final class HomeView: UIView { return element }() - private lazy var tableView: UITableView = { - let tableView = UITableView(frame: .zero) - tableView.translatesAutoresizingMaskIntoConstraints = false - tableView.register(ActivityCellView.self, forCellReuseIdentifier: ActivityCellView.reuseIdentifier) - tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.reuseIdentifier) - tableView.dataSource = self - tableView.delegate = self - return tableView + private lazy var activityListView: ActivityListView = { + let element = ActivityListView() + element.translatesAutoresizingMaskIntoConstraints = false + return element }() - + init() { super.init(frame: .zero) + self.activityListView.configTableViewProtocol(delegate: self, dataSource: self) self.setupViews() } @@ -49,7 +46,7 @@ final class HomeView: UIView { accountSummaryView.updateValues(balance: configuration.homeData.balance, savings: configuration.homeData.savings, spending: configuration.homeData.spending) - tableView.reloadData() + activityListView.reloadData() } } @@ -63,7 +60,7 @@ private extension HomeView { func configureSubviews() { addSubview(accountSummaryView) - addSubview(tableView) + addSubview(activityListView) } func configureSubviewsConstraints() { @@ -72,17 +69,18 @@ private extension HomeView { accountSummaryView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 16), accountSummaryView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -16), - tableView.topAnchor.constraint(equalTo: accountSummaryView.bottomAnchor, constant: 16), - tableView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor), - tableView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor), - tableView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor) + activityListView.topAnchor.constraint(equalTo: accountSummaryView.bottomAnchor, constant: 16), + activityListView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor), + activityListView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor), + activityListView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor) ]) } } extension HomeView: UITableViewDataSource, UITableViewDelegate { + public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return activities.count + activities.count } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { @@ -90,15 +88,15 @@ extension HomeView: UITableViewDataSource, UITableViewDelegate { indexPath.row < activities.count else { return .init() } - - cell.updateValues(activity: activities[indexPath.row]) return cell } - func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { delegate?.didSelectActivity() } + + func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + "Activity" + } } - diff --git a/solutions/devsprint-caio-santos-7/FinanceAppTests/Screens/ActivityDetails/ActivityListViewTests.swift b/solutions/devsprint-caio-santos-7/FinanceAppTests/Screens/ActivityDetails/ActivityListViewTests.swift new file mode 100644 index 0000000..c7c7fc1 --- /dev/null +++ b/solutions/devsprint-caio-santos-7/FinanceAppTests/Screens/ActivityDetails/ActivityListViewTests.swift @@ -0,0 +1,56 @@ +// +// ActivityListViewTests.swift +// FinanceAppTests +// +// Created by Caio Santos on 18/10/22. +// + +import UIKit +import SnapshotTesting +import XCTest + +@testable import FinanceApp + +final class ActivityListViewTests: XCTestCase { + + private var sut: ActivityListView? + private var activities: [Activity] = [ + .init(name: "Test", price: 99, time: "Test"), + .init(name: "Test", price: 99, time: "Test"), + .init(name: "Test", price: 99, time: "Test"), + .init(name: "Test", price: 99, time: "Test"), + ] + + override func setUp() { +// SnapshotTesting.isRecording = true + sut = ActivityListView() + } + + func testRenderView() throws { + let unwrappedSut = try XCTUnwrap(sut) + unwrappedSut.configTableViewProtocol(delegate: self, dataSource: self) + assertSnapshot(matching: unwrappedSut, as: .image(size: .init(width: UIScreen.main.bounds.width, + height: 527))) + } +} + +extension ActivityListViewTests: UITableViewDataSource, UITableViewDelegate { + + public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return activities.count + } + + public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + guard let cell: ActivityCellView = .createCell(for: tableView, at: indexPath), + indexPath.row < activities.count else { + return .init() + } + cell.updateValues(activity: activities[indexPath.row]) + return cell + } + + func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + "Activity" + } +} + diff --git a/solutions/devsprint-caio-santos-7/FinanceAppTests/Screens/ActivityDetails/__Snapshots__/ActivityListViewTests/testRenderView.1.png b/solutions/devsprint-caio-santos-7/FinanceAppTests/Screens/ActivityDetails/__Snapshots__/ActivityListViewTests/testRenderView.1.png new file mode 100644 index 0000000..609fe51 Binary files /dev/null and b/solutions/devsprint-caio-santos-7/FinanceAppTests/Screens/ActivityDetails/__Snapshots__/ActivityListViewTests/testRenderView.1.png differ