Skip to content

Commit

Permalink
Merge pull request #940 from DeluxeAlonso/feature/code-cleanup
Browse files Browse the repository at this point in the history
Feature/code cleanup
  • Loading branch information
DeluxeAlonso authored Jul 24, 2024
2 parents 2495b8a + 53fc110 commit 178b39e
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class ProfileAssembly: Assembly {
accountUseCase: useCaseProvider.accountUseCase())
}

container.register(ProfileViewModelProtocol.self) { (resolver, user: User) in
container.register(ProfileViewModelProtocol.self) { (resolver, user: UserProtocol) in
guard let interactor = resolver.resolve(ProfileInteractorProtocol.self) else {
fatalError("ProfileInteractorProtocol dependency could not be resolved")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,3 @@ extension ImageConfigurable {
}

}

extension User: ImageConfigurable {

var avatarImageURL: URL? {
guard let avatarPath, let avatarImageBaseURLString else { return nil }
let urlString = avatarImageBaseURLString.appending(avatarPath)
return URL(string: urlString)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ final class AuthenticationHandler: AuthenticationHandlerProtocol {

// MARK: - Authentitation Persistence

func currentUser() -> User? {
func currentUser() -> UserProtocol? {
guard let userId = authUseCase.currentUserId() else { return nil }
return userUseCase.find(with: userId)
return userUseCase.find(with: userId).flatMap(UserModel.init)
}

func isUserSignedIn() -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
// Copyright © 2021 Alonso. All rights reserved.
//

import UpcomingMoviesDomain

protocol AuthenticationHandlerProtocol {

func currentUser() -> User?
func currentUser() -> UserProtocol?
func isUserSignedIn() -> Bool
func deleteCurrentUser()

Expand Down
26 changes: 21 additions & 5 deletions UpcomingMovies/Helpers/Protocols/Models/UserProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,46 @@
import Foundation
import UpcomingMoviesDomain

protocol UserProtocol {
protocol UserProtocol: ImageConfigurable {

var id: Int { get }
var name: String { get }
var username: String { get }
var includeAdult: Bool { get }
var avatarPath: String? { get }
var avatarImageURL: URL? { get }

func hasUpdatedInfo(_ newUserInfo: UserProtocol) -> Bool

}

struct UserModel: UserProtocol {

let id: Int
let name: String
let username: String
let includeAdult: Bool
let avatarPath: String?


private let avatarPath: String?

var avatarImageURL: URL? {
guard let avatarPath, let avatarImageBaseURLString else { return nil }
let urlString = avatarImageBaseURLString.appending(avatarPath)
return URL(string: urlString)
}

init(_ user: User) {
self.id = user.id
self.name = user.name
self.username = user.username
self.includeAdult = user.includeAdult
self.avatarPath = user.avatarPath
}

func hasUpdatedInfo(_ newUserInfo: UserProtocol) -> Bool {
self.id != newUserInfo.id ||
self.name != newUserInfo.name ||
self.username != newUserInfo.username ||
self.avatarImageURL != newUserInfo.avatarImageURL
}

}
3 changes: 1 addition & 2 deletions UpcomingMovies/Scenes/Account/AccountCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import UIKit
import UpcomingMoviesDomain

final class AccountCoordinator: BaseCoordinator, AccountCoordinatorProtocol {

Expand Down Expand Up @@ -35,7 +34,7 @@ final class AccountCoordinator: BaseCoordinator, AccountCoordinatorProtocol {
coordinator.start(coordinatorMode: .embed(parentViewController: parentViewController, containerView: nil))
}

func embedProfileViewController(on parentViewController: ProfileViewControllerDelegate, for user: User) {
func embedProfileViewController(on parentViewController: ProfileViewControllerDelegate, for user: UserProtocol) {
navigationController.setNavigationBarHidden(false, animated: true)

let coordinator = ProfileCoordinator(navigationController: navigationController, user: user, delegate: parentViewController)
Expand Down
2 changes: 1 addition & 1 deletion UpcomingMovies/Scenes/Account/AccountInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class AccountInteractor: AccountInteractorProtocol {
self.authHandler = authHandler
}

func currentUser() -> User? {
func currentUser() -> UserProtocol? {
authHandler.currentUser()
}

Expand Down
7 changes: 3 additions & 4 deletions UpcomingMovies/Scenes/Account/AccountProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@
//

import UIKit
import UpcomingMoviesDomain

protocol AccountViewModelProtocol {

var showAuthPermission: AnyPublishBindable<URL> { get }
var didReceiveError: AnyPublishBindable<Void> { get }

func isUserSignedIn() -> Bool
func currentUser() -> User?
func currentUser() -> UserProtocol?

}

protocol AccountInteractorProtocol {

func currentUser() -> User?
func currentUser() -> UserProtocol?

}

protocol AccountCoordinatorProtocol: AnyObject {

func embedSignInViewController(on parentViewController: SignInViewControllerDelegate)
func embedProfileViewController(on parentViewController: ProfileViewControllerDelegate, for user: User)
func embedProfileViewController(on parentViewController: ProfileViewControllerDelegate, for user: UserProtocol)

func removeSignInViewController(from parentViewController: UIViewController)
func removeProfileViewController(from parentViewController: UIViewController)
Expand Down
2 changes: 1 addition & 1 deletion UpcomingMovies/Scenes/Account/AccountViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class AccountViewModel: AccountViewModelProtocol {
currentUser() != nil
}

func currentUser() -> User? {
func currentUser() -> UserProtocol? {
interactor.currentUser()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import UpcomingMoviesDomain

protocol ProfileAccountInfoCellViewModelProtocol {

Expand All @@ -25,7 +24,7 @@ final class ProfileAccountInfoCellViewModel: ProfileAccountInfoCellViewModelProt

// MARK: - Initializers

init(userAccount: User) {
init(userAccount: UserProtocol) {
self.name = userAccount.name
self.username = userAccount.username
self.avatarImageURL = userAccount.avatarImageURL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
//

import UIKit
import UpcomingMoviesDomain

final class ProfileCoordinator: BaseCoordinator, ProfileCoordinatorProtocol {

private let user: User
private let user: UserProtocol
private weak var delegate: ProfileViewControllerDelegate?

init(navigationController: UINavigationController, user: User, delegate: ProfileViewControllerDelegate?) {
init(navigationController: UINavigationController, user: UserProtocol, delegate: ProfileViewControllerDelegate?) {
self.user = user
self.delegate = delegate
super.init(navigationController: navigationController)
Expand Down
4 changes: 2 additions & 2 deletions UpcomingMovies/Scenes/Account/Profile/ProfileInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ final class ProfileInteractor: ProfileInteractorProtocol {
}

// TODO: - Change this method to get the account detail given the id of a user account
func getAccountDetail(completion: @escaping (Result<User, Error>) -> Void) {
func getAccountDetail(completion: @escaping (Result<UserProtocol, Error>) -> Void) {
accountUseCase.getAccountDetail(completion: { result in
switch result {
case .success(let user):
self.userUseCase.saveUser(user)
completion(.success(user))
completion(.success(UserModel(user)))
case .failure(let error):
completion(.failure(error))
}
Expand Down
4 changes: 2 additions & 2 deletions UpcomingMovies/Scenes/Account/Profile/ProfileProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2020 Alonso. All rights reserved.
//

import UpcomingMoviesDomain
import UIKit

protocol ProfileViewModelProtocol {

Expand All @@ -31,7 +31,7 @@ protocol ProfileViewModelProtocol {

protocol ProfileInteractorProtocol {

func getAccountDetail(completion: @escaping (Result<User, Error>) -> Void)
func getAccountDetail(completion: @escaping (Result<UserProtocol, Error>) -> Void)
func signOutUser(completion: @escaping (Result<Bool, Error>) -> Void)

}
Expand Down
8 changes: 4 additions & 4 deletions UpcomingMovies/Scenes/Account/Profile/ProfileViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
// Copyright © 2019 Alonso. All rights reserved.
//

import UpcomingMoviesDomain
import Foundation

final class ProfileViewModel: ProfileViewModelProtocol {

// MARK: - Stored properties

private var userAccount: User
private var userAccount: UserProtocol
private let interactor: ProfileInteractorProtocol
private let factory: ProfileFactoryProtocol

Expand All @@ -28,7 +28,7 @@ final class ProfileViewModel: ProfileViewModelProtocol {

// MARK: - Initializers

init(userAccount: User,
init(userAccount: UserProtocol,
interactor: ProfileInteractorProtocol,
factory: ProfileFactoryProtocol) {
self.userAccount = userAccount
Expand Down Expand Up @@ -73,7 +73,7 @@ final class ProfileViewModel: ProfileViewModelProtocol {
guard let user = result.wrappedValue else { return }

// If there is no update to display we don't reload user account info
if self.userAccount != user {
if self.userAccount.hasUpdatedInfo(user) {
self.userAccount = user
self.reloadAccountInfo.send(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class SearchMoviesResultInteractor: SearchMoviesResultInteractorProtocol {

var didUpdateMovieSearches: (() -> Void)?

private var currentUser: User? {
private var currentUser: UserProtocol? {
authHandler.currentUser()
}

Expand Down
9 changes: 4 additions & 5 deletions UpcomingMoviesTests/Account/AccountViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import XCTest
@testable import UpcomingMovies
import UpcomingMoviesDomain
@testable import NetworkInfrastructure

final class AccountViewModelTests: XCTestCase {
Expand All @@ -30,7 +29,7 @@ final class AccountViewModelTests: XCTestCase {

func testCurrentUserNotNil() {
// Arrange
let userToTest = User.with()
let userToTest = MockUserProtocol()
// Act
mockInteractor.currentUserResult = userToTest
let user = viewModelToTest.currentUser()
Expand All @@ -40,7 +39,7 @@ final class AccountViewModelTests: XCTestCase {

func testCurrentUserNil() {
// Arrange
let userToTest: UpcomingMoviesDomain.User? = nil
let userToTest: UserProtocol? = nil
// Act
mockInteractor.currentUserResult = userToTest
let user = viewModelToTest.currentUser()
Expand All @@ -50,7 +49,7 @@ final class AccountViewModelTests: XCTestCase {

func testIsUserSignedInTrue() {
// Arrange
let userToTest = User.with()
let userToTest = MockUserProtocol()
// Act
mockInteractor.currentUserResult = userToTest
let isUserSignedIn = viewModelToTest.isUserSignedIn()
Expand All @@ -60,7 +59,7 @@ final class AccountViewModelTests: XCTestCase {

func testIsUserSignedInFalse() {
// Arrange
let userToTest: UpcomingMoviesDomain.User? = nil
let userToTest: UserProtocol? = nil
// Act
mockInteractor.currentUserResult = userToTest
let isUserSignedIn = viewModelToTest.isUserSignedIn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ProfileAccountInfoCellViewModelTests: XCTestCase {
func testName() {
// Arrange
let nameToTest = "Name"
let viewModel = createSUT(with: User.with(name: nameToTest))
let viewModel = createSUT(with: MockUserProtocol(name: nameToTest))
// Act
let viewModelName = viewModel.name
// Assert
Expand All @@ -25,14 +25,14 @@ final class ProfileAccountInfoCellViewModelTests: XCTestCase {
func testUsername() {
// Arrange
let usernameToTest = "Username"
let viewModel = createSUT(with: User.with(username: usernameToTest))
let viewModel = createSUT(with: MockUserProtocol(username: usernameToTest))
// Act
let viewModelUsername = viewModel.username
// Assert
XCTAssertEqual(usernameToTest, viewModelUsername)
}

private func createSUT(with user: User) -> ProfileAccountInfoCellViewModel {
private func createSUT(with user: UserProtocol) -> ProfileAccountInfoCellViewModel {
ProfileAccountInfoCellViewModel(userAccount: user)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ final class ProfileCoordinatorTests: XCTestCase {

func testBuild() {
// Arrange
let coordinator = createSUT(user: .with())
let coordinator = createSUT(user: MockUserProtocol())
// Act
let viewController = coordinator.build()
// Assert
XCTAssertNotNil(viewController.delegate)
}

private func createSUT(user: User) -> ProfileCoordinator {
private func createSUT(user: UserProtocol) -> ProfileCoordinator {
ProfileCoordinator(navigationController: navigationController, user: user, delegate: delegate)
}

Expand Down
12 changes: 9 additions & 3 deletions UpcomingMoviesTests/Account/Profile/ProfileViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class ProfileViewModelTests: XCTestCase {
try super.setUpWithError()
mockInteractor = MockProfileInteractor()
mockFactory = MockProfileViewFactory()
viewModelToTest = ProfileViewModel(userAccount: User.with(),
viewModelToTest = ProfileViewModel(userAccount: MockUserProtocol(),
interactor: mockInteractor,
factory: mockFactory)
}
Expand All @@ -34,7 +34,13 @@ final class ProfileViewModelTests: XCTestCase {

func testGetAccountDetailSuccessInfoReloaded() {
// Arrange
let userToTest = User.with(name: "Alonso")
let currentUser = MockUserProtocol()
currentUser.hasUpdatedInfoResult = true
viewModelToTest = ProfileViewModel(userAccount: currentUser,
interactor: mockInteractor,
factory: mockFactory)

let userToTest = MockUserProtocol(name: "Alonso")
let expectation = XCTestExpectation(description: "Reload account info")
// Act
viewModelToTest.reloadAccountInfo.bind { _ in
Expand All @@ -48,7 +54,7 @@ final class ProfileViewModelTests: XCTestCase {

func testGetAccountDetailSuccessInfoNotReloaded() {
// Arrange
let userToTest = User.with()
let userToTest = MockUserProtocol()
let expectation = XCTestExpectation(description: "Should not reload account info")
expectation.isInverted = true
// Act
Expand Down
Loading

0 comments on commit 178b39e

Please sign in to comment.