-
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.
Migrate AddCard scene from MVC to VIP
- Loading branch information
Showing
12 changed files
with
302 additions
and
128 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
42 changes: 42 additions & 0 deletions
42
SWDestinyTrades/Classes/AddCard/Factory/AddCardViewControllerFactory.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,42 @@ | ||
// | ||
// AddCardViewControllerFactory.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 30/12/23. | ||
// Copyright © 2023 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
final class AddCardViewControllerFactory: ViewControllerFactory { | ||
|
||
private let database: DatabaseProtocol? | ||
private let addCardType: AddCardType | ||
private let personDTO: PersonDTO? | ||
private let userCollectionDTO: UserCollectionDTO? | ||
|
||
init(database: DatabaseProtocol?, | ||
addCardType: AddCardType, | ||
personDTO: PersonDTO? = nil, | ||
userCollectionDTO: UserCollectionDTO? = nil) { | ||
self.database = database | ||
self.addCardType = addCardType | ||
self.personDTO = personDTO | ||
self.userCollectionDTO = userCollectionDTO | ||
} | ||
|
||
func createViewController() -> UIViewController { | ||
let viewController = AddCardViewController() | ||
let router = AddCardNavigator(viewController) | ||
let interactor = AddCardInteractor() | ||
let viewModel = AddCardViewModel(person: personDTO, userCollection: userCollectionDTO, type: addCardType) | ||
let presenter = AddCardPresenter(view: viewController, | ||
interactor: interactor, | ||
database: database, | ||
navigator: router, | ||
viewModel: viewModel) | ||
viewController.presenter = presenter | ||
return viewController | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
SWDestinyTrades/Classes/AddCard/Interactor/AddCardInteractor.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,26 @@ | ||
// | ||
// AddCardInteractor.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 30/12/23. | ||
// Copyright © 2023 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol AddCardInteractorProtocol { | ||
func retrieveAllCards() async throws -> [CardDTO] | ||
} | ||
|
||
final class AddCardInteractor: AddCardInteractorProtocol { | ||
|
||
private let service: SWDestinyServiceProtocol | ||
|
||
init(service: SWDestinyServiceProtocol = SWDestinyService()) { | ||
self.service = service | ||
} | ||
|
||
func retrieveAllCards() async throws -> [CardDTO] { | ||
return try await service.retrieveAllCards() | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
SWDestinyTrades/Classes/AddCard/Presenter/AddCardPresenter.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,122 @@ | ||
// | ||
// AddCardPresenter.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 30/12/23. | ||
// Copyright © 2023 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol AddCardPresenterProtocol { | ||
func fetchAllCards() | ||
func insert(card: CardDTO) | ||
func doingSearch(_ query: String) | ||
func cardDetailButtonTouched(with card: CardDTO) | ||
} | ||
|
||
final class AddCardPresenter: AddCardPresenterProtocol { | ||
|
||
private let interactor: AddCardInteractorProtocol | ||
private let database: DatabaseProtocol? | ||
private let navigator: AddCardNavigator | ||
private let viewModel: AddCardViewModel | ||
|
||
private weak var view: AddCardViewProtocol? | ||
private var cards = [CardDTO]() | ||
|
||
init(view: AddCardViewProtocol, | ||
interactor: AddCardInteractorProtocol, | ||
database: DatabaseProtocol?, | ||
navigator: AddCardNavigator, | ||
viewModel: AddCardViewModel) { | ||
self.view = view | ||
self.interactor = interactor | ||
self.database = database | ||
self.navigator = navigator | ||
self.viewModel = viewModel | ||
} | ||
|
||
func fetchAllCards() { | ||
view?.startLoading() | ||
Task { [weak self] in | ||
guard let self else { return } | ||
|
||
do { | ||
let allCards = try await self.interactor.retrieveAllCards() | ||
await MainActor.run { [weak self] in | ||
self?.view?.stopLoading() | ||
self?.view?.updateSearchList(allCards) | ||
self?.cards = allCards | ||
} | ||
} catch { | ||
await MainActor.run { | ||
ToastMessages.showNetworkErrorMessage() | ||
LoggerManager.shared.log(event: .allCards, parameters: ["error": error.localizedDescription]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func insert(card: CardDTO) { | ||
switch viewModel.type { | ||
case .lent: | ||
insertToLentMe(card: card) | ||
case .borrow: | ||
insertToBorrowed(card: card) | ||
case .collection: | ||
insertToCollection(card: card) | ||
} | ||
} | ||
|
||
func doingSearch(_ query: String) { | ||
view?.doingSearch(query) | ||
} | ||
|
||
func cardDetailButtonTouched(with card: CardDTO) { | ||
navigator.navigate(to: .cardDetail(database: database, with: cards, card: card)) | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
private func insertToBorrowed(card: CardDTO) { | ||
if let person = viewModel.person, !person.borrowed.contains(where: { $0.code == card.code }) { | ||
try? database?.update { [weak self] in | ||
person.borrowed.append(card) | ||
self?.showSuccessMessage(card: card) | ||
} | ||
let personDataDict: [String: PersonDTO] = ["personDTO": person] | ||
NotificationCenter.default.post(name: NotificationKey.reloadTableViewNotification, object: nil, userInfo: personDataDict) | ||
} else { | ||
ToastMessages.showInfoMessage(title: "", message: L10n.alreadyAdded) | ||
} | ||
} | ||
|
||
private func insertToLentMe(card: CardDTO) { | ||
if let person = viewModel.person, !person.lentMe.contains(where: { $0.code == card.code }) { | ||
try? database?.update { [weak self] in | ||
person.lentMe.append(card) | ||
self?.showSuccessMessage(card: card) | ||
} | ||
let personDataDict: [String: PersonDTO] = ["personDTO": person] | ||
NotificationCenter.default.post(name: NotificationKey.reloadTableViewNotification, object: nil, userInfo: personDataDict) | ||
} else { | ||
ToastMessages.showInfoMessage(title: "", message: L10n.alreadyAdded) | ||
} | ||
} | ||
|
||
private func insertToCollection(card: CardDTO) { | ||
if let userCollection = viewModel.userCollection, !userCollection.myCollection.contains(where: { $0.code == card.code }) { | ||
try? database?.update { [weak self] in | ||
userCollection.myCollection.append(card) | ||
self?.showSuccessMessage(card: card) | ||
} | ||
} else { | ||
ToastMessages.showInfoMessage(title: "", message: L10n.alreadyAdded) | ||
} | ||
} | ||
|
||
private func showSuccessMessage(card: CardDTO) { | ||
LoadingHUD.show(.labeledSuccess(title: L10n.added, subtitle: card.name)) | ||
} | ||
} |
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
Oops, something went wrong.