-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
268 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
SWDestinyTrades/Classes/LoanDetail/Presenter/LoansDetailPresenter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// LoansDetailPresenter.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 25/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
protocol LoansDetailPresenterProtocol: AnyObject { | ||
func loadDataFromRealm() | ||
func setNavigationTitle() | ||
func navigateToCardDetail(with card: CardDTO, destination: AddCardType) | ||
func navigateToAddCard(type: AddCardType) | ||
} | ||
|
||
final class LoansDetailPresenter: LoansDetailPresenterProtocol { | ||
|
||
private let database: DatabaseProtocol? | ||
private var person: PersonDTO | ||
private let navigator: LoanDetailNavigator | ||
|
||
private weak var controller: LoansDetailViewControllerProtocol? | ||
|
||
init(controller: LoansDetailViewControllerProtocol?, | ||
database: DatabaseProtocol?, | ||
person: PersonDTO, | ||
navigator: LoanDetailNavigator) { | ||
self.controller = controller | ||
self.database = database | ||
self.person = person | ||
self.navigator = navigator | ||
|
||
NotificationCenter.default.addObserver(self, | ||
selector: #selector(reloadTableView), | ||
name: NotificationKey.reloadTableViewNotification, | ||
object: nil) | ||
} | ||
|
||
func loadDataFromRealm() { | ||
controller?.updateTableViewData(person: person) | ||
} | ||
|
||
func setNavigationTitle() { | ||
controller?.setNavigationTitle("\(person.name) \(person.lastName)") | ||
} | ||
|
||
func navigateToCardDetail(with card: CardDTO, destination: AddCardType) { | ||
let source = destination == .lent ? person.lentMe : person.borrowed | ||
navigator.navigate(to: .cardDetail(database: database, with: Array(source), card: card)) | ||
} | ||
|
||
func navigateToAddCard(type: AddCardType) { | ||
navigator.navigate(to: .addCard(database: database, with: person, type: type)) | ||
} | ||
|
||
// MARK: - UIBarButton Actions | ||
|
||
@objc | ||
private func reloadTableView(_ notification: NSNotification) { | ||
if let personDTO = notification.userInfo?["personDTO"] as? PersonDTO { | ||
person = personDTO | ||
loadDataFromRealm() | ||
} | ||
} | ||
} | ||
|
||
extension LoansDetailPresenter: LoansDetailsProtocol { | ||
|
||
func stepperValueChanged(newValue: Int, card: CardDTO) { | ||
try? database?.update { | ||
card.quantity = newValue | ||
} | ||
} | ||
|
||
func remove(from section: AddCardType, at index: Int) { | ||
try? database?.update { [weak self] in | ||
switch section { | ||
case .lent: | ||
self?.person.lentMe.remove(at: index) | ||
case .borrow: | ||
self?.person.borrowed.remove(at: index) | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
SWDestinyTrades/Classes/LoanDetail/Protocols/LoansDetailViewControllerProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// LoansDetailViewControllerProtocol.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 25/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol LoansDetailViewControllerProtocol: AnyObject { | ||
func updateTableViewData(person: PersonDTO) | ||
func setNavigationTitle(_ title: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
SWDestinyTradesTests/Screens/LoanDetail/Doubles/LoansDetailViewControllerSpy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// LoansDetailViewControllerSpy.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 25/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class LoansDetailViewControllerSpy: UIViewController, LoansDetailViewControllerProtocol { | ||
|
||
private(set) var didCallUpdateTableViewData = [PersonDTO]() | ||
func updateTableViewData(person: PersonDTO) { | ||
didCallUpdateTableViewData.append(person) | ||
} | ||
|
||
private(set) var didCallSetNavigationTitle = [String]() | ||
func setNavigationTitle(_ title: String) { | ||
didCallSetNavigationTitle.append(title) | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
SWDestinyTradesTests/Screens/LoanDetail/Presenter/LoansDetailPresenterTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// | ||
// LoansDetailPresenterTests.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 25/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class LoansDetailPresenterTests: XCTestCase { | ||
|
||
private var sut: LoansDetailPresenter! | ||
private var controller: LoansDetailViewControllerSpy! | ||
private var database: RealmDatabase? | ||
private var navigator: LoanDetailNavigator! | ||
private var navigationController: UINavigationControllerMock! | ||
private var person: PersonDTO! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
person = .stub(lentMe: [.stub(), .stub()], borrowed: [.stub(), .stub()]) | ||
controller = LoansDetailViewControllerSpy() | ||
database = RealmDatabaseHelper.createMemoryDatabase(identifier: #function) | ||
navigationController = UINavigationControllerMock(rootViewController: controller) | ||
navigator = LoanDetailNavigator(controller) | ||
sut = LoansDetailPresenter(controller: controller, | ||
database: database, | ||
person: person, | ||
navigator: navigator) | ||
} | ||
|
||
override func tearDown() { | ||
controller = nil | ||
database = nil | ||
navigator = nil | ||
navigationController = nil | ||
sut = nil | ||
super.tearDown() | ||
} | ||
|
||
// MARK: - Test loadDataFromRealm | ||
|
||
func test_loadDataFromRealm() { | ||
sut.loadDataFromRealm() | ||
|
||
XCTAssertEqual(controller.didCallUpdateTableViewData.count, 1) | ||
XCTAssertNotNil(controller.didCallUpdateTableViewData[0]) | ||
} | ||
|
||
// MARK: - Test setNavigationTitle | ||
|
||
func test_setNavigationTitle() { | ||
sut.setNavigationTitle() | ||
|
||
XCTAssertEqual(controller.didCallSetNavigationTitle.count, 1) | ||
XCTAssertEqual(controller.didCallSetNavigationTitle[0], "User Mock") | ||
} | ||
|
||
// MARK: - Test navigateToCardDetail | ||
|
||
func test_navigateToCardDetailViewController_when_the_destination_is_borrow() { | ||
sut.navigateToCardDetail(with: .stub(), destination: .borrow) | ||
|
||
XCTAssertTrue(navigationController.currentPushedViewController is CardDetailViewController) | ||
} | ||
|
||
func test_navigateToCardDetailViewController_when_the_destination_is_lent() { | ||
sut.navigateToCardDetail(with: .stub(), destination: .lent) | ||
|
||
XCTAssertTrue(navigationController.currentPushedViewController is CardDetailViewController) | ||
} | ||
|
||
// MARK: - Test navigateToAddCard | ||
|
||
func test_navigateToAddCard() { | ||
sut.navigateToAddCard(type: .borrow) | ||
|
||
XCTAssertTrue(navigationController.currentPushedViewController is AddCardViewController) | ||
} | ||
|
||
// MARK: - Test reloadTableView | ||
|
||
func test_reloadTableView() { | ||
|
||
let notification = Notification(name: NotificationKey.reloadTableViewNotification, object: nil, userInfo: ["personDTO": PersonDTO.stub()]) | ||
|
||
NotificationCenter.default.post(notification) | ||
|
||
XCTAssertEqual(controller.didCallUpdateTableViewData.count, 1) | ||
XCTAssertNotNil(controller.didCallUpdateTableViewData[0]) | ||
} | ||
|
||
// MARK: - Test stepperValueChanged | ||
|
||
func test_stepperValueChanged() { | ||
let card: CardDTO = .stub() | ||
sut.stepperValueChanged(newValue: 4, card: card) | ||
|
||
XCTAssertEqual(card.quantity, 4) | ||
} | ||
|
||
// MARK: - Test remove | ||
|
||
func test_remove_from_borrow() { | ||
XCTAssertEqual(person.borrowed.count, 2) | ||
|
||
sut.remove(from: .borrow, at: 0) | ||
|
||
XCTAssertEqual(person.borrowed.count, 1) | ||
} | ||
|
||
func test_remove_from_lent() { | ||
XCTAssertEqual(person.lentMe.count, 2) | ||
|
||
sut.remove(from: .lent, at: 0) | ||
|
||
XCTAssertEqual(person.lentMe.count, 1) | ||
} | ||
|
||
func test_remove_from_another_case() { | ||
XCTAssertEqual(person.lentMe.count, 2) | ||
XCTAssertEqual(person.borrowed.count, 2) | ||
|
||
sut.remove(from: .collection, at: 0) | ||
|
||
XCTAssertEqual(person.lentMe.count, 2) | ||
XCTAssertEqual(person.borrowed.count, 2) | ||
} | ||
} |